Skip to content

Commit 2ebe42c

Browse files
committed
Connect OutputStream-based generator to Ruby bits
This connects up the OutputStream-based generator logic to the incoming IO parameter (an IO or nil). Also included here are some small changes to support the new state.rb: * Require the state.rb file at the end of ext init. * Move State#configure to _configure. * Add State#strict? as an alias for strict.
1 parent 4581b1e commit 2ebe42c

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

java/src/json/ext/Generator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,16 @@ private Generator() {
5959
*/
6060
public static <T extends IRubyObject> IRubyObject
6161
generateJson(ThreadContext context, T object,
62-
GeneratorState config) {
62+
GeneratorState config, IRubyObject io) {
6363
Session session = new Session(context, config);
6464
Handler<? super T> handler = getHandlerFor(context.runtime, object);
6565

66-
return handler.generateNew(session, object);
66+
if (io.isNil()) {
67+
return handler.generateNew(session, object);
68+
}
69+
70+
handler.generateToBuffer(session, object, new IOOutputStream(io));
71+
return io;
6772
}
6873

6974
/**

java/src/json/ext/GeneratorService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public boolean basicLoad(Ruby runtime) throws IOException {
3737
generatorModule.defineModuleUnder("GeneratorMethods");
3838
GeneratorMethods.populate(info, generatorMethods);
3939

40+
runtime.getLoadService().require("json/ext/generator/state");
41+
4042
return true;
4143
}
4244
}

java/src/json/ext/GeneratorState.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public static IRubyObject from_state(ThreadContext context,
139139
}
140140

141141
@JRubyMethod(meta=true)
142-
public static IRubyObject generate(ThreadContext context, IRubyObject klass, IRubyObject obj, IRubyObject opts) {
143-
return fromState(context, opts).generate(context, obj);
142+
public static IRubyObject generate(ThreadContext context, IRubyObject klass, IRubyObject obj, IRubyObject opts, IRubyObject io) {
143+
return fromState(context, opts)._generate(context, obj, io);
144144
}
145145

146146
static GeneratorState fromState(ThreadContext context, IRubyObject opts) {
@@ -196,7 +196,7 @@ static GeneratorState fromState(ThreadContext context, RuntimeInfo info,
196196
*/
197197
@JRubyMethod(optional=1, visibility=Visibility.PRIVATE)
198198
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
199-
configure(context, args.length > 0 ? args[0] : null);
199+
_configure(context, args.length > 0 ? args[0] : null);
200200
return this;
201201
}
202202

@@ -228,9 +228,9 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject vOrig) {
228228
* the result. If no valid JSON document can be created this method raises
229229
* a GeneratorError exception.
230230
*/
231-
@JRubyMethod
232-
public IRubyObject generate(ThreadContext context, IRubyObject obj) {
233-
IRubyObject result = Generator.generateJson(context, obj, this);
231+
@JRubyMethod(visibility = Visibility.PRIVATE)
232+
public IRubyObject _generate(ThreadContext context, IRubyObject obj, IRubyObject io) {
233+
IRubyObject result = Generator.generateJson(context, obj, this, io);
234234
RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime());
235235
if (!(result instanceof RubyString)) {
236236
return result;
@@ -411,7 +411,7 @@ public boolean strict() {
411411
return strict;
412412
}
413413

414-
@JRubyMethod(name="strict")
414+
@JRubyMethod(name={"strict","strict?"})
415415
public RubyBoolean strict_get(ThreadContext context) {
416416
return context.getRuntime().newBoolean(strict);
417417
}
@@ -484,8 +484,8 @@ private ByteList prepareByteList(ThreadContext context, IRubyObject value) {
484484
* @param vOpts The options hash
485485
* @return The receiver
486486
*/
487-
@JRubyMethod(alias = "merge")
488-
public IRubyObject configure(ThreadContext context, IRubyObject vOpts) {
487+
@JRubyMethod(visibility=Visibility.PRIVATE)
488+
public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) {
489489
OptionsReader opts = new OptionsReader(context, vOpts);
490490

491491
ByteList indent = opts.getString("indent");

0 commit comments

Comments
 (0)