Skip to content

Commit

Permalink
Duplicate to_s shortcut from CRuby
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Nov 22, 2024
1 parent 5b77eed commit f30ade0
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
import org.jruby.RubyBasicObject;
import org.jruby.RubyBignum;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.exceptions.RaiseException;
import org.jruby.util.ConvertBytes;
import org.jruby.util.IOOutputStream;
import org.jruby.util.TypeConverter;

import java.io.BufferedOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -366,7 +369,20 @@ public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyOb

Ruby runtime = context.runtime;

IRubyObject keyStr = key.callMethod(context, "to_s");
IRubyObject keyStr;
RubyClass keyClass = key.getType();
if (key instanceof RubyString) {
if (keyClass == runtime.getString()) {
keyStr = key;
} else {
keyStr = key.callMethod(context, "to_s");
}
} else if (keyClass == runtime.getSymbol()) {
keyStr = key.asString();
} else {
keyStr = TypeConverter.convertToType(key, runtime.getString(), "to_s");
}

if (keyStr.getMetaClass() == runtime.getString()) {
STRING_HANDLER.generate(context, session, (RubyString) keyStr, buffer);
} else {
Expand Down

0 comments on commit f30ade0

Please # to comment.