diff options
author | Florian Frank <flori@ping.de> | 2011-12-21 14:01:21 +0100 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2011-12-21 14:01:21 +0100 |
commit | 8e62190df53c9d120219a88e113a5588d2f5cd58 (patch) | |
tree | b04ef57befa04abe8a8fbe33f1b82e489886f11d | |
parent | 60ef7adb347b473cbcce9aacacb2943ae5fb4d4c (diff) | |
parent | 357aaaa3d96c5a42c567331abf5f41e8274ccb5b (diff) | |
download | json-8e62190df53c9d120219a88e113a5588d2f5cd58.tar.gz |
Merge branch 'master' into merge-master-into-MagLev-mastermerge-master-into-MagLev-master
Conflicts:
ext/json/ext/parser/parser.c
-rw-r--r-- | README.rdoc | 2 | ||||
-rw-r--r-- | ext/json/ext/generator/extconf.rb | 6 | ||||
-rw-r--r-- | ext/json/ext/parser/extconf.rb | 6 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.c | 148 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.rl | 10 | ||||
-rw-r--r-- | java/lib/bytelist-1.0.6.jar | bin | 10493 -> 0 bytes | |||
-rw-r--r-- | java/lib/jcodings.jar | bin | 242327 -> 0 bytes | |||
-rw-r--r-- | java/src/json/ext/OptionsReader.java | 8 | ||||
-rw-r--r-- | java/src/json/ext/Parser.java | 363 | ||||
-rw-r--r-- | java/src/json/ext/Parser.rl | 203 | ||||
-rwxr-xr-x | tests/test_json.rb | 55 |
11 files changed, 480 insertions, 321 deletions
diff --git a/README.rdoc b/README.rdoc index 072b43d..039d759 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,4 +1,4 @@ -= JSON implementation for Ruby http://travis-ci.org/flori/json.png += JSON implementation for Ruby http://travis-ci.org/flori/json.png?branch=master == Description diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb index a9eae14..32aed7f 100644 --- a/ext/json/ext/generator/extconf.rb +++ b/ext/json/ext/generator/extconf.rb @@ -5,9 +5,9 @@ unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3') end if CONFIG['CC'] =~ /gcc/ $CFLAGS << ' -Wall' - #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') - # $CFLAGS << ' -O0 -ggdb' - #end + unless $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') + $CFLAGS << ' -O0 -ggdb' + end end create_makefile 'json/ext/generator' diff --git a/ext/json/ext/parser/extconf.rb b/ext/json/ext/parser/extconf.rb index b09f122..4791829 100644 --- a/ext/json/ext/parser/extconf.rb +++ b/ext/json/ext/parser/extconf.rb @@ -5,9 +5,9 @@ unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3') end if CONFIG['CC'] =~ /gcc/ $CFLAGS << ' -Wall' - #unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') - # $CFLAGS << ' -O0 -ggdb' - #end + if $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb') + $CFLAGS << ' -O0 -ggdb' + end end create_makefile 'json/ext/parser' diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index ae8f3ab..8259d64 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -81,7 +81,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode, i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match, - i_match_string, i_aset, i_leftshift; + i_match_string, i_aset, i_aref, i_leftshift; #line 110 "parser.rl" @@ -445,7 +445,12 @@ case 26: if (cs >= JSON_object_first_final) { if (json->create_additions) { - VALUE klassname = rb_hash_aref(*result, json->create_id); + VALUE klassname; + if (NIL_P(json->object_class)) { + klassname = rb_hash_aref(*result, json->create_id); + } else { + klassname = rb_funcall(*result, i_aref, 1, json->create_id); + } if (!NIL_P(klassname)) { VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname); if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) { @@ -461,7 +466,7 @@ case 26: -#line 465 "parser.c" +#line 470 "parser.c" static const int JSON_value_start = 1; static const int JSON_value_first_final = 21; static const int JSON_value_error = 0; @@ -469,7 +474,7 @@ static const int JSON_value_error = 0; static const int JSON_value_en_main = 1; -#line 266 "parser.rl" +#line 271 "parser.rl" static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -477,14 +482,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 481 "parser.c" +#line 486 "parser.c" { cs = JSON_value_start; } -#line 273 "parser.rl" +#line 278 "parser.rl" -#line 488 "parser.c" +#line 493 "parser.c" { if ( p == pe ) goto _test_eof; @@ -509,14 +514,14 @@ st0: cs = 0; goto _out; tr0: -#line 214 "parser.rl" +#line 219 "parser.rl" { char *np = JSON_parse_string(json, p, pe, result); if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;} } goto st21; tr2: -#line 219 "parser.rl" +#line 224 "parser.rl" { char *np; if(pe > p + 9 - json->quirks_mode && !strncmp(MinusInfinity, p, 9)) { @@ -536,7 +541,7 @@ tr2: } goto st21; tr5: -#line 237 "parser.rl" +#line 242 "parser.rl" { char *np; json->current_nesting++; @@ -546,7 +551,7 @@ tr5: } goto st21; tr9: -#line 245 "parser.rl" +#line 250 "parser.rl" { char *np; json->current_nesting++; @@ -556,7 +561,7 @@ tr9: } goto st21; tr16: -#line 207 "parser.rl" +#line 212 "parser.rl" { if (json->allow_nan) { *result = CInfinity; @@ -566,7 +571,7 @@ tr16: } goto st21; tr18: -#line 200 "parser.rl" +#line 205 "parser.rl" { if (json->allow_nan) { *result = CNaN; @@ -576,19 +581,19 @@ tr18: } goto st21; tr22: -#line 194 "parser.rl" +#line 199 "parser.rl" { *result = Qfalse; } goto st21; tr25: -#line 191 "parser.rl" +#line 196 "parser.rl" { *result = Qnil; } goto st21; tr28: -#line 197 "parser.rl" +#line 202 "parser.rl" { *result = Qtrue; } @@ -597,9 +602,9 @@ st21: if ( ++p == pe ) goto _test_eof21; case 21: -#line 253 "parser.rl" +#line 258 "parser.rl" { p--; {p++; cs = 21; goto _out;} } -#line 603 "parser.c" +#line 608 "parser.c" goto st0; st2: if ( ++p == pe ) @@ -760,7 +765,7 @@ case 20: _out: {} } -#line 274 "parser.rl" +#line 279 "parser.rl" if (cs >= JSON_value_first_final) { return p; @@ -770,7 +775,7 @@ case 20: } -#line 774 "parser.c" +#line 779 "parser.c" static const int JSON_integer_start = 1; static const int JSON_integer_first_final = 3; static const int JSON_integer_error = 0; @@ -778,7 +783,7 @@ static const int JSON_integer_error = 0; static const int JSON_integer_en_main = 1; -#line 290 "parser.rl" +#line 295 "parser.rl" static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -786,15 +791,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res int cs = EVIL; -#line 790 "parser.c" +#line 795 "parser.c" { cs = JSON_integer_start; } -#line 297 "parser.rl" +#line 302 "parser.rl" json->memo = p; -#line 798 "parser.c" +#line 803 "parser.c" { if ( p == pe ) goto _test_eof; @@ -828,14 +833,14 @@ case 3: goto st0; goto tr4; tr4: -#line 287 "parser.rl" +#line 292 "parser.rl" { p--; {p++; cs = 4; goto _out;} } goto st4; st4: if ( ++p == pe ) goto _test_eof4; case 4: -#line 839 "parser.c" +#line 844 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -854,7 +859,7 @@ case 5: _out: {} } -#line 299 "parser.rl" +#line 304 "parser.rl" if (cs >= JSON_integer_first_final) { long len = p - json->memo; @@ -869,7 +874,7 @@ case 5: } -#line 873 "parser.c" +#line 878 "parser.c" static const int JSON_float_start = 1; static const int JSON_float_first_final = 8; static const int JSON_float_error = 0; @@ -877,7 +882,7 @@ static const int JSON_float_error = 0; static const int JSON_float_en_main = 1; -#line 324 "parser.rl" +#line 329 "parser.rl" static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -885,15 +890,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul int cs = EVIL; -#line 889 "parser.c" +#line 894 "parser.c" { cs = JSON_float_start; } -#line 331 "parser.rl" +#line 336 "parser.rl" json->memo = p; -#line 897 "parser.c" +#line 902 "parser.c" { if ( p == pe ) goto _test_eof; @@ -951,14 +956,14 @@ case 8: goto st0; goto tr9; tr9: -#line 318 "parser.rl" +#line 323 "parser.rl" { p--; {p++; cs = 9; goto _out;} } goto st9; st9: if ( ++p == pe ) goto _test_eof9; case 9: -#line 962 "parser.c" +#line 967 "parser.c" goto st0; st5: if ( ++p == pe ) @@ -1019,7 +1024,7 @@ case 7: _out: {} } -#line 333 "parser.rl" +#line 338 "parser.rl" if (cs >= JSON_float_first_final) { long len = p - json->memo; @@ -1035,7 +1040,7 @@ case 7: -#line 1039 "parser.c" +#line 1044 "parser.c" static const int JSON_array_start = 1; static const int JSON_array_first_final = 17; static const int JSON_array_error = 0; @@ -1043,7 +1048,7 @@ static const int JSON_array_error = 0; static const int JSON_array_en_main = 1; -#line 376 "parser.rl" +#line 381 "parser.rl" static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result) @@ -1057,14 +1062,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class); -#line 1061 "parser.c" +#line 1066 "parser.c" { cs = JSON_array_start; } -#line 389 "parser.rl" +#line 394 "parser.rl" -#line 1068 "parser.c" +#line 1073 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1103,7 +1108,7 @@ case 2: goto st2; goto st0; tr2: -#line 353 "parser.rl" +#line 358 "parser.rl" { VALUE v = Qnil; char *np = JSON_parse_value(json, p, pe, &v); @@ -1123,7 +1128,7 @@ st3: if ( ++p == pe ) goto _test_eof3; case 3: -#line 1127 "parser.c" +#line 1132 "parser.c" switch( (*p) ) { case 13: goto st3; case 32: goto st3; @@ -1223,14 +1228,14 @@ case 12: goto st3; goto st12; tr4: -#line 368 "parser.rl" +#line 373 "parser.rl" { p--; {p++; cs = 17; goto _out;} } goto st17; st17: if ( ++p == pe ) goto _test_eof17; case 17: -#line 1234 "parser.c" +#line 1239 "parser.c" goto st0; st13: if ( ++p == pe ) @@ -1286,7 +1291,7 @@ case 16: _out: {} } -#line 390 "parser.rl" +#line 395 "parser.rl" if(cs >= JSON_array_first_final) { return p + 1; @@ -1367,7 +1372,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd) } -#line 1371 "parser.c" +#line 1376 "parser.c" static const int JSON_string_start = 1; static const int JSON_string_first_final = 8; static const int JSON_string_error = 0; @@ -1375,7 +1380,7 @@ static const int JSON_string_error = 0; static const int JSON_string_en_main = 1; -#line 489 "parser.rl" +#line 494 "parser.rl" static int @@ -1397,15 +1402,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu *result = rb_str_buf_new(0); -#line 1401 "parser.c" +#line 1406 "parser.c" { cs = JSON_string_start; } -#line 510 "parser.rl" +#line 515 "parser.rl" json->memo = p; -#line 1409 "parser.c" +#line 1414 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1430,7 +1435,7 @@ case 2: goto st0; goto st2; tr2: -#line 475 "parser.rl" +#line 480 "parser.rl" { *result = json_string_unescape(*result, json->memo + 1, p); if (NIL_P(*result)) { @@ -1441,14 +1446,14 @@ tr2: {p = (( p + 1))-1;} } } -#line 486 "parser.rl" +#line 491 "parser.rl" { p--; {p++; cs = 8; goto _out;} } goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: -#line 1452 "parser.c" +#line 1457 "parser.c" goto st0; st3: if ( ++p == pe ) @@ -1524,7 +1529,7 @@ case 7: _out: {} } -#line 512 "parser.rl" +#line 517 "parser.rl" if (json->create_additions && RTEST(match_string = json->match_string)) { VALUE klass; @@ -1733,7 +1738,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1737 "parser.c" +#line 1742 "parser.c" static const int JSON_start = 1; static const int JSON_first_final = 10; static const int JSON_error = 0; @@ -1741,7 +1746,7 @@ static const int JSON_error = 0; static const int JSON_en_main = 1; -#line 744 "parser.rl" +#line 749 "parser.rl" static VALUE cParser_parse_strict(VALUE self) @@ -1752,16 +1757,16 @@ static VALUE cParser_parse_strict(VALUE self) GET_PARSER; -#line 1756 "parser.c" +#line 1761 "parser.c" { cs = JSON_start; } -#line 754 "parser.rl" +#line 759 "parser.rl" p = json->source; pe = p + json->len; -#line 1765 "parser.c" +#line 1770 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1817,7 +1822,7 @@ case 5: goto st1; goto st5; tr3: -#line 733 "parser.rl" +#line 738 "parser.rl" { char *np; json->current_nesting = 1; @@ -1826,7 +1831,7 @@ tr3: } goto st10; tr4: -#line 726 "parser.rl" +#line 731 "parser.rl" { char *np; json->current_nesting = 1; @@ -1838,7 +1843,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1842 "parser.c" +#line 1847 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1895,7 +1900,7 @@ case 9: _out: {} } -#line 757 "parser.rl" +#line 762 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -1907,7 +1912,7 @@ case 9: -#line 1911 "parser.c" +#line 1916 "parser.c" static const int JSON_quirks_mode_start = 1; static const int JSON_quirks_mode_first_final = 10; static const int JSON_quirks_mode_error = 0; @@ -1915,7 +1920,7 @@ static const int JSON_quirks_mode_error = 0; static const int JSON_quirks_mode_en_main = 1; -#line 782 "parser.rl" +#line 787 "parser.rl" static VALUE cParser_parse_quirks_mode(VALUE self) @@ -1926,16 +1931,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self) GET_PARSER; -#line 1930 "parser.c" +#line 1935 "parser.c" { cs = JSON_quirks_mode_start; } -#line 792 "parser.rl" +#line 797 "parser.rl" p = json->source; pe = p + json->len; -#line 1939 "parser.c" +#line 1944 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1969,7 +1974,7 @@ st0: cs = 0; goto _out; tr2: -#line 774 "parser.rl" +#line 779 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1979,7 +1984,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1983 "parser.c" +#line 1988 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -2068,7 +2073,7 @@ case 9: _out: {} } -#line 795 "parser.rl" +#line 800 "parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; @@ -2189,6 +2194,7 @@ void Init_parser() i_key_p = rb_intern("key?"); i_deep_const_get = rb_intern("deep_const_get"); i_aset = rb_intern("[]="); + i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index 9e7b7da..9f9056b 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode, i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match, - i_match_string, i_aset, i_leftshift; + i_match_string, i_aset, i_aref, i_leftshift; %%{ machine JSON_common; @@ -167,7 +167,12 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu if (cs >= JSON_object_first_final) { if (json->create_additions) { - VALUE klassname = rb_hash_aref(*result, json->create_id); + VALUE klassname; + if (NIL_P(json->object_class)) { + klassname = rb_hash_aref(*result, json->create_id); + } else { + klassname = rb_funcall(*result, i_aref, 1, json->create_id); + } if (!NIL_P(klassname)) { VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname); if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) { @@ -912,6 +917,7 @@ void Init_parser() i_key_p = rb_intern("key?"); i_deep_const_get = rb_intern("deep_const_get"); i_aset = rb_intern("[]="); + i_aref = rb_intern("[]"); i_leftshift = rb_intern("<<"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); diff --git a/java/lib/bytelist-1.0.6.jar b/java/lib/bytelist-1.0.6.jar Binary files differdeleted file mode 100644 index 7918e7c..0000000 --- a/java/lib/bytelist-1.0.6.jar +++ /dev/null diff --git a/java/lib/jcodings.jar b/java/lib/jcodings.jar Binary files differdeleted file mode 100644 index e33fc99..0000000 --- a/java/lib/jcodings.jar +++ /dev/null diff --git a/java/src/json/ext/OptionsReader.java b/java/src/json/ext/OptionsReader.java index a0b76b1..6835735 100644 --- a/java/src/json/ext/OptionsReader.java +++ b/java/src/json/ext/OptionsReader.java @@ -102,13 +102,7 @@ final class OptionsReader { IRubyObject value = get(key); if (value == null || value.isNil()) return defaultValue; - - if (value instanceof RubyClass && - ((RubyClass)value).getAllocator() == defaultValue.getAllocator()) { - return (RubyClass)value; - } - throw runtime.newTypeError(key + " option must be a subclass of " - + defaultValue); + return (RubyClass)value; } public RubyHash getHash(String key) { diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java index 0b59c8f..0058f95 100644 --- a/java/src/json/ext/Parser.java +++ b/java/src/json/ext/Parser.java @@ -28,6 +28,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; +import org.jruby.util.ConvertBytes; +import static org.jruby.util.ConvertDouble.DoubleConverter; /** * The <code>JSON::Ext::Parser</code> class. @@ -58,7 +60,7 @@ public class Parser extends RubyObject { private static final int DEFAULT_MAX_NESTING = 19; - private static final String JSON_MINUS_INFINITY = "-Infinity"; + private static final ByteList JSON_MINUS_INFINITY = new ByteList(ByteList.plain("-Infinity")); // constant names in the JSON module containing those values private static final String CONST_NAN = "NaN"; private static final String CONST_INFINITY = "Infinity"; @@ -82,13 +84,13 @@ public class Parser extends RubyObject { * The result of the successful parsing. Should never be * <code>null</code>. */ - final IRubyObject result; + IRubyObject result; /** * The point where the parser returned. */ - final int p; + int p; - ParserResult(IRubyObject result, int p) { + void update(IRubyObject result, int p) { this.result = result; this.p = p; } @@ -303,9 +305,11 @@ public class Parser extends RubyObject { private final Parser parser; private final ThreadContext context; private final ByteList byteList; + private final ByteList view; private final byte[] data; private final StringDecoder decoder; private int currentNesting = 0; + private final DoubleConverter dc; // initialization value for all state variables. // no idea about the origins of this value, ask Flori ;) @@ -316,7 +320,9 @@ public class Parser extends RubyObject { this.context = context; this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); + this.view = new ByteList(data, false); this.decoder = new StringDecoder(context); + this.dc = new DoubleConverter(); } private RaiseException unexpectedToken(int absStart, int absEnd) { @@ -331,11 +337,11 @@ public class Parser extends RubyObject { } -// line 357 "Parser.rl" +// line 363 "Parser.rl" -// line 339 "Parser.java" +// line 345 "Parser.java" private static byte[] init__JSON_value_actions_0() { return new byte [] { @@ -449,22 +455,22 @@ static final int JSON_value_error = 0; static final int JSON_value_en_main = 1; -// line 463 "Parser.rl" +// line 469 "Parser.rl" - ParserResult parseValue(int p, int pe) { + void parseValue(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; -// line 461 "Parser.java" +// line 467 "Parser.java" { cs = JSON_value_start; } -// line 470 "Parser.rl" +// line 476 "Parser.rl" -// line 468 "Parser.java" +// line 474 "Parser.java" { int _klen; int _trans = 0; @@ -490,13 +496,13 @@ case 1: while ( _nacts-- > 0 ) { switch ( _JSON_value_actions[_acts++] ) { case 9: -// line 448 "Parser.rl" +// line 454 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 500 "Parser.java" +// line 506 "Parser.java" } } @@ -559,25 +565,25 @@ case 1: switch ( _JSON_value_actions[_acts++] ) { case 0: -// line 365 "Parser.rl" +// line 371 "Parser.rl" { result = getRuntime().getNil(); } break; case 1: -// line 368 "Parser.rl" +// line 374 "Parser.rl" { result = getRuntime().getFalse(); } break; case 2: -// line 371 "Parser.rl" +// line 377 "Parser.rl" { result = getRuntime().getTrue(); } break; case 3: -// line 374 "Parser.rl" +// line 380 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_NAN); @@ -587,7 +593,7 @@ case 1: } break; case 4: -// line 381 "Parser.rl" +// line 387 "Parser.rl" { if (parser.allowNaN) { result = getConstant(CONST_INFINITY); @@ -597,10 +603,10 @@ case 1: } break; case 5: -// line 388 "Parser.rl" +// line 394 "Parser.rl" { if (pe > p + 9 - (parser.quirksMode ? 1 : 0) && - absSubSequence(p, p + 9).toString().equals(JSON_MINUS_INFINITY)) { + absSubSequence(p, p + 9).equals(JSON_MINUS_INFINITY)) { if (parser.allowNaN) { result = getConstant(CONST_MINUS_INFINITY); @@ -611,13 +617,13 @@ case 1: throw unexpectedToken(p, pe); } } - ParserResult res = parseFloat(p, pe); - if (res != null) { + parseFloat(res, p, pe); + if (res.result != null) { result = res.result; {p = (( res.p))-1;} } - res = parseInteger(p, pe); - if (res != null) { + parseInteger(res, p, pe); + if (res.result != null) { result = res.result; {p = (( res.p))-1;} } @@ -626,10 +632,10 @@ case 1: } break; case 6: -// line 414 "Parser.rl" +// line 420 "Parser.rl" { - ParserResult res = parseString(p, pe); - if (res == null) { + parseString(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -639,12 +645,12 @@ case 1: } break; case 7: -// line 424 "Parser.rl" +// line 430 "Parser.rl" { currentNesting++; - ParserResult res = parseArray(p, pe); + parseArray(res, p, pe); currentNesting--; - if (res == null) { + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -654,12 +660,12 @@ case 1: } break; case 8: -// line 436 "Parser.rl" +// line 442 "Parser.rl" { currentNesting++; - ParserResult res = parseObject(p, pe); + parseObject(res, p, pe); currentNesting--; - if (res == null) { + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -668,7 +674,7 @@ case 1: } } break; -// line 672 "Parser.java" +// line 678 "Parser.java" } } } @@ -688,17 +694,17 @@ case 5: break; } } -// line 471 "Parser.rl" +// line 477 "Parser.rl" if (cs >= JSON_value_first_final && result != null) { - return new ParserResult(result, p); + res.update(result, p); } else { - return null; + res.update(null, p); } } -// line 702 "Parser.java" +// line 708 "Parser.java" private static byte[] init__JSON_integer_actions_0() { return new byte [] { @@ -797,22 +803,33 @@ static final int JSON_integer_error = 0; static final int JSON_integer_en_main = 1; -// line 490 "Parser.rl" +// line 496 "Parser.rl" - ParserResult parseInteger(int p, int pe) { + void parseInteger(ParserResult res, int p, int pe) { + int new_p = parseIntegerInternal(p, pe); + if (new_p == -1) { + res.update(null, p); + return; + } + RubyInteger number = createInteger(p, new_p); + res.update(number, new_p + 1); + return; + } + + int parseIntegerInternal(int p, int pe) { int cs = EVIL; -// line 808 "Parser.java" +// line 825 "Parser.java" { cs = JSON_integer_start; } -// line 496 "Parser.rl" +// line 513 "Parser.rl" int memo = p; -// line 816 "Parser.java" +// line 833 "Parser.java" { int _klen; int _trans = 0; @@ -893,13 +910,13 @@ case 1: switch ( _JSON_integer_actions[_acts++] ) { case 0: -// line 484 "Parser.rl" +// line 490 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 903 "Parser.java" +// line 920 "Parser.java" } } } @@ -919,22 +936,29 @@ case 5: break; } } -// line 498 "Parser.rl" +// line 515 "Parser.rl" if (cs < JSON_integer_first_final) { - return null; + return -1; } - ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyInteger number = RubyNumeric.str2inum(getRuntime(), expr, 10, true); - return new ParserResult(number, p + 1); + return p; + } + + RubyInteger createInteger(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return bytesToInum(runtime, num); + } + + RubyInteger bytesToInum(Ruby runtime, ByteList num) { + return runtime.is1_9() ? + ConvertBytes.byteListToInum19(runtime, num, 10, true) : + ConvertBytes.byteListToInum(runtime, num, 10, true); } -// line 938 "Parser.java" +// line 962 "Parser.java" private static byte[] init__JSON_float_actions_0() { return new byte [] { @@ -1036,22 +1060,33 @@ static final int JSON_float_error = 0; static final int JSON_float_en_main = 1; -// line 526 "Parser.rl" +// line 550 "Parser.rl" - ParserResult parseFloat(int p, int pe) { + void parseFloat(ParserResult res, int p, int pe) { + int new_p = parseFloatInternal(p, pe); + if (new_p == -1) { + res.update(null, p); + return; + } + RubyFloat number = createFloat(p, new_p); + res.update(number, new_p + 1); + return; + } + + int parseFloatInternal(int p, int pe) { int cs = EVIL; -// line 1047 "Parser.java" +// line 1082 "Parser.java" { cs = JSON_float_start; } -// line 532 "Parser.rl" +// line 567 "Parser.rl" int memo = p; -// line 1055 "Parser.java" +// line 1090 "Parser.java" { int _klen; int _trans = 0; @@ -1132,13 +1167,13 @@ case 1: switch ( _JSON_float_actions[_acts++] ) { case 0: -// line 517 "Parser.rl" +// line 541 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1142 "Parser.java" +// line 1177 "Parser.java" } } } @@ -1158,22 +1193,23 @@ case 5: break; } } -// line 534 "Parser.rl" +// line 569 "Parser.rl" if (cs < JSON_float_first_final) { - return null; + return -1; } - - ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyFloat number = RubyNumeric.str2fnum(getRuntime(), expr, true); - return new ParserResult(number, p + 1); + + return p; + } + + RubyFloat createFloat(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return RubyFloat.newFloat(runtime, dc.parse(num, true, runtime.is1_9())); } -// line 1177 "Parser.java" +// line 1213 "Parser.java" private static byte[] init__JSON_string_actions_0() { return new byte [] { @@ -1275,23 +1311,23 @@ static final int JSON_string_error = 0; static final int JSON_string_en_main = 1; -// line 578 "Parser.rl" +// line 614 "Parser.rl" - ParserResult parseString(int p, int pe) { + void parseString(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; -// line 1287 "Parser.java" +// line 1323 "Parser.java" { cs = JSON_string_start; } -// line 585 "Parser.rl" +// line 621 "Parser.rl" int memo = p; -// line 1295 "Parser.java" +// line 1331 "Parser.java" { int _klen; int _trans = 0; @@ -1372,7 +1408,7 @@ case 1: switch ( _JSON_string_actions[_acts++] ) { case 0: -// line 553 "Parser.rl" +// line 589 "Parser.rl" { int offset = byteList.begin(); ByteList decoded = decoder.decode(byteList, memo + 1 - offset, @@ -1387,13 +1423,13 @@ case 1: } break; case 1: -// line 566 "Parser.rl" +// line 602 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1397 "Parser.java" +// line 1433 "Parser.java" } } } @@ -1413,7 +1449,7 @@ case 5: break; } } -// line 587 "Parser.rl" +// line 623 "Parser.rl" if (parser.createAdditions) { RubyHash match_string = parser.match_string; @@ -1441,14 +1477,14 @@ case 5: } if (cs >= JSON_string_first_final && result != null) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { - return null; + res.update(null, p + 1); } } -// line 1452 "Parser.java" +// line 1488 "Parser.java" private static byte[] init__JSON_array_actions_0() { return new byte [] { @@ -1561,10 +1597,10 @@ static final int JSON_array_error = 0; static final int JSON_array_en_main = 1; -// line 657 "Parser.rl" +// line 693 "Parser.rl" - ParserResult parseArray(int p, int pe) { + void parseArray(ParserResult res, int p, int pe) { int cs = EVIL; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { @@ -1572,21 +1608,23 @@ static final int JSON_array_en_main = 1; "nesting of " + currentNesting + " is too deep"); } - // this is guaranteed to be a RubyArray due to the earlier - // allocator test at OptionsReader#getClass - RubyArray result = - (RubyArray)parser.arrayClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + IRubyObject result; + if (parser.arrayClass == getRuntime().getArray()) { + result = RubyArray.newArray(getRuntime()); + } else { + result = parser.arrayClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } -// line 1583 "Parser.java" +// line 1623 "Parser.java" { cs = JSON_array_start; } -// line 674 "Parser.rl" +// line 714 "Parser.rl" -// line 1590 "Parser.java" +// line 1630 "Parser.java" { int _klen; int _trans = 0; @@ -1667,30 +1705,30 @@ case 1: switch ( _JSON_array_actions[_acts++] ) { case 0: -// line 626 "Parser.rl" +// line 662 "Parser.rl" { - ParserResult res = parseValue(p, pe); - if (res == null) { + parseValue(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - if (!parser.arrayClass.getName().equals("Array")) { - result.callMethod(context, "<<", res.result); + if (parser.arrayClass == getRuntime().getArray()) { + ((RubyArray)result).append(res.result); } else { - result.append(res.result); + result.callMethod(context, "<<", res.result); } {p = (( res.p))-1;} } } break; case 1: -// line 641 "Parser.rl" +// line 677 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1694 "Parser.java" +// line 1734 "Parser.java" } } } @@ -1710,17 +1748,17 @@ case 5: break; } } -// line 675 "Parser.rl" +// line 715 "Parser.rl" if (cs >= JSON_array_first_final) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { throw unexpectedToken(p, pe); } } -// line 1724 "Parser.java" +// line 1764 "Parser.java" private static byte[] init__JSON_object_actions_0() { return new byte [] { @@ -1843,12 +1881,13 @@ static final int JSON_object_error = 0; static final int JSON_object_en_main = 1; -// line 734 "Parser.rl" +// line 774 "Parser.rl" - ParserResult parseObject(int p, int pe) { + void parseObject(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject lastName = null; + boolean objectDefault = true; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { throw newException(Utils.M_NESTING_ERROR, @@ -1857,19 +1896,24 @@ static final int JSON_object_en_main = 1; // this is guaranteed to be a RubyHash due to the earlier // allocator test at OptionsReader#getClass - RubyHash result = - (RubyHash)parser.objectClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + IRubyObject result; + if (parser.objectClass == getRuntime().getHash()) { + result = RubyHash.newHash(getRuntime()); + } else { + objectDefault = false; + result = parser.objectClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } -// line 1866 "Parser.java" +// line 1912 "Parser.java" { cs = JSON_object_start; } -// line 752 "Parser.rl" +// line 798 "Parser.rl" -// line 1873 "Parser.java" +// line 1919 "Parser.java" { int _klen; int _trans = 0; @@ -1950,27 +1994,27 @@ case 1: switch ( _JSON_object_actions[_acts++] ) { case 0: -// line 689 "Parser.rl" +// line 729 "Parser.rl" { - ParserResult res = parseValue(p, pe); - if (res == null) { + parseValue(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { - if (!parser.objectClass.getName().equals("Hash")) { - result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); + if (parser.objectClass == getRuntime().getHash()) { + ((RubyHash)result).op_aset(context, lastName, res.result); } else { - result.op_aset(context, lastName, res.result); + result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); } {p = (( res.p))-1;} } } break; case 1: -// line 704 "Parser.rl" +// line 744 "Parser.rl" { - ParserResult res = parseString(p, pe); - if (res == null) { + parseString(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -1987,13 +2031,13 @@ case 1: } break; case 2: -// line 722 "Parser.rl" +// line 762 "Parser.rl" { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } break; -// line 1997 "Parser.java" +// line 2043 "Parser.java" } } } @@ -2013,17 +2057,24 @@ case 5: break; } } -// line 753 "Parser.rl" +// line 799 "Parser.rl" if (cs < JSON_object_first_final) { - return null; + res.update(null, p + 1); + return; } IRubyObject returnedResult = result; // attempt to de-serialize object if (parser.createAdditions) { - IRubyObject vKlassName = result.op_aref(context, parser.createId); + IRubyObject vKlassName; + if (objectDefault) { + vKlassName = ((RubyHash)result).op_aref(context, parser.createId); + } else { + vKlassName = result.callMethod(context, "[]", parser.createId); + } + if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate IRubyObject klass = parser.info.jsonModule.get(). @@ -2035,11 +2086,11 @@ case 5: } } } - return new ParserResult(returnedResult, p + 1); + res.update(returnedResult, p + 1); } -// line 2043 "Parser.java" +// line 2096 "Parser.java" private static byte[] init__JSON_actions_0() { return new byte [] { @@ -2143,25 +2194,26 @@ static final int JSON_error = 0; static final int JSON_en_main = 1; -// line 811 "Parser.rl" +// line 864 "Parser.rl" public IRubyObject parseStrict() { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); -// line 2156 "Parser.java" +// line 2210 "Parser.java" { cs = JSON_start; } -// line 819 "Parser.rl" +// line 873 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2165 "Parser.java" +// line 2219 "Parser.java" { int _klen; int _trans = 0; @@ -2242,11 +2294,11 @@ case 1: switch ( _JSON_actions[_acts++] ) { case 0: -// line 783 "Parser.rl" +// line 836 "Parser.rl" { currentNesting = 1; - ParserResult res = parseObject(p, pe); - if (res == null) { + parseObject(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2256,11 +2308,11 @@ case 1: } break; case 1: -// line 795 "Parser.rl" +// line 848 "Parser.rl" { currentNesting = 1; - ParserResult res = parseArray(p, pe); - if (res == null) { + parseArray(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2269,7 +2321,7 @@ case 1: } } break; -// line 2273 "Parser.java" +// line 2327 "Parser.java" } } } @@ -2289,7 +2341,7 @@ case 5: break; } } -// line 822 "Parser.rl" +// line 876 "Parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -2299,7 +2351,7 @@ case 5: } -// line 2303 "Parser.java" +// line 2357 "Parser.java" private static byte[] init__JSON_quirks_mode_actions_0() { return new byte [] { @@ -2402,25 +2454,26 @@ static final int JSON_quirks_mode_error = 0; static final int JSON_quirks_mode_en_main = 1; -// line 850 "Parser.rl" +// line 904 "Parser.rl" public IRubyObject parseQuirksMode() { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); -// line 2415 "Parser.java" +// line 2470 "Parser.java" { cs = JSON_quirks_mode_start; } -// line 858 "Parser.rl" +// line 913 "Parser.rl" p = byteList.begin(); pe = p + byteList.length(); -// line 2424 "Parser.java" +// line 2479 "Parser.java" { int _klen; int _trans = 0; @@ -2501,10 +2554,10 @@ case 1: switch ( _JSON_quirks_mode_actions[_acts++] ) { case 0: -// line 836 "Parser.rl" +// line 890 "Parser.rl" { - ParserResult res = parseValue(p, pe); - if (res == null) { + parseValue(res, p, pe); + if (res.result == null) { p--; { p += 1; _goto_targ = 5; if (true) continue _goto;} } else { @@ -2513,7 +2566,7 @@ case 1: } } break; -// line 2517 "Parser.java" +// line 2572 "Parser.java" } } } @@ -2533,7 +2586,7 @@ case 5: break; } } -// line 861 "Parser.rl" +// line 916 "Parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; @@ -2552,16 +2605,14 @@ case 5: } /** - * Returns a subsequence of the source ByteList, based on source - * array byte offsets (i.e., the ByteList's own begin offset is not - * automatically added). + * Updates the "view" bytelist with the new offsets and returns it. * @param start * @param end */ private ByteList absSubSequence(int absStart, int absEnd) { - int offset = byteList.begin(); - return (ByteList)byteList.subSequence(absStart - offset, - absEnd - offset); + view.setBegin(absStart); + view.setRealSize(absEnd - absStart); + return view; } /** diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl index e8cd874..6d9d4f9 100644 --- a/java/src/json/ext/Parser.rl +++ b/java/src/json/ext/Parser.rl @@ -26,6 +26,8 @@ import org.jruby.runtime.ThreadContext; import org.jruby.runtime.Visibility; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; +import org.jruby.util.ConvertBytes; +import static org.jruby.util.ConvertDouble.DoubleConverter; /** * The <code>JSON::Ext::Parser</code> class. @@ -56,7 +58,7 @@ public class Parser extends RubyObject { private static final int DEFAULT_MAX_NESTING = 19; - private static final String JSON_MINUS_INFINITY = "-Infinity"; + private static final ByteList JSON_MINUS_INFINITY = new ByteList(ByteList.plain("-Infinity")); // constant names in the JSON module containing those values private static final String CONST_NAN = "NaN"; private static final String CONST_INFINITY = "Infinity"; @@ -80,13 +82,13 @@ public class Parser extends RubyObject { * The result of the successful parsing. Should never be * <code>null</code>. */ - final IRubyObject result; + IRubyObject result; /** * The point where the parser returned. */ - final int p; + int p; - ParserResult(IRubyObject result, int p) { + void update(IRubyObject result, int p) { this.result = result; this.p = p; } @@ -301,9 +303,11 @@ public class Parser extends RubyObject { private final Parser parser; private final ThreadContext context; private final ByteList byteList; + private final ByteList view; private final byte[] data; private final StringDecoder decoder; private int currentNesting = 0; + private final DoubleConverter dc; // initialization value for all state variables. // no idea about the origins of this value, ask Flori ;) @@ -314,7 +318,9 @@ public class Parser extends RubyObject { this.context = context; this.byteList = parser.checkAndGetSource().getByteList(); this.data = byteList.unsafeBytes(); + this.view = new ByteList(data, false); this.decoder = new StringDecoder(context); + this.dc = new DoubleConverter(); } private RaiseException unexpectedToken(int absStart, int absEnd) { @@ -387,7 +393,7 @@ public class Parser extends RubyObject { } action parse_number { if (pe > fpc + 9 - (parser.quirksMode ? 1 : 0) && - absSubSequence(fpc, fpc + 9).toString().equals(JSON_MINUS_INFINITY)) { + absSubSequence(fpc, fpc + 9).equals(JSON_MINUS_INFINITY)) { if (parser.allowNaN) { result = getConstant(CONST_MINUS_INFINITY); @@ -398,13 +404,13 @@ public class Parser extends RubyObject { throw unexpectedToken(p, pe); } } - ParserResult res = parseFloat(fpc, pe); - if (res != null) { + parseFloat(res, fpc, pe); + if (res.result != null) { result = res.result; fexec res.p; } - res = parseInteger(fpc, pe); - if (res != null) { + parseInteger(res, fpc, pe); + if (res.result != null) { result = res.result; fexec res.p; } @@ -412,8 +418,8 @@ public class Parser extends RubyObject { fbreak; } action parse_string { - ParserResult res = parseString(fpc, pe); - if (res == null) { + parseString(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -423,9 +429,9 @@ public class Parser extends RubyObject { } action parse_array { currentNesting++; - ParserResult res = parseArray(fpc, pe); + parseArray(res, fpc, pe); currentNesting--; - if (res == null) { + if (res.result == null) { fhold; fbreak; } else { @@ -435,9 +441,9 @@ public class Parser extends RubyObject { } action parse_object { currentNesting++; - ParserResult res = parseObject(fpc, pe); + parseObject(res, fpc, pe); currentNesting--; - if (res == null) { + if (res.result == null) { fhold; fbreak; } else { @@ -462,7 +468,7 @@ public class Parser extends RubyObject { ) %*exit; }%% - ParserResult parseValue(int p, int pe) { + void parseValue(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; @@ -470,9 +476,9 @@ public class Parser extends RubyObject { %% write exec; if (cs >= JSON_value_first_final && result != null) { - return new ParserResult(result, p); + res.update(result, p); } else { - return null; + res.update(null, p); } } @@ -489,7 +495,18 @@ public class Parser extends RubyObject { main := '-'? ( '0' | [1-9][0-9]* ) ( ^[0-9]? @exit ); }%% - ParserResult parseInteger(int p, int pe) { + void parseInteger(ParserResult res, int p, int pe) { + int new_p = parseIntegerInternal(p, pe); + if (new_p == -1) { + res.update(null, p); + return; + } + RubyInteger number = createInteger(p, new_p); + res.update(number, new_p + 1); + return; + } + + int parseIntegerInternal(int p, int pe) { int cs = EVIL; %% write init; @@ -497,15 +514,22 @@ public class Parser extends RubyObject { %% write exec; if (cs < JSON_integer_first_final) { - return null; + return -1; } - ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyInteger number = RubyNumeric.str2inum(getRuntime(), expr, 10, true); - return new ParserResult(number, p + 1); + return p; + } + + RubyInteger createInteger(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return bytesToInum(runtime, num); + } + + RubyInteger bytesToInum(Ruby runtime, ByteList num) { + return runtime.is1_9() ? + ConvertBytes.byteListToInum19(runtime, num, 10, true) : + ConvertBytes.byteListToInum(runtime, num, 10, true); } %%{ @@ -525,7 +549,18 @@ public class Parser extends RubyObject { ( ^[0-9Ee.\-]? @exit ); }%% - ParserResult parseFloat(int p, int pe) { + void parseFloat(ParserResult res, int p, int pe) { + int new_p = parseFloatInternal(p, pe); + if (new_p == -1) { + res.update(null, p); + return; + } + RubyFloat number = createFloat(p, new_p); + res.update(number, new_p + 1); + return; + } + + int parseFloatInternal(int p, int pe) { int cs = EVIL; %% write init; @@ -533,15 +568,16 @@ public class Parser extends RubyObject { %% write exec; if (cs < JSON_float_first_final) { - return null; + return -1; } - - ByteList num = absSubSequence(memo, p); - // note: this is actually a shared string, but since it is temporary and - // read-only, it doesn't really matter - RubyString expr = RubyString.newStringLight(getRuntime(), num); - RubyFloat number = RubyNumeric.str2fnum(getRuntime(), expr, true); - return new ParserResult(number, p + 1); + + return p; + } + + RubyFloat createFloat(int p, int new_p) { + Ruby runtime = getRuntime(); + ByteList num = absSubSequence(p, new_p); + return RubyFloat.newFloat(runtime, dc.parse(num, true, runtime.is1_9())); } %%{ @@ -577,7 +613,7 @@ public class Parser extends RubyObject { ) '"' @exit; }%% - ParserResult parseString(int p, int pe) { + void parseString(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject result = null; @@ -611,9 +647,9 @@ public class Parser extends RubyObject { } if (cs >= JSON_string_first_final && result != null) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { - return null; + res.update(null, p + 1); } } @@ -624,15 +660,15 @@ public class Parser extends RubyObject { write data; action parse_value { - ParserResult res = parseValue(fpc, pe); - if (res == null) { + parseValue(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { - if (!parser.arrayClass.getName().equals("Array")) { - result.callMethod(context, "<<", res.result); + if (parser.arrayClass == getRuntime().getArray()) { + ((RubyArray)result).append(res.result); } else { - result.append(res.result); + result.callMethod(context, "<<", res.result); } fexec res.p; } @@ -656,7 +692,7 @@ public class Parser extends RubyObject { end_array @exit; }%% - ParserResult parseArray(int p, int pe) { + void parseArray(ParserResult res, int p, int pe) { int cs = EVIL; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { @@ -664,17 +700,19 @@ public class Parser extends RubyObject { "nesting of " + currentNesting + " is too deep"); } - // this is guaranteed to be a RubyArray due to the earlier - // allocator test at OptionsReader#getClass - RubyArray result = - (RubyArray)parser.arrayClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + IRubyObject result; + if (parser.arrayClass == getRuntime().getArray()) { + result = RubyArray.newArray(getRuntime()); + } else { + result = parser.arrayClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } %% write init; %% write exec; if (cs >= JSON_array_first_final) { - return new ParserResult(result, p + 1); + res.update(result, p + 1); } else { throw unexpectedToken(p, pe); } @@ -687,23 +725,23 @@ public class Parser extends RubyObject { write data; action parse_value { - ParserResult res = parseValue(fpc, pe); - if (res == null) { + parseValue(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { - if (!parser.objectClass.getName().equals("Hash")) { - result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); + if (parser.objectClass == getRuntime().getHash()) { + ((RubyHash)result).op_aset(context, lastName, res.result); } else { - result.op_aset(context, lastName, res.result); + result.callMethod(context, "[]=", new IRubyObject[] { lastName, res.result }); } fexec res.p; } } action parse_name { - ParserResult res = parseString(fpc, pe); - if (res == null) { + parseString(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -733,9 +771,10 @@ public class Parser extends RubyObject { ) @exit; }%% - ParserResult parseObject(int p, int pe) { + void parseObject(ParserResult res, int p, int pe) { int cs = EVIL; IRubyObject lastName = null; + boolean objectDefault = true; if (parser.maxNesting > 0 && currentNesting > parser.maxNesting) { throw newException(Utils.M_NESTING_ERROR, @@ -744,22 +783,34 @@ public class Parser extends RubyObject { // this is guaranteed to be a RubyHash due to the earlier // allocator test at OptionsReader#getClass - RubyHash result = - (RubyHash)parser.objectClass.newInstance(context, - IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + IRubyObject result; + if (parser.objectClass == getRuntime().getHash()) { + result = RubyHash.newHash(getRuntime()); + } else { + objectDefault = false; + result = parser.objectClass.newInstance(context, + IRubyObject.NULL_ARRAY, Block.NULL_BLOCK); + } %% write init; %% write exec; if (cs < JSON_object_first_final) { - return null; + res.update(null, p + 1); + return; } IRubyObject returnedResult = result; // attempt to de-serialize object if (parser.createAdditions) { - IRubyObject vKlassName = result.op_aref(context, parser.createId); + IRubyObject vKlassName; + if (objectDefault) { + vKlassName = ((RubyHash)result).op_aref(context, parser.createId); + } else { + vKlassName = result.callMethod(context, "[]", parser.createId); + } + if (!vKlassName.isNil()) { // might throw ArgumentError, we let it propagate IRubyObject klass = parser.info.jsonModule.get(). @@ -771,7 +822,7 @@ public class Parser extends RubyObject { } } } - return new ParserResult(returnedResult, p + 1); + res.update(returnedResult, p + 1); } %%{ @@ -782,8 +833,8 @@ public class Parser extends RubyObject { action parse_object { currentNesting = 1; - ParserResult res = parseObject(fpc, pe); - if (res == null) { + parseObject(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -794,8 +845,8 @@ public class Parser extends RubyObject { action parse_array { currentNesting = 1; - ParserResult res = parseArray(fpc, pe); - if (res == null) { + parseArray(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -814,6 +865,7 @@ public class Parser extends RubyObject { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); %% write init; p = byteList.begin(); @@ -834,8 +886,8 @@ public class Parser extends RubyObject { write data; action parse_value { - ParserResult res = parseValue(fpc, pe); - if (res == null) { + parseValue(res, fpc, pe); + if (res.result == null) { fhold; fbreak; } else { @@ -853,6 +905,7 @@ public class Parser extends RubyObject { int cs = EVIL; int p, pe; IRubyObject result = null; + ParserResult res = new ParserResult(); %% write init; p = byteList.begin(); @@ -876,16 +929,14 @@ public class Parser extends RubyObject { } /** - * Returns a subsequence of the source ByteList, based on source - * array byte offsets (i.e., the ByteList's own begin offset is not - * automatically added). + * Updates the "view" bytelist with the new offsets and returns it. * @param start * @param end */ private ByteList absSubSequence(int absStart, int absEnd) { - int offset = byteList.begin(); - return (ByteList)byteList.subSequence(absStart - offset, - absEnd - offset); + view.setBegin(absStart); + view.setRealSize(absEnd - absStart); + return view; } /** diff --git a/tests/test_json.rb b/tests/test_json.rb index c308933..c06fd19 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -5,6 +5,7 @@ require 'test/unit' require File.join(File.dirname(__FILE__), 'setup_variant') require 'stringio' require 'tempfile' +require 'ostruct' unless Array.method_defined?(:permutation) begin @@ -218,13 +219,41 @@ class TC_JSON < Test::Unit::TestCase end end - def test_parse_array_custom_class + class SubArrayWrapper + def initialize + @data = [] + end + + attr_reader :data + + def [](index) + @data[index] + end + + def <<(value) + @data << value + @shifted = true + end + + def shifted? + @shifted + end + end + + def test_parse_array_custom_array_derived_class res = parse('[1,2]', :array_class => SubArray) assert_equal([1,2], res) assert_equal(SubArray, res.class) assert res.shifted? end + def test_parse_array_custom_non_array_derived_class + res = parse('[1,2]', :array_class => SubArrayWrapper) + assert_equal([1,2], res.data) + assert_equal(SubArrayWrapper, res.class) + assert res.shifted? + end + def test_parse_object assert_equal({}, parse('{}')) assert_equal({}, parse(' { } ')) @@ -256,13 +285,35 @@ class TC_JSON < Test::Unit::TestCase end end - def test_parse_object_custom_class + class SubOpenStruct < OpenStruct + def [](k) + __send__(k) + end + + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + end + + def item_set? + @item_set + end + end + + def test_parse_object_custom_hash_derived_class res = parse('{"foo":"bar"}', :object_class => SubHash) assert_equal({"foo" => "bar"}, res) assert_equal(SubHash, res.class) assert res.item_set? end + def test_parse_object_custom_non_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) + assert_equal "bar", res.foo + assert_equal(SubOpenStruct, res.class) + assert res.item_set? + end + def test_generation_of_core_subclasses_with_new_to_json obj = SubHash2["foo" => SubHash2["bar" => true]] obj_json = JSON(obj) |