summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2015-01-02 23:55:20 +0100
committerFlorian Frank <flori@ping.de>2015-01-02 23:55:20 +0100
commit9b4302c7d59cc83e9b4733da9da26df569e0d278 (patch)
tree09cf604c6e7df08c956f850aecb1093986cd08fa
parent05ba45c2d46c8ff4807613b60b204f85a7720997 (diff)
parent737bb637c7accfb1a704bc15560ab8bb83fccfa4 (diff)
downloadjson-9b4302c7d59cc83e9b4733da9da26df569e0d278.tar.gz
Merge branch 'master' into zzak-ruby-2.2
-rw-r--r--java/src/json/ext/Parser.java169
-rw-r--r--java/src/json/ext/Parser.rl9
-rw-r--r--lib/json/add/complex.rb8
-rw-r--r--lib/json/add/rational.rb5
-rw-r--r--lib/json/add/time.rb2
-rw-r--r--lib/json/common.rb8
-rwxr-xr-xtests/test_json.rb6
7 files changed, 113 insertions, 94 deletions
diff --git a/java/src/json/ext/Parser.java b/java/src/json/ext/Parser.java
index 1664240..37423f5 100644
--- a/java/src/json/ext/Parser.java
+++ b/java/src/json/ext/Parser.java
@@ -248,7 +248,7 @@ public class Parser extends RubyObject {
*/
@JRubyMethod
public IRubyObject parse(ThreadContext context) {
- return new ParserSession(this, context).parse();
+ return new ParserSession(this, context, info).parse();
}
/**
@@ -304,6 +304,7 @@ public class Parser extends RubyObject {
private static class ParserSession {
private final Parser parser;
private final ThreadContext context;
+ private final RuntimeInfo info;
private final ByteList byteList;
private final ByteList view;
private final byte[] data;
@@ -315,9 +316,10 @@ public class Parser extends RubyObject {
// no idea about the origins of this value, ask Flori ;)
private static final int EVIL = 0x666;
- private ParserSession(Parser parser, ThreadContext context) {
+ private ParserSession(Parser parser, ThreadContext context, RuntimeInfo info) {
this.parser = parser;
this.context = context;
+ this.info = info;
this.byteList = parser.checkAndGetSource().getByteList();
this.data = byteList.unsafeBytes();
this.view = new ByteList(data, false);
@@ -337,11 +339,11 @@ public class Parser extends RubyObject {
}
-// line 363 "Parser.rl"
+// line 365 "Parser.rl"
-// line 345 "Parser.java"
+// line 347 "Parser.java"
private static byte[] init__JSON_value_actions_0()
{
return new byte [] {
@@ -455,7 +457,7 @@ static final int JSON_value_error = 0;
static final int JSON_value_en_main = 1;
-// line 469 "Parser.rl"
+// line 471 "Parser.rl"
void parseValue(ParserResult res, int p, int pe) {
@@ -463,14 +465,14 @@ static final int JSON_value_en_main = 1;
IRubyObject result = null;
-// line 467 "Parser.java"
+// line 469 "Parser.java"
{
cs = JSON_value_start;
}
-// line 476 "Parser.rl"
+// line 478 "Parser.rl"
-// line 474 "Parser.java"
+// line 476 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -496,13 +498,13 @@ case 1:
while ( _nacts-- > 0 ) {
switch ( _JSON_value_actions[_acts++] ) {
case 9:
-// line 454 "Parser.rl"
+// line 456 "Parser.rl"
{
p--;
{ p += 1; _goto_targ = 5; if (true) continue _goto;}
}
break;
-// line 506 "Parser.java"
+// line 508 "Parser.java"
}
}
@@ -565,25 +567,25 @@ case 1:
switch ( _JSON_value_actions[_acts++] )
{
case 0:
-// line 371 "Parser.rl"
+// line 373 "Parser.rl"
{
result = getRuntime().getNil();
}
break;
case 1:
-// line 374 "Parser.rl"
+// line 376 "Parser.rl"
{
result = getRuntime().getFalse();
}
break;
case 2:
-// line 377 "Parser.rl"
+// line 379 "Parser.rl"
{
result = getRuntime().getTrue();
}
break;
case 3:
-// line 380 "Parser.rl"
+// line 382 "Parser.rl"
{
if (parser.allowNaN) {
result = getConstant(CONST_NAN);
@@ -593,7 +595,7 @@ case 1:
}
break;
case 4:
-// line 387 "Parser.rl"
+// line 389 "Parser.rl"
{
if (parser.allowNaN) {
result = getConstant(CONST_INFINITY);
@@ -603,7 +605,7 @@ case 1:
}
break;
case 5:
-// line 394 "Parser.rl"
+// line 396 "Parser.rl"
{
if (pe > p + 9 - (parser.quirksMode ? 1 : 0) &&
absSubSequence(p, p + 9).equals(JSON_MINUS_INFINITY)) {
@@ -632,7 +634,7 @@ case 1:
}
break;
case 6:
-// line 420 "Parser.rl"
+// line 422 "Parser.rl"
{
parseString(res, p, pe);
if (res.result == null) {
@@ -645,7 +647,7 @@ case 1:
}
break;
case 7:
-// line 430 "Parser.rl"
+// line 432 "Parser.rl"
{
currentNesting++;
parseArray(res, p, pe);
@@ -660,7 +662,7 @@ case 1:
}
break;
case 8:
-// line 442 "Parser.rl"
+// line 444 "Parser.rl"
{
currentNesting++;
parseObject(res, p, pe);
@@ -674,7 +676,7 @@ case 1:
}
}
break;
-// line 678 "Parser.java"
+// line 680 "Parser.java"
}
}
}
@@ -694,7 +696,7 @@ case 5:
break; }
}
-// line 477 "Parser.rl"
+// line 479 "Parser.rl"
if (cs >= JSON_value_first_final && result != null) {
res.update(result, p);
@@ -704,7 +706,7 @@ case 5:
}
-// line 708 "Parser.java"
+// line 710 "Parser.java"
private static byte[] init__JSON_integer_actions_0()
{
return new byte [] {
@@ -803,7 +805,7 @@ static final int JSON_integer_error = 0;
static final int JSON_integer_en_main = 1;
-// line 496 "Parser.rl"
+// line 498 "Parser.rl"
void parseInteger(ParserResult res, int p, int pe) {
@@ -821,15 +823,15 @@ static final int JSON_integer_en_main = 1;
int cs = EVIL;
-// line 825 "Parser.java"
+// line 827 "Parser.java"
{
cs = JSON_integer_start;
}
-// line 513 "Parser.rl"
+// line 515 "Parser.rl"
int memo = p;
-// line 833 "Parser.java"
+// line 835 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -910,13 +912,13 @@ case 1:
switch ( _JSON_integer_actions[_acts++] )
{
case 0:
-// line 490 "Parser.rl"
+// line 492 "Parser.rl"
{
p--;
{ p += 1; _goto_targ = 5; if (true) continue _goto;}
}
break;
-// line 920 "Parser.java"
+// line 922 "Parser.java"
}
}
}
@@ -936,7 +938,7 @@ case 5:
break; }
}
-// line 515 "Parser.rl"
+// line 517 "Parser.rl"
if (cs < JSON_integer_first_final) {
return -1;
@@ -958,7 +960,7 @@ case 5:
}
-// line 962 "Parser.java"
+// line 964 "Parser.java"
private static byte[] init__JSON_float_actions_0()
{
return new byte [] {
@@ -1060,7 +1062,7 @@ static final int JSON_float_error = 0;
static final int JSON_float_en_main = 1;
-// line 550 "Parser.rl"
+// line 552 "Parser.rl"
void parseFloat(ParserResult res, int p, int pe) {
@@ -1078,15 +1080,15 @@ static final int JSON_float_en_main = 1;
int cs = EVIL;
-// line 1082 "Parser.java"
+// line 1084 "Parser.java"
{
cs = JSON_float_start;
}
-// line 567 "Parser.rl"
+// line 569 "Parser.rl"
int memo = p;
-// line 1090 "Parser.java"
+// line 1092 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -1167,13 +1169,13 @@ case 1:
switch ( _JSON_float_actions[_acts++] )
{
case 0:
-// line 541 "Parser.rl"
+// line 543 "Parser.rl"
{
p--;
{ p += 1; _goto_targ = 5; if (true) continue _goto;}
}
break;
-// line 1177 "Parser.java"
+// line 1179 "Parser.java"
}
}
}
@@ -1193,7 +1195,7 @@ case 5:
break; }
}
-// line 569 "Parser.rl"
+// line 571 "Parser.rl"
if (cs < JSON_float_first_final) {
return -1;
@@ -1209,7 +1211,7 @@ case 5:
}
-// line 1213 "Parser.java"
+// line 1215 "Parser.java"
private static byte[] init__JSON_string_actions_0()
{
return new byte [] {
@@ -1311,7 +1313,7 @@ static final int JSON_string_error = 0;
static final int JSON_string_en_main = 1;
-// line 614 "Parser.rl"
+// line 616 "Parser.rl"
void parseString(ParserResult res, int p, int pe) {
@@ -1319,15 +1321,15 @@ static final int JSON_string_en_main = 1;
IRubyObject result = null;
-// line 1323 "Parser.java"
+// line 1325 "Parser.java"
{
cs = JSON_string_start;
}
-// line 621 "Parser.rl"
+// line 623 "Parser.rl"
int memo = p;
-// line 1331 "Parser.java"
+// line 1333 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -1408,7 +1410,7 @@ case 1:
switch ( _JSON_string_actions[_acts++] )
{
case 0:
-// line 589 "Parser.rl"
+// line 591 "Parser.rl"
{
int offset = byteList.begin();
ByteList decoded = decoder.decode(byteList, memo + 1 - offset,
@@ -1423,13 +1425,13 @@ case 1:
}
break;
case 1:
-// line 602 "Parser.rl"
+// line 604 "Parser.rl"
{
p--;
{ p += 1; _goto_targ = 5; if (true) continue _goto;}
}
break;
-// line 1433 "Parser.java"
+// line 1435 "Parser.java"
}
}
}
@@ -1449,7 +1451,7 @@ case 5:
break; }
}
-// line 623 "Parser.rl"
+// line 625 "Parser.rl"
if (parser.createAdditions) {
RubyHash match_string = parser.match_string;
@@ -1476,8 +1478,7 @@ case 5:
}
}
- if (cs >= JSON_string_first_final && result != null) {
- RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime());
+ if (cs >= JSON_string_first_final && result != null) {
if (info.encodingsSupported() && result instanceof RubyString) {
((RubyString)result).force_encoding(context, info.utf8.get());
}
@@ -1488,7 +1489,7 @@ case 5:
}
-// line 1492 "Parser.java"
+// line 1493 "Parser.java"
private static byte[] init__JSON_array_actions_0()
{
return new byte [] {
@@ -1601,7 +1602,7 @@ static final int JSON_array_error = 0;
static final int JSON_array_en_main = 1;
-// line 697 "Parser.rl"
+// line 698 "Parser.rl"
void parseArray(ParserResult res, int p, int pe) {
@@ -1621,14 +1622,14 @@ static final int JSON_array_en_main = 1;
}
-// line 1625 "Parser.java"
+// line 1626 "Parser.java"
{
cs = JSON_array_start;
}
-// line 716 "Parser.rl"
+// line 717 "Parser.rl"
-// line 1632 "Parser.java"
+// line 1633 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -1709,7 +1710,7 @@ case 1:
switch ( _JSON_array_actions[_acts++] )
{
case 0:
-// line 666 "Parser.rl"
+// line 667 "Parser.rl"
{
parseValue(res, p, pe);
if (res.result == null) {
@@ -1726,13 +1727,13 @@ case 1:
}
break;
case 1:
-// line 681 "Parser.rl"
+// line 682 "Parser.rl"
{
p--;
{ p += 1; _goto_targ = 5; if (true) continue _goto;}
}
break;
-// line 1736 "Parser.java"
+// line 1737 "Parser.java"
}
}
}
@@ -1752,7 +1753,7 @@ case 5:
break; }
}
-// line 717 "Parser.rl"
+// line 718 "Parser.rl"
if (cs >= JSON_array_first_final) {
res.update(result, p + 1);
@@ -1762,7 +1763,7 @@ case 5:
}
-// line 1766 "Parser.java"
+// line 1767 "Parser.java"
private static byte[] init__JSON_object_actions_0()
{
return new byte [] {
@@ -1885,7 +1886,7 @@ static final int JSON_object_error = 0;
static final int JSON_object_en_main = 1;
-// line 776 "Parser.rl"
+// line 777 "Parser.rl"
void parseObject(ParserResult res, int p, int pe) {
@@ -1910,14 +1911,14 @@ static final int JSON_object_en_main = 1;
}
-// line 1914 "Parser.java"
+// line 1915 "Parser.java"
{
cs = JSON_object_start;
}
-// line 800 "Parser.rl"
+// line 801 "Parser.rl"
-// line 1921 "Parser.java"
+// line 1922 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -1998,7 +1999,7 @@ case 1:
switch ( _JSON_object_actions[_acts++] )
{
case 0:
-// line 731 "Parser.rl"
+// line 732 "Parser.rl"
{
parseValue(res, p, pe);
if (res.result == null) {
@@ -2015,7 +2016,7 @@ case 1:
}
break;
case 1:
-// line 746 "Parser.rl"
+// line 747 "Parser.rl"
{
parseString(res, p, pe);
if (res.result == null) {
@@ -2035,13 +2036,13 @@ case 1:
}
break;
case 2:
-// line 764 "Parser.rl"
+// line 765 "Parser.rl"
{
p--;
{ p += 1; _goto_targ = 5; if (true) continue _goto;}
}
break;
-// line 2045 "Parser.java"
+// line 2046 "Parser.java"
}
}
}
@@ -2061,7 +2062,7 @@ case 5:
break; }
}
-// line 801 "Parser.rl"
+// line 802 "Parser.rl"
if (cs < JSON_object_first_final) {
res.update(null, p + 1);
@@ -2094,7 +2095,7 @@ case 5:
}
-// line 2098 "Parser.java"
+// line 2099 "Parser.java"
private static byte[] init__JSON_actions_0()
{
return new byte [] {
@@ -2198,7 +2199,7 @@ static final int JSON_error = 0;
static final int JSON_en_main = 1;
-// line 866 "Parser.rl"
+// line 867 "Parser.rl"
public IRubyObject parseStrict() {
@@ -2208,16 +2209,16 @@ static final int JSON_en_main = 1;
ParserResult res = new ParserResult();
-// line 2212 "Parser.java"
+// line 2213 "Parser.java"
{
cs = JSON_start;
}
-// line 875 "Parser.rl"
+// line 876 "Parser.rl"
p = byteList.begin();
pe = p + byteList.length();
-// line 2221 "Parser.java"
+// line 2222 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -2298,7 +2299,7 @@ case 1:
switch ( _JSON_actions[_acts++] )
{
case 0:
-// line 838 "Parser.rl"
+// line 839 "Parser.rl"
{
currentNesting = 1;
parseObject(res, p, pe);
@@ -2312,7 +2313,7 @@ case 1:
}
break;
case 1:
-// line 850 "Parser.rl"
+// line 851 "Parser.rl"
{
currentNesting = 1;
parseArray(res, p, pe);
@@ -2325,7 +2326,7 @@ case 1:
}
}
break;
-// line 2329 "Parser.java"
+// line 2330 "Parser.java"
}
}
}
@@ -2345,7 +2346,7 @@ case 5:
break; }
}
-// line 878 "Parser.rl"
+// line 879 "Parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -2355,7 +2356,7 @@ case 5:
}
-// line 2359 "Parser.java"
+// line 2360 "Parser.java"
private static byte[] init__JSON_quirks_mode_actions_0()
{
return new byte [] {
@@ -2458,7 +2459,7 @@ static final int JSON_quirks_mode_error = 0;
static final int JSON_quirks_mode_en_main = 1;
-// line 906 "Parser.rl"
+// line 907 "Parser.rl"
public IRubyObject parseQuirksMode() {
@@ -2468,16 +2469,16 @@ static final int JSON_quirks_mode_en_main = 1;
ParserResult res = new ParserResult();
-// line 2472 "Parser.java"
+// line 2473 "Parser.java"
{
cs = JSON_quirks_mode_start;
}
-// line 915 "Parser.rl"
+// line 916 "Parser.rl"
p = byteList.begin();
pe = p + byteList.length();
-// line 2481 "Parser.java"
+// line 2482 "Parser.java"
{
int _klen;
int _trans = 0;
@@ -2558,7 +2559,7 @@ case 1:
switch ( _JSON_quirks_mode_actions[_acts++] )
{
case 0:
-// line 892 "Parser.rl"
+// line 893 "Parser.rl"
{
parseValue(res, p, pe);
if (res.result == null) {
@@ -2570,7 +2571,7 @@ case 1:
}
}
break;
-// line 2574 "Parser.java"
+// line 2575 "Parser.java"
}
}
}
@@ -2590,7 +2591,7 @@ case 5:
break; }
}
-// line 918 "Parser.rl"
+// line 919 "Parser.rl"
if (cs >= JSON_quirks_mode_first_final && p == pe) {
return result;
diff --git a/java/src/json/ext/Parser.rl b/java/src/json/ext/Parser.rl
index 7dcdd2e..6d65963 100644
--- a/java/src/json/ext/Parser.rl
+++ b/java/src/json/ext/Parser.rl
@@ -246,7 +246,7 @@ public class Parser extends RubyObject {
*/
@JRubyMethod
public IRubyObject parse(ThreadContext context) {
- return new ParserSession(this, context).parse();
+ return new ParserSession(this, context, info).parse();
}
/**
@@ -302,6 +302,7 @@ public class Parser extends RubyObject {
private static class ParserSession {
private final Parser parser;
private final ThreadContext context;
+ private final RuntimeInfo info;
private final ByteList byteList;
private final ByteList view;
private final byte[] data;
@@ -313,9 +314,10 @@ public class Parser extends RubyObject {
// no idea about the origins of this value, ask Flori ;)
private static final int EVIL = 0x666;
- private ParserSession(Parser parser, ThreadContext context) {
+ private ParserSession(Parser parser, ThreadContext context, RuntimeInfo info) {
this.parser = parser;
this.context = context;
+ this.info = info;
this.byteList = parser.checkAndGetSource().getByteList();
this.data = byteList.unsafeBytes();
this.view = new ByteList(data, false);
@@ -646,8 +648,7 @@ public class Parser extends RubyObject {
}
}
- if (cs >= JSON_string_first_final && result != null) {
- RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime());
+ if (cs >= JSON_string_first_final && result != null) {
if (info.encodingsSupported() && result instanceof RubyString) {
((RubyString)result).force_encoding(context, info.utf8.get());
}
diff --git a/lib/json/add/complex.rb b/lib/json/add/complex.rb
index d7ebebf..2723f60 100644
--- a/lib/json/add/complex.rb
+++ b/lib/json/add/complex.rb
@@ -4,10 +4,15 @@ end
defined?(::Complex) or require 'complex'
class Complex
+
+ # Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
+ # value <tt>i</tt>, to a Complex object.
def self.json_create(object)
Complex(object['r'], object['i'])
end
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -16,7 +21,8 @@ class Complex
}
end
+ # Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
def to_json(*)
as_json.to_json
end
-end
+end \ No newline at end of file
diff --git a/lib/json/add/rational.rb b/lib/json/add/rational.rb
index 867cd92..ee39c20 100644
--- a/lib/json/add/rational.rb
+++ b/lib/json/add/rational.rb
@@ -4,10 +4,14 @@ end
defined?(::Rational) or require 'rational'
class Rational
+ # Deserializes JSON string by converting numerator value <tt>n</tt>,
+ # denominator value <tt>d</tt>, to a Rational object.
def self.json_create(object)
Rational(object['n'], object['d'])
end
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
def as_json(*)
{
JSON.create_id => self.class.name,
@@ -16,6 +20,7 @@ class Rational
}
end
+ # Stores class name (Rational) along with numerator value <tt>n</tt> and denominator value <tt>d</tt> as JSON string
def to_json(*)
as_json.to_json
end
diff --git a/lib/json/add/time.rb b/lib/json/add/time.rb
index 338209d..d983467 100644
--- a/lib/json/add/time.rb
+++ b/lib/json/add/time.rb
@@ -10,7 +10,7 @@ class Time
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
object['n'] = usec * 1000
end
- if instance_methods.include?(:tv_nsec)
+ if method_defined?(:tv_nsec)
at(object['s'], Rational(object['n'], 1000))
else
at(object['s'], object['n'] / 1000)
diff --git a/lib/json/common.rb b/lib/json/common.rb
index 8fbaa2b..f44184e 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -148,7 +148,7 @@ module JSON
# the default.
# * *create_additions*: If set to false, the Parser doesn't create
# additions even if a matching class and create_id was found. This option
- # defaults to true.
+ # defaults to false.
# * *object_class*: Defaults to Hash
# * *array_class*: Defaults to Array
def parse(source, opts = {})
@@ -169,7 +169,7 @@ module JSON
# to true.
# * *create_additions*: If set to false, the Parser doesn't create
# additions even if a matching class and create_id was found. This option
- # defaults to true.
+ # defaults to false.
def parse!(source, opts = {})
opts = {
:max_nesting => false,
@@ -390,7 +390,7 @@ module JSON
end
end
opts = JSON.dump_default_options
- limit and opts.update(:max_nesting => limit)
+ opts = opts.merge(:max_nesting => limit) if limit
result = generate(obj, opts)
if anIO
anIO.write result
@@ -411,7 +411,7 @@ module JSON
string
end
- # Shortuct for iconv.
+ # Shortcut for iconv.
if ::String.method_defined?(:encode)
# Encodes string using Ruby's _String.encode_
def self.iconv(to, from, string)
diff --git a/tests/test_json.rb b/tests/test_json.rb
index 462650e..7957773 100755
--- a/tests/test_json.rb
+++ b/tests/test_json.rb
@@ -517,6 +517,12 @@ EOT
assert_equal too_deep, output.string
end
+ def test_dump_should_modify_defaults
+ max_nesting = JSON.dump_default_options[:max_nesting]
+ JSON.dump([], StringIO.new, 10)
+ assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
+ end
+
def test_big_integers
json1 = JSON([orig = (1 << 31) - 1])
assert_equal orig, JSON[json1][0]