Skip to content

Commit

Permalink
Split generator to_json methods by arity
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Nov 21, 2024
1 parent 351e11d commit adb8e56
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 63 deletions.
21 changes: 14 additions & 7 deletions java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,28 @@ private Generator() {
/**
* Encodes the given object as a JSON string, using the given handler.
*/
static <T extends IRubyObject> RubyString
generateJson(ThreadContext context, T object,
Handler<? super T> handler, IRubyObject[] args) {
Session session = new Session(context, args.length > 0 ? args[0] : null);
static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T object, Handler<? super T> handler) {
Session session = new Session(context, null);
return session.infect(handler.generateNew(session, object));
}

static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T object, Handler<? super T> handler, IRubyObject arg0) {
Session session = new Session(context, arg0);
return session.infect(handler.generateNew(session, object));
}

/**
* Encodes the given object as a JSON string, detecting the appropriate handler
* for the given object.
*/
static <T extends IRubyObject> RubyString
generateJson(ThreadContext context, T object, IRubyObject[] args) {
static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T object) {
Handler<? super T> handler = getHandlerFor(context.runtime, object);
return generateJson(context, object, handler);
}

static <T extends IRubyObject> RubyString generateJson(ThreadContext context, T object, IRubyObject arg0) {
Handler<? super T> handler = getHandlerFor(context.runtime, object);
return generateJson(context, object, handler, args);
return generateJson(context, object, handler, arg0);
}

/**
Expand Down
142 changes: 86 additions & 56 deletions java/src/json/ext/GeneratorMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,48 +62,63 @@ private static void defineMethods(RubyModule parentModule,
submodule.defineAnnotatedMethods(klass);
}


public static class RbHash {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, (RubyHash)vSelf,
Generator.HASH_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyHash)vSelf, Generator.HASH_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyHash)vSelf, Generator.HASH_HANDLER, arg0);
}
}

public static class RbArray {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, (RubyArray)vSelf,
Generator.ARRAY_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyArray)vSelf, Generator.ARRAY_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyArray)vSelf, Generator.ARRAY_HANDLER, arg0);
}
}

public static class RbInteger {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, vSelf, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, vSelf);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, vSelf, arg0);
}
}

public static class RbFloat {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, (RubyFloat)vSelf,
Generator.FLOAT_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyFloat)vSelf, Generator.FLOAT_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyFloat)vSelf, Generator.FLOAT_HANDLER, arg0);
}
}

public static class RbString {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, (RubyString)vSelf,
Generator.STRING_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyString)vSelf, Generator.STRING_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyString)vSelf, Generator.STRING_HANDLER, arg0);
}

/**
Expand All @@ -112,12 +127,16 @@ public static IRubyObject to_json(ThreadContext context,
* <p>This method creates a JSON text from the result of a call to
* {@link #to_json_raw_object} of this String.
*/
@JRubyMethod(rest=true)
public static IRubyObject to_json_raw(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
@JRubyMethod
public static IRubyObject to_json_raw(ThreadContext context, IRubyObject vSelf) {
RubyHash obj = toJsonRawObject(context, Utils.ensureString(vSelf));
return Generator.generateJson(context, obj,
Generator.HASH_HANDLER, args);
return Generator.generateJson(context, obj, Generator.HASH_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json_raw(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
RubyHash obj = toJsonRawObject(context, Utils.ensureString(vSelf));
return Generator.generateJson(context, obj, Generator.HASH_HANDLER, arg0);
}

/**
Expand All @@ -128,9 +147,8 @@ public static IRubyObject to_json_raw(ThreadContext context,
* method should be used if you want to convert raw strings to JSON
* instead of UTF-8 strings, e.g. binary data.
*/
@JRubyMethod(rest=true)
public static IRubyObject to_json_raw_object(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
@JRubyMethod
public static IRubyObject to_json_raw_object(ThreadContext context, IRubyObject vSelf) {
return toJsonRawObject(context, Utils.ensureString(vSelf));
}

Expand All @@ -154,9 +172,8 @@ private static RubyHash toJsonRawObject(ThreadContext context,
return result;
}

@JRubyMethod(required=1, module=true)
public static IRubyObject included(ThreadContext context,
IRubyObject vSelf, IRubyObject module) {
@JRubyMethod(module=true)
public static IRubyObject included(ThreadContext context, IRubyObject vSelf, IRubyObject module) {
RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime());
return module.callMethod(context, "extend", info.stringExtendModule.get());
}
Expand All @@ -170,7 +187,7 @@ public static class StringExtend {
* array for the key "raw"). The Ruby String can be created by this
* module method.
*/
@JRubyMethod(required=1)
@JRubyMethod
public static IRubyObject json_create(ThreadContext context,
IRubyObject vSelf, IRubyObject vHash) {
Ruby runtime = context.getRuntime();
Expand All @@ -195,37 +212,50 @@ public static IRubyObject json_create(ThreadContext context,
}

public static class RbTrue {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, (RubyBoolean)vSelf,
Generator.TRUE_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyBoolean)vSelf, Generator.TRUE_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyBoolean)vSelf, Generator.TRUE_HANDLER, arg0);
}
}

public static class RbFalse {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, (RubyBoolean)vSelf,
Generator.FALSE_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, (RubyBoolean)vSelf, Generator.FALSE_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, (RubyBoolean)vSelf, Generator.FALSE_HANDLER, arg0);
}
}

public static class RbNil {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject vSelf, IRubyObject[] args) {
return Generator.generateJson(context, vSelf,
Generator.NIL_HANDLER, args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf) {
return Generator.generateJson(context, vSelf, Generator.NIL_HANDLER);
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject vSelf, IRubyObject arg0) {
return Generator.generateJson(context, vSelf, Generator.NIL_HANDLER, arg0);
}
}

public static class RbObject {
@JRubyMethod(rest=true)
public static IRubyObject to_json(ThreadContext context,
IRubyObject self, IRubyObject[] args) {
return RbString.to_json(context, self.asString(), args);
@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject self) {
return RbString.to_json(context, self.asString());
}

@JRubyMethod
public static IRubyObject to_json(ThreadContext context, IRubyObject self, IRubyObject arg0) {
return RbString.to_json(context, self.asString(), arg0);
}
}
}

0 comments on commit adb8e56

Please sign in to comment.