From f30ade0fbcdd91a7b981c6c2b998899dcc0cd2ab Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Fri, 22 Nov 2024 09:13:37 -0600 Subject: [PATCH] Duplicate to_s shortcut from CRuby --- java/src/json/ext/Generator.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/java/src/json/ext/Generator.java b/java/src/json/ext/Generator.java index 6db539a3..a502b7c9 100644 --- a/java/src/json/ext/Generator.java +++ b/java/src/json/ext/Generator.java @@ -11,10 +11,12 @@ 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; @@ -22,6 +24,7 @@ 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; @@ -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 {