Skip to content

Commit 5f3b2b2

Browse files
committed
[fix] 2.5 compat on JRuby 9.2 Fixnum/Bignum -> Integer
slightly ugly but the only way to still compile under 1.7 once Ruby 1.9.3 gets dropped this should get ironed out ! resolves GH-336
1 parent f1d635d commit 5f3b2b2

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

java/src/json/ext/Generator.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.jruby.Ruby;
99
import org.jruby.RubyArray;
10+
import org.jruby.RubyBasicObject;
1011
import org.jruby.RubyBignum;
1112
import org.jruby.RubyBoolean;
1213
import org.jruby.RubyClass;
@@ -15,6 +16,7 @@
1516
import org.jruby.RubyHash;
1617
import org.jruby.RubyNumeric;
1718
import org.jruby.RubyString;
19+
import org.jruby.runtime.ClassIndex;
1820
import org.jruby.runtime.ThreadContext;
1921
import org.jruby.runtime.builtin.IRubyObject;
2022
import org.jruby.util.ByteList;
@@ -57,6 +59,19 @@ private Generator() {
5759
return handler.generateNew(session, object);
5860
}
5961

62+
// NOTE: drop this once Ruby 1.9.3 support is gone!
63+
private static final int FIXNUM = 1;
64+
private static final int BIGNUM = 2;
65+
private static final int ARRAY = 3;
66+
private static final int STRING = 4;
67+
private static final int NIL = 5;
68+
private static final int TRUE = 6;
69+
private static final int FALSE = 7;
70+
private static final int HASH = 10;
71+
private static final int FLOAT = 11;
72+
// hard-coded due JRuby 1.7 compatibility
73+
// https://github.com/jruby/jruby/blob/1.7.27/core/src/main/java/org/jruby/runtime/ClassIndex.java
74+
6075
/**
6176
* Returns the best serialization handler for the given object.
6277
*/
@@ -65,16 +80,24 @@ private Generator() {
6580
@SuppressWarnings("unchecked")
6681
private static <T extends IRubyObject>
6782
Handler<? super T> getHandlerFor(Ruby runtime, T object) {
68-
RubyClass metaClass = object.getMetaClass();
69-
if (metaClass == runtime.getString()) return (Handler)STRING_HANDLER;
70-
if (metaClass == runtime.getFixnum()) return (Handler)FIXNUM_HANDLER;
71-
if (metaClass == runtime.getHash()) return (Handler)HASH_HANDLER;
72-
if (metaClass == runtime.getArray()) return (Handler)ARRAY_HANDLER;
73-
if (object.isNil()) return (Handler)NIL_HANDLER;
74-
if (object == runtime.getTrue()) return (Handler)TRUE_HANDLER;
75-
if (object == runtime.getFalse()) return (Handler)FALSE_HANDLER;
76-
if (metaClass == runtime.getFloat()) return (Handler)FLOAT_HANDLER;
77-
if (metaClass == runtime.getBignum()) return (Handler)BIGNUM_HANDLER;
83+
switch (((RubyBasicObject) object).getNativeTypeIndex()) {
84+
// can not use getNativeClassIndex due 1.7 compatibility
85+
case NIL : return (Handler) NIL_HANDLER;
86+
case TRUE : return (Handler) TRUE_HANDLER;
87+
case FALSE : return (Handler) FALSE_HANDLER;
88+
case FLOAT : return (Handler) FLOAT_HANDLER;
89+
case FIXNUM : return (Handler) FIXNUM_HANDLER;
90+
case BIGNUM : return (Handler) BIGNUM_HANDLER;
91+
case STRING :
92+
if (((RubyBasicObject) object).getMetaClass() != runtime.getString()) break;
93+
return (Handler) STRING_HANDLER;
94+
case ARRAY :
95+
if (((RubyBasicObject) object).getMetaClass() != runtime.getArray()) break;
96+
return (Handler) ARRAY_HANDLER;
97+
case HASH :
98+
if (((RubyBasicObject) object).getMetaClass() != runtime.getHash()) break;
99+
return (Handler) HASH_HANDLER;
100+
}
78101
return GENERIC_HANDLER;
79102
}
80103

0 commit comments

Comments
 (0)