Skip to content

Commit

Permalink
Connect OutputStream-based generator to Ruby bits
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
headius authored and byroot committed Nov 21, 2024
1 parent db393d6 commit 9ad9583
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
9 changes: 7 additions & 2 deletions java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ private Generator() {
*/
public static <T extends IRubyObject> IRubyObject
generateJson(ThreadContext context, T object,
GeneratorState config) {
GeneratorState config, IRubyObject io) {
Session session = new Session(context, config);
Handler<? super T> handler = getHandlerFor(context.runtime, object);

return handler.generateNew(session, object);
if (io.isNil()) {
return handler.generateNew(session, object);
}

handler.generateToBuffer(session, object, new IOOutputStream(io));
return io;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions java/src/json/ext/GeneratorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public boolean basicLoad(Ruby runtime) throws IOException {
generatorModule.defineModuleUnder("GeneratorMethods");
GeneratorMethods.populate(info, generatorMethods);

runtime.getLoadService().require("json/ext/generator/state");

return true;
}
}
18 changes: 9 additions & 9 deletions java/src/json/ext/GeneratorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public static IRubyObject from_state(ThreadContext context,
}

@JRubyMethod(meta=true)
public static IRubyObject generate(ThreadContext context, IRubyObject klass, IRubyObject obj, IRubyObject opts) {
return fromState(context, opts).generate(context, obj);
public static IRubyObject generate(ThreadContext context, IRubyObject klass, IRubyObject obj, IRubyObject opts, IRubyObject io) {
return fromState(context, opts)._generate(context, obj, io);
}

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

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

@JRubyMethod(name="strict")
@JRubyMethod(name={"strict","strict?"})
public RubyBoolean strict_get(ThreadContext context) {
return context.getRuntime().newBoolean(strict);
}
Expand Down Expand Up @@ -484,8 +484,8 @@ private ByteList prepareByteList(ThreadContext context, IRubyObject value) {
* @param vOpts The options hash
* @return The receiver
*/
@JRubyMethod(alias = "merge")
public IRubyObject configure(ThreadContext context, IRubyObject vOpts) {
@JRubyMethod(visibility=Visibility.PRIVATE)
public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) {
OptionsReader opts = new OptionsReader(context, vOpts);

ByteList indent = opts.getString("indent");
Expand Down

0 comments on commit 9ad9583

Please sign in to comment.