summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/json/generator/generator.c54
-rw-r--r--ext/json/generator/generator.h1
-rw-r--r--ext/json/json.gemspecbin592 -> 5473 bytes
-rw-r--r--ext/json/lib/json/common.rb75
-rw-r--r--ext/json/lib/json/ext.rb6
-rw-r--r--ext/json/lib/json/ext/.keep0
-rw-r--r--ext/json/lib/json/generic_object.rb8
-rw-r--r--ext/json/lib/json/version.rb2
-rw-r--r--ext/json/parser/parser.c735
-rw-r--r--ext/json/parser/parser.h8
-rw-r--r--ext/json/parser/parser.rl214
-rw-r--r--ext/json/parser/prereq.mk10
12 files changed, 371 insertions, 742 deletions
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index faa4fdf011..2b56721b2d 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -20,7 +20,7 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
- i_quirks_mode, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
+ i_pack, i_unpack, i_create_id, i_extend, i_key_p,
i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
i_buffer_initial_length, i_dup;
@@ -222,6 +222,7 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
}
}
+ RB_GC_GUARD(string);
}
/* Converts string to a JSON string in FBuffer buffer, where only the
@@ -641,8 +642,6 @@ static VALUE cState_configure(VALUE self, VALUE opts)
state->allow_nan = RTEST(tmp);
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
state->ascii_only = RTEST(tmp);
- tmp = rb_hash_aref(opts, ID2SYM(i_quirks_mode));
- state->quirks_mode = RTEST(tmp);
return self;
}
@@ -676,7 +675,6 @@ static VALUE cState_to_h(VALUE self)
rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
- rb_hash_aset(result, ID2SYM(i_quirks_mode), state->quirks_mode ? Qtrue : Qfalse);
rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
@@ -853,7 +851,6 @@ static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_
generate_json_bignum(buffer, Vstate, state, obj);
}
#endif
-
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
{
double value = RFLOAT_VALUE(obj);
@@ -944,21 +941,6 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
}
/*
- * This function returns true if string is either a JSON array or JSON object.
- * It might suffer from false positives, e. g. syntactically incorrect JSON in
- * the string or certain UTF-8 characters on the right hand side.
- */
-static int isArrayOrObject(VALUE string)
-{
- long string_len = RSTRING_LEN(string);
- char *p = RSTRING_PTR(string), *q = p + string_len - 1;
- if (string_len < 2) return 0;
- for (; p < q && isspace((unsigned char)*p); p++);
- for (; q > p && isspace((unsigned char)*q); q--);
- return (*p == '[' && *q == ']') || (*p == '{' && *q == '}');
-}
-
-/*
* call-seq: generate(obj)
*
* Generates a valid JSON document from object +obj+ and returns the
@@ -969,9 +951,6 @@ static VALUE cState_generate(VALUE self, VALUE obj)
{
VALUE result = cState_partial_generate(self, obj);
GET_STATE(self);
- if (!state->quirks_mode && !isArrayOrObject(result)) {
- rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
- }
return result;
}
@@ -990,8 +969,6 @@ static VALUE cState_generate(VALUE self, VALUE obj)
* * *allow_nan*: true if NaN, Infinity, and -Infinity should be
* generated, otherwise an exception is thrown, if these values are
* encountered. This options defaults to false.
- * * *quirks_mode*: Enables quirks_mode for parser, that is for example
- * generating single JSON values instead of documents is possible.
* * *buffer_initial_length*: sets the initial length of the generator's
* internal buffer.
*/
@@ -1299,29 +1276,6 @@ static VALUE cState_ascii_only_p(VALUE self)
}
/*
- * call-seq: quirks_mode?
- *
- * Returns true, if quirks mode is enabled. Otherwise returns false.
- */
-static VALUE cState_quirks_mode_p(VALUE self)
-{
- GET_STATE(self);
- return state->quirks_mode ? Qtrue : Qfalse;
-}
-
-/*
- * call-seq: quirks_mode=(enable)
- *
- * If set to true, enables the quirks_mode mode.
- */
-static VALUE cState_quirks_mode_set(VALUE self, VALUE enable)
-{
- GET_STATE(self);
- state->quirks_mode = RTEST(enable);
- return Qnil;
-}
-
-/*
* call-seq: depth
*
* This integer returns the current depth of data structure nesting.
@@ -1409,9 +1363,6 @@ void Init_generator(void)
rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
- rb_define_method(cState, "quirks_mode?", cState_quirks_mode_p, 0);
- rb_define_method(cState, "quirks_mode", cState_quirks_mode_p, 0);
- rb_define_method(cState, "quirks_mode=", cState_quirks_mode_set, 1);
rb_define_method(cState, "depth", cState_depth, 0);
rb_define_method(cState, "depth=", cState_depth_set, 1);
rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
@@ -1468,7 +1419,6 @@ void Init_generator(void)
i_max_nesting = rb_intern("max_nesting");
i_allow_nan = rb_intern("allow_nan");
i_ascii_only = rb_intern("ascii_only");
- i_quirks_mode = rb_intern("quirks_mode");
i_depth = rb_intern("depth");
i_buffer_initial_length = rb_intern("buffer_initial_length");
i_pack = rb_intern("pack");
diff --git a/ext/json/generator/generator.h b/ext/json/generator/generator.h
index eb4557ff55..900b4d58f3 100644
--- a/ext/json/generator/generator.h
+++ b/ext/json/generator/generator.h
@@ -73,7 +73,6 @@ typedef struct JSON_Generator_StateStruct {
long max_nesting;
char allow_nan;
char ascii_only;
- char quirks_mode;
long depth;
long buffer_initial_length;
} JSON_Generator_State;
diff --git a/ext/json/json.gemspec b/ext/json/json.gemspec
index 9fb6fa155a..c5a3c1003d 100644
--- a/ext/json/json.gemspec
+++ b/ext/json/json.gemspec
Binary files differ
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index 020615fc1d..a0221fe94b 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/lib/json/common.rb
@@ -4,12 +4,12 @@ require 'json/generic_object'
module JSON
class << self
- # If _object_ is string-like, parse the string and return the parsed result
- # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
- # data structure object and return it.
+ # If _object_ is string-like, parse the string and return the parsed
+ # result as a Ruby data structure. Otherwise generate a JSON text from the
+ # Ruby data structure object and return it.
#
- # The _opts_ argument is passed through to generate/parse respectively. See
- # generate and parse for their documentation.
+ # The _opts_ argument is passed through to generate/parse respectively.
+ # See generate and parse for their documentation.
def [](object, opts = {})
if object.respond_to? :to_str
JSON.parse(object.to_str, opts)
@@ -25,7 +25,7 @@ module JSON
# Set the JSON parser class _parser_ to be used by JSON.
def parser=(parser) # :nodoc:
@parser = parser
- remove_const :Parser if JSON.const_defined_in?(self, :Parser)
+ remove_const :Parser if const_defined?(:Parser, false)
const_set :Parser, parser
end
@@ -36,8 +36,8 @@ module JSON
def deep_const_get(path) # :nodoc:
path.to_s.split(/::/).inject(Object) do |p, c|
case
- when c.empty? then p
- when JSON.const_defined_in?(p, c) then p.const_get(c)
+ when c.empty? then p
+ when p.const_defined?(c, true) then p.const_get(c)
else
begin
p.const_missing(c)
@@ -139,10 +139,10 @@ module JSON
# _opts_ can have the following
# keys:
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
- # structures. Disable depth checking with :max_nesting => false. It defaults
- # to 100.
+ # structures. Disable depth checking with :max_nesting => false. It
+ # defaults to 100.
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
+ # defiance of RFC 7159 to be parsed by the Parser. This option defaults
# to false.
# * *symbolize_names*: If set to true, returns symbols for the names
# (keys) in a JSON object. Otherwise strings are returned. Strings are
@@ -162,11 +162,11 @@ module JSON
#
# _opts_ can have the following keys:
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
- # structures. Enable depth checking with :max_nesting => anInteger. The parse!
- # methods defaults to not doing max depth checking: This can be dangerous
- # if someone wants to fill up your stack.
+ # structures. Enable depth checking with :max_nesting => anInteger. The
+ # parse! methods defaults to not doing max depth checking: This can be
+ # dangerous if someone wants to fill up your stack.
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
+ # defiance of RFC 7159 to be parsed by the Parser. This option defaults
# 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
@@ -175,7 +175,7 @@ module JSON
opts = {
:max_nesting => false,
:allow_nan => true
- }.update(opts)
+ }.merge(opts)
Parser.new(source, opts).parse
end
@@ -296,13 +296,13 @@ module JSON
# The global default options for the JSON.load method:
# :max_nesting: false
# :allow_nan: true
- # :quirks_mode: true
+ # :allow_blank: true
attr_accessor :load_default_options
end
self.load_default_options = {
:max_nesting => false,
:allow_nan => true,
- :quirks_mode => true,
+ :allow_blank => true,
:create_additions => true,
}
@@ -329,7 +329,7 @@ module JSON
elsif source.respond_to?(:read)
source = source.read
end
- if opts[:quirks_mode] && (source.nil? || source.empty?)
+ if opts[:allow_blank] && (source.nil? || source.empty?)
source = 'null'
end
result = parse(source, opts)
@@ -358,13 +358,12 @@ module JSON
# The global default options for the JSON.dump method:
# :max_nesting: false
# :allow_nan: true
- # :quirks_mode: true
+ # :allow_blank: true
attr_accessor :dump_default_options
end
self.dump_default_options = {
:max_nesting => false,
:allow_nan => true,
- :quirks_mode => true,
}
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
@@ -403,37 +402,9 @@ module JSON
raise ArgumentError, "exceed depth limit"
end
- # Swap consecutive bytes of _string_ in place.
- def self.swap!(string) # :nodoc:
- 0.upto(string.size / 2) do |i|
- break unless string[2 * i + 1]
- string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
- end
- string
- end
-
- # Shortcut for iconv.
- if ::String.method_defined?(:encode)
- # Encodes string using Ruby's _String.encode_
- def self.iconv(to, from, string)
- string.encode(to, from)
- end
- else
- require 'iconv'
- # Encodes string using _iconv_ library
- def self.iconv(to, from, string)
- Iconv.conv(to, from, string)
- end
- end
-
- if ::Object.method(:const_defined?).arity == 1
- def self.const_defined_in?(modul, constant)
- modul.const_defined?(constant)
- end
- else
- def self.const_defined_in?(modul, constant)
- modul.const_defined?(constant, false)
- end
+ # Encodes string using Ruby's _String.encode_
+ def self.iconv(to, from, string)
+ string.encode(to, from)
end
end
diff --git a/ext/json/lib/json/ext.rb b/ext/json/lib/json/ext.rb
index 2486f084cd..43b8519b34 100644
--- a/ext/json/lib/json/ext.rb
+++ b/ext/json/lib/json/ext.rb
@@ -1,10 +1,4 @@
# frozen_string_literal: false
-if ENV['SIMPLECOV_COVERAGE'].to_i == 1
- require 'simplecov'
- SimpleCov.start do
- add_filter "/tests/"
- end
-end
require 'json/common'
module JSON
diff --git a/ext/json/lib/json/ext/.keep b/ext/json/lib/json/ext/.keep
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/ext/json/lib/json/ext/.keep
diff --git a/ext/json/lib/json/generic_object.rb b/ext/json/lib/json/generic_object.rb
index 2241f6c7ce..8d72d90092 100644
--- a/ext/json/lib/json/generic_object.rb
+++ b/ext/json/lib/json/generic_object.rb
@@ -48,6 +48,14 @@ module JSON
table
end
+ def [](name)
+ __send__(name)
+ end unless method_defined?(:[])
+
+ def []=(name, value)
+ __send__("#{name}=", value)
+ end unless method_defined?(:[]=)
+
def |(other)
self.class[other.to_hash.merge(to_hash)]
end
diff --git a/ext/json/lib/json/version.rb b/ext/json/lib/json/version.rb
index b5748334b9..5184ad3c19 100644
--- a/ext/json/lib/json/version.rb
+++ b/ext/json/lib/json/version.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: false
module JSON
# JSON version
- VERSION = '1.8.3'
+ VERSION = '2.0.1'
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index 773605cf7c..975a267695 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -4,7 +4,7 @@
#include "parser.h"
#if defined HAVE_RUBY_ENCODING_H
-# define EXC_ENCODING UTF_8,
+# define EXC_ENCODING rb_utf8_encoding(),
# ifndef HAVE_RB_ENC_RAISE
static void
enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
@@ -89,26 +89,20 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
return len;
}
-#ifdef HAVE_RUBY_ENCODING_H
-static rb_encoding *UTF_8, *UTF_16BE, *UTF_16LE, *UTF_32BE, *UTF_32LE;
-#else
-static ID i_iconv;
-#endif
-
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
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_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
i_match_string, i_aset, i_aref, i_leftshift;
-#line 130 "parser.rl"
+#line 124 "parser.rl"
-#line 112 "parser.c"
+#line 106 "parser.c"
enum {JSON_object_start = 1};
enum {JSON_object_first_final = 27};
enum {JSON_object_error = 0};
@@ -116,30 +110,30 @@ enum {JSON_object_error = 0};
enum {JSON_object_en_main = 1};
-#line 171 "parser.rl"
+#line 165 "parser.rl"
-static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
+static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
{
int cs = EVIL;
VALUE last_name = Qnil;
VALUE object_class = json->object_class;
- if (json->max_nesting && json->current_nesting > json->max_nesting) {
- rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
+ if (json->max_nesting && current_nesting > json->max_nesting) {
+ rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
}
*result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
-#line 136 "parser.c"
+#line 130 "parser.c"
{
cs = JSON_object_start;
}
-#line 186 "parser.rl"
+#line 180 "parser.rl"
-#line 143 "parser.c"
+#line 137 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -167,7 +161,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 153 "parser.rl"
+#line 147 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -180,7 +174,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 184 "parser.c"
+#line 178 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -247,10 +241,10 @@ case 8:
goto st8;
goto st0;
tr11:
-#line 138 "parser.rl"
+#line 132 "parser.rl"
{
VALUE v = Qnil;
- char *np = JSON_parse_value(json, p, pe, &v);
+ char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
if (np == NULL) {
p--; {p++; cs = 9; goto _out;}
} else {
@@ -267,7 +261,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 271 "parser.c"
+#line 265 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -356,14 +350,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 161 "parser.rl"
+#line 155 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 367 "parser.c"
+#line 361 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -461,7 +455,7 @@ case 26:
_out: {}
}
-#line 187 "parser.rl"
+#line 181 "parser.rl"
if (cs >= JSON_object_first_final) {
if (json->create_additions) {
@@ -486,69 +480,78 @@ case 26:
-#line 490 "parser.c"
+#line 484 "parser.c"
enum {JSON_value_start = 1};
-enum {JSON_value_first_final = 21};
+enum {JSON_value_first_final = 29};
enum {JSON_value_error = 0};
enum {JSON_value_en_main = 1};
-#line 291 "parser.rl"
+#line 281 "parser.rl"
-static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
+static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
{
int cs = EVIL;
-#line 506 "parser.c"
+#line 500 "parser.c"
{
cs = JSON_value_start;
}
-#line 298 "parser.rl"
+#line 288 "parser.rl"
-#line 513 "parser.c"
+#line 507 "parser.c"
{
if ( p == pe )
goto _test_eof;
switch ( cs )
{
+st1:
+ if ( ++p == pe )
+ goto _test_eof1;
case 1:
switch( (*p) ) {
- case 34: goto tr0;
- case 45: goto tr2;
- case 73: goto st2;
- case 78: goto st9;
- case 91: goto tr5;
- case 102: goto st11;
- case 110: goto st15;
- case 116: goto st18;
- case 123: goto tr9;
+ case 13: goto st1;
+ case 32: goto st1;
+ case 34: goto tr2;
+ case 45: goto tr3;
+ case 47: goto st6;
+ case 73: goto st10;
+ case 78: goto st17;
+ case 91: goto tr7;
+ case 102: goto st19;
+ case 110: goto st23;
+ case 116: goto st26;
+ case 123: goto tr11;
}
- if ( 48 <= (*p) && (*p) <= 57 )
- goto tr2;
+ if ( (*p) > 10 ) {
+ if ( 48 <= (*p) && (*p) <= 57 )
+ goto tr3;
+ } else if ( (*p) >= 9 )
+ goto st1;
goto st0;
st0:
cs = 0;
goto _out;
-tr0:
-#line 239 "parser.rl"
+tr2:
+#line 233 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
- if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
+ if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
}
- goto st21;
-tr2:
-#line 244 "parser.rl"
+ goto st29;
+tr3:
+#line 238 "parser.rl"
{
char *np;
- if(pe > p + 9 - json->quirks_mode && !strncmp(MinusInfinity, p, 9)) {
+ if(pe > p + 8 && !strncmp(MinusInfinity, p, 9)) {
if (json->allow_nan) {
*result = CMinusInfinity;
{p = (( p + 10))-1;}
- p--; {p++; cs = 21; goto _out;}
+ p--; {p++; cs = 29; goto _out;}
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
}
@@ -557,31 +560,27 @@ tr2:
if (np != NULL) {p = (( np))-1;}
np = JSON_parse_integer(json, p, pe, result);
if (np != NULL) {p = (( np))-1;}
- p--; {p++; cs = 21; goto _out;}
+ p--; {p++; cs = 29; goto _out;}
}
- goto st21;
-tr5:
-#line 262 "parser.rl"
+ goto st29;
+tr7:
+#line 256 "parser.rl"
{
char *np;
- json->current_nesting++;
- np = JSON_parse_array(json, p, pe, result);
- json->current_nesting--;
- if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
+ np = JSON_parse_array(json, p, pe, result, current_nesting + 1);
+ if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
}
- goto st21;
-tr9:
-#line 270 "parser.rl"
+ goto st29;
+tr11:
+#line 262 "parser.rl"
{
char *np;
- json->current_nesting++;
- np = JSON_parse_object(json, p, pe, result);
- json->current_nesting--;
- if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
+ np = JSON_parse_object(json, p, pe, result, current_nesting + 1);
+ if (np == NULL) { p--; {p++; cs = 29; goto _out;} } else {p = (( np))-1;}
}
- goto st21;
-tr16:
-#line 232 "parser.rl"
+ goto st29;
+tr25:
+#line 226 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -589,9 +588,9 @@ tr16:
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
}
}
- goto st21;
-tr18:
-#line 225 "parser.rl"
+ goto st29;
+tr27:
+#line 219 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -599,168 +598,240 @@ tr18:
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
}
}
- goto st21;
-tr22:
-#line 219 "parser.rl"
+ goto st29;
+tr31:
+#line 213 "parser.rl"
{
*result = Qfalse;
}
- goto st21;
-tr25:
-#line 216 "parser.rl"
+ goto st29;
+tr34:
+#line 210 "parser.rl"
{
*result = Qnil;
}
- goto st21;
-tr28:
-#line 222 "parser.rl"
+ goto st29;
+tr37:
+#line 216 "parser.rl"
{
*result = Qtrue;
}
- goto st21;
-st21:
- if ( ++p == pe )
- goto _test_eof21;
-case 21:
-#line 278 "parser.rl"
- { p--; {p++; cs = 21; goto _out;} }
-#line 628 "parser.c"
+ goto st29;
+st29:
+ if ( ++p == pe )
+ goto _test_eof29;
+case 29:
+#line 268 "parser.rl"
+ { p--; {p++; cs = 29; goto _out;} }
+#line 627 "parser.c"
+ switch( (*p) ) {
+ case 13: goto st29;
+ case 32: goto st29;
+ case 47: goto st2;
+ }
+ if ( 9 <= (*p) && (*p) <= 10 )
+ goto st29;
goto st0;
st2:
if ( ++p == pe )
goto _test_eof2;
case 2:
- if ( (*p) == 110 )
- goto st3;
+ switch( (*p) ) {
+ case 42: goto st3;
+ case 47: goto st5;
+ }
goto st0;
st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
- if ( (*p) == 102 )
+ if ( (*p) == 42 )
goto st4;
- goto st0;
+ goto st3;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
- if ( (*p) == 105 )
- goto st5;
- goto st0;
+ switch( (*p) ) {
+ case 42: goto st4;
+ case 47: goto st29;
+ }
+ goto st3;
st5:
if ( ++p == pe )
goto _test_eof5;
case 5:
- if ( (*p) == 110 )
- goto st6;
- goto st0;
+ if ( (*p) == 10 )
+ goto st29;
+ goto st5;
st6:
if ( ++p == pe )
goto _test_eof6;
case 6:
- if ( (*p) == 105 )
- goto st7;
+ switch( (*p) ) {
+ case 42: goto st7;
+ case 47: goto st9;
+ }
goto st0;
st7:
if ( ++p == pe )
goto _test_eof7;
case 7:
- if ( (*p) == 116 )
+ if ( (*p) == 42 )
goto st8;
- goto st0;
+ goto st7;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
- if ( (*p) == 121 )
- goto tr16;
- goto st0;
+ switch( (*p) ) {
+ case 42: goto st8;
+ case 47: goto st1;
+ }
+ goto st7;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
- if ( (*p) == 97 )
- goto st10;
- goto st0;
+ if ( (*p) == 10 )
+ goto st1;
+ goto st9;
st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
- if ( (*p) == 78 )
- goto tr18;
+ if ( (*p) == 110 )
+ goto st11;
goto st0;
st11:
if ( ++p == pe )
goto _test_eof11;
case 11:
- if ( (*p) == 97 )
+ if ( (*p) == 102 )
goto st12;
goto st0;
st12:
if ( ++p == pe )
goto _test_eof12;
case 12:
- if ( (*p) == 108 )
+ if ( (*p) == 105 )
goto st13;
goto st0;
st13:
if ( ++p == pe )
goto _test_eof13;
case 13:
- if ( (*p) == 115 )
+ if ( (*p) == 110 )
goto st14;
goto st0;
st14:
if ( ++p == pe )
goto _test_eof14;
case 14:
- if ( (*p) == 101 )
- goto tr22;
+ if ( (*p) == 105 )
+ goto st15;
goto st0;
st15:
if ( ++p == pe )
goto _test_eof15;
case 15:
- if ( (*p) == 117 )
+ if ( (*p) == 116 )
goto st16;
goto st0;
st16:
if ( ++p == pe )
goto _test_eof16;
case 16:
- if ( (*p) == 108 )
- goto st17;
+ if ( (*p) == 121 )
+ goto tr25;
goto st0;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
- if ( (*p) == 108 )
- goto tr25;
+ if ( (*p) == 97 )
+ goto st18;
goto st0;
st18:
if ( ++p == pe )
goto _test_eof18;
case 18:
- if ( (*p) == 114 )
- goto st19;
+ if ( (*p) == 78 )
+ goto tr27;
goto st0;
st19:
if ( ++p == pe )
goto _test_eof19;
case 19:
- if ( (*p) == 117 )
+ if ( (*p) == 97 )
goto st20;
goto st0;
st20:
if ( ++p == pe )
goto _test_eof20;
case 20:
+ if ( (*p) == 108 )
+ goto st21;
+ goto st0;
+st21:
+ if ( ++p == pe )
+ goto _test_eof21;
+case 21:
+ if ( (*p) == 115 )
+ goto st22;
+ goto st0;
+st22:
+ if ( ++p == pe )
+ goto _test_eof22;
+case 22:
if ( (*p) == 101 )
- goto tr28;
+ goto tr31;
+ goto st0;
+st23:
+ if ( ++p == pe )
+ goto _test_eof23;
+case 23:
+ if ( (*p) == 117 )
+ goto st24;
+ goto st0;
+st24:
+ if ( ++p == pe )
+ goto _test_eof24;
+case 24:
+ if ( (*p) == 108 )
+ goto st25;
+ goto st0;
+st25:
+ if ( ++p == pe )
+ goto _test_eof25;
+case 25:
+ if ( (*p) == 108 )
+ goto tr34;
+ goto st0;
+st26:
+ if ( ++p == pe )
+ goto _test_eof26;
+case 26:
+ if ( (*p) == 114 )
+ goto st27;
+ goto st0;
+st27:
+ if ( ++p == pe )
+ goto _test_eof27;
+case 27:
+ if ( (*p) == 117 )
+ goto st28;
+ goto st0;
+st28:
+ if ( ++p == pe )
+ goto _test_eof28;
+case 28:
+ if ( (*p) == 101 )
+ goto tr37;
goto st0;
}
- _test_eof21: cs = 21; goto _test_eof;
+ _test_eof1: cs = 1; goto _test_eof;
+ _test_eof29: cs = 29; goto _test_eof;
_test_eof2: cs = 2; goto _test_eof;
_test_eof3: cs = 3; goto _test_eof;
_test_eof4: cs = 4; goto _test_eof;
@@ -780,12 +851,20 @@ case 20:
_test_eof18: cs = 18; goto _test_eof;
_test_eof19: cs = 19; goto _test_eof;
_test_eof20: cs = 20; goto _test_eof;
+ _test_eof21: cs = 21; goto _test_eof;
+ _test_eof22: cs = 22; goto _test_eof;
+ _test_eof23: cs = 23; goto _test_eof;
+ _test_eof24: cs = 24; goto _test_eof;
+ _test_eof25: cs = 25; goto _test_eof;
+ _test_eof26: cs = 26; goto _test_eof;
+ _test_eof27: cs = 27; goto _test_eof;
+ _test_eof28: cs = 28; goto _test_eof;
_test_eof: {}
_out: {}
}
-#line 299 "parser.rl"
+#line 289 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -795,7 +874,7 @@ case 20:
}
-#line 799 "parser.c"
+#line 878 "parser.c"
enum {JSON_integer_start = 1};
enum {JSON_integer_first_final = 3};
enum {JSON_integer_error = 0};
@@ -803,7 +882,7 @@ enum {JSON_integer_error = 0};
enum {JSON_integer_en_main = 1};
-#line 315 "parser.rl"
+#line 305 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -811,15 +890,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 815 "parser.c"
+#line 894 "parser.c"
{
cs = JSON_integer_start;
}
-#line 322 "parser.rl"
+#line 312 "parser.rl"
json->memo = p;
-#line 823 "parser.c"
+#line 902 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -853,14 +932,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 312 "parser.rl"
+#line 302 "parser.rl"
{ p--; {p++; cs = 4; goto _out;} }
goto st4;
st4:
if ( ++p == pe )
goto _test_eof4;
case 4:
-#line 864 "parser.c"
+#line 943 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -879,7 +958,7 @@ case 5:
_out: {}
}
-#line 324 "parser.rl"
+#line 314 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -894,7 +973,7 @@ case 5:
}
-#line 898 "parser.c"
+#line 977 "parser.c"
enum {JSON_float_start = 1};
enum {JSON_float_first_final = 8};
enum {JSON_float_error = 0};
@@ -902,7 +981,7 @@ enum {JSON_float_error = 0};
enum {JSON_float_en_main = 1};
-#line 349 "parser.rl"
+#line 339 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -910,15 +989,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 914 "parser.c"
+#line 993 "parser.c"
{
cs = JSON_float_start;
}
-#line 356 "parser.rl"
+#line 346 "parser.rl"
json->memo = p;
-#line 922 "parser.c"
+#line 1001 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -976,14 +1055,14 @@ case 8:
goto st0;
goto tr9;
tr9:
-#line 343 "parser.rl"
+#line 333 "parser.rl"
{ p--; {p++; cs = 9; goto _out;} }
goto st9;
st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 987 "parser.c"
+#line 1066 "parser.c"
goto st0;
st5:
if ( ++p == pe )
@@ -1044,7 +1123,7 @@ case 7:
_out: {}
}
-#line 358 "parser.rl"
+#line 348 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1060,7 +1139,7 @@ case 7:
-#line 1064 "parser.c"
+#line 1143 "parser.c"
enum {JSON_array_start = 1};
enum {JSON_array_first_final = 17};
enum {JSON_array_error = 0};
@@ -1068,28 +1147,28 @@ enum {JSON_array_error = 0};
enum {JSON_array_en_main = 1};
-#line 401 "parser.rl"
+#line 391 "parser.rl"
-static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
+static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
{
int cs = EVIL;
VALUE array_class = json->array_class;
- if (json->max_nesting && json->current_nesting > json->max_nesting) {
- rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
+ if (json->max_nesting && current_nesting > json->max_nesting) {
+ rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
}
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
-#line 1086 "parser.c"
+#line 1165 "parser.c"
{
cs = JSON_array_start;
}
-#line 414 "parser.rl"
+#line 404 "parser.rl"
-#line 1093 "parser.c"
+#line 1172 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1128,10 +1207,10 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 378 "parser.rl"
+#line 368 "parser.rl"
{
VALUE v = Qnil;
- char *np = JSON_parse_value(json, p, pe, &v);
+ char *np = JSON_parse_value(json, p, pe, &v, current_nesting);
if (np == NULL) {
p--; {p++; cs = 3; goto _out;}
} else {
@@ -1148,7 +1227,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1152 "parser.c"
+#line 1231 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1248,14 +1327,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 393 "parser.rl"
+#line 383 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1259 "parser.c"
+#line 1338 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1311,7 +1390,7 @@ case 16:
_out: {}
}
-#line 415 "parser.rl"
+#line 405 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1392,7 +1471,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1396 "parser.c"
+#line 1475 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1400,7 +1479,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 514 "parser.rl"
+#line 504 "parser.rl"
static int
@@ -1422,15 +1501,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1426 "parser.c"
+#line 1505 "parser.c"
{
cs = JSON_string_start;
}
-#line 535 "parser.rl"
+#line 525 "parser.rl"
json->memo = p;
-#line 1434 "parser.c"
+#line 1513 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1455,7 +1534,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 500 "parser.rl"
+#line 490 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1466,14 +1545,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 511 "parser.rl"
+#line 501 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1477 "parser.c"
+#line 1556 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1549,7 +1628,7 @@ case 7:
_out: {}
}
-#line 537 "parser.rl"
+#line 527 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1564,6 +1643,8 @@ case 7:
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
+ } else {
+ rb_str_resize(*result, RSTRING_LEN(*result));
}
if (cs >= JSON_string_first_final) {
return p + 1;
@@ -1586,41 +1667,13 @@ case 7:
static VALUE convert_encoding(VALUE source)
{
- const char *ptr = RSTRING_PTR(source);
- long len = RSTRING_LEN(source);
- if (len < 2) {
- rb_raise(eParserError, "A JSON text must at least contain two octets!");
- }
#ifdef HAVE_RUBY_ENCODING_H
- {
- rb_encoding *enc = rb_enc_get(source);
- if (enc == rb_ascii8bit_encoding()) {
- if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
- source = rb_str_conv_enc(source, UTF_32BE, rb_utf8_encoding());
- } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
- source = rb_str_conv_enc(source, UTF_16BE, rb_utf8_encoding());
- } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
- source = rb_str_conv_enc(source, UTF_32LE, rb_utf8_encoding());
- } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
- source = rb_str_conv_enc(source, UTF_16LE, rb_utf8_encoding());
- } else {
- source = rb_str_dup(source);
- FORCE_UTF8(source);
- }
- } else {
- source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
- }
- }
-#else
- if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32be"), source);
- } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16be"), source);
- } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32le"), source);
- } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16le"), source);
- }
+ rb_encoding *enc = rb_enc_get(source);
+ if (enc == rb_ascii8bit_encoding()) {
+ FORCE_UTF8(source);
+ } else {
+ source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
+ }
#endif
return source;
}
@@ -1643,8 +1696,9 @@ static VALUE convert_encoding(VALUE source)
* defiance of RFC 4627 to be parsed by the Parser. This option defaults to
* false.
* * *symbolize_names*: If set to true, returns symbols for the names
- * (keys) in a JSON object. Otherwise strings are returned, which is also
- * the default.
+ * (keys) in a JSON object. Otherwise strings are returned, which is
+ * also the default. It's not possible to use this option in
+ * conjunction with the *create_additions* option.
* * *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 false.
@@ -1695,19 +1749,17 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->symbolize_names = 0;
}
- tmp = ID2SYM(i_quirks_mode);
- if (option_given_p(opts, tmp)) {
- VALUE quirks_mode = rb_hash_aref(opts, tmp);
- json->quirks_mode = RTEST(quirks_mode) ? 1 : 0;
- } else {
- json->quirks_mode = 0;
- }
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
} else {
json->create_additions = 0;
}
+ if (json->symbolize_names && json->create_additions) {
+ rb_raise(rb_eArgError,
+ "options :symbolize_names and :create_additions cannot be "
+ " used in conjunction");
+ }
tmp = ID2SYM(i_create_id);
if (option_given_p(opts, tmp)) {
json->create_id = rb_hash_aref(opts, tmp);
@@ -1744,11 +1796,8 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
json->object_class = Qnil;
json->array_class = Qnil;
}
+ source = convert_encoding(StringValue(source));
StringValue(source);
- if (!json->quirks_mode) {
- source = convert_encoding(source);
- }
- json->current_nesting = 0;
json->len = RSTRING_LEN(source);
json->source = RSTRING_PTR(source);;
json->Vsource = source;
@@ -1756,7 +1805,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1760 "parser.c"
+#line 1809 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1764,201 +1813,33 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 767 "parser.rl"
+#line 717 "parser.rl"
-static VALUE cParser_parse_strict(VALUE self)
+/*
+ * call-seq: parse()
+ *
+ * Parses the current JSON text _source_ and returns the complete data
+ * structure as a result.
+ */
+static VALUE cParser_parse(VALUE self)
{
- char *p, *pe;
- int cs = EVIL;
- VALUE result = Qnil;
- GET_PARSER;
+ char *p, *pe;
+ int cs = EVIL;
+ VALUE result = Qnil;
+ GET_PARSER;
-#line 1779 "parser.c"
+#line 1834 "parser.c"
{
cs = JSON_start;
}
-#line 777 "parser.rl"
- p = json->source;
- pe = p + json->len;
+#line 733 "parser.rl"
+ p = json->source;
+ pe = p + json->len;
-#line 1788 "parser.c"
- {
- if ( p == pe )
- goto _test_eof;
- switch ( cs )
- {
-st1:
- if ( ++p == pe )
- goto _test_eof1;
-case 1:
- switch( (*p) ) {
- case 13: goto st1;
- case 32: goto st1;
- case 47: goto st2;
- case 91: goto tr3;
- case 123: goto tr4;
- }
- if ( 9 <= (*p) && (*p) <= 10 )
- goto st1;
- goto st0;
-st0:
-cs = 0;
- goto _out;
-st2:
- if ( ++p == pe )
- goto _test_eof2;
-case 2:
- switch( (*p) ) {
- case 42: goto st3;
- case 47: goto st5;
- }
- goto st0;
-st3:
- if ( ++p == pe )
- goto _test_eof3;
-case 3:
- if ( (*p) == 42 )
- goto st4;
- goto st3;
-st4:
- if ( ++p == pe )
- goto _test_eof4;
-case 4:
- switch( (*p) ) {
- case 42: goto st4;
- case 47: goto st1;
- }
- goto st3;
-st5:
- if ( ++p == pe )
- goto _test_eof5;
-case 5:
- if ( (*p) == 10 )
- goto st1;
- goto st5;
-tr3:
-#line 756 "parser.rl"
- {
- char *np;
- json->current_nesting = 1;
- np = JSON_parse_array(json, p, pe, &result);
- if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
- }
- goto st10;
-tr4:
-#line 749 "parser.rl"
- {
- char *np;
- json->current_nesting = 1;
- np = JSON_parse_object(json, p, pe, &result);
- if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
- }
- goto st10;
-st10:
- if ( ++p == pe )
- goto _test_eof10;
-case 10:
-#line 1865 "parser.c"
- switch( (*p) ) {
- case 13: goto st10;
- case 32: goto st10;
- case 47: goto st6;
- }
- if ( 9 <= (*p) && (*p) <= 10 )
- goto st10;
- goto st0;
-st6:
- if ( ++p == pe )
- goto _test_eof6;
-case 6:
- switch( (*p) ) {
- case 42: goto st7;
- case 47: goto st9;
- }
- goto st0;
-st7:
- if ( ++p == pe )
- goto _test_eof7;
-case 7:
- if ( (*p) == 42 )
- goto st8;
- goto st7;
-st8:
- if ( ++p == pe )
- goto _test_eof8;
-case 8:
- switch( (*p) ) {
- case 42: goto st8;
- case 47: goto st10;
- }
- goto st7;
-st9:
- if ( ++p == pe )
- goto _test_eof9;
-case 9:
- if ( (*p) == 10 )
- goto st10;
- goto st9;
- }
- _test_eof1: cs = 1; goto _test_eof;
- _test_eof2: cs = 2; goto _test_eof;
- _test_eof3: cs = 3; goto _test_eof;
- _test_eof4: cs = 4; goto _test_eof;
- _test_eof5: cs = 5; goto _test_eof;
- _test_eof10: cs = 10; goto _test_eof;
- _test_eof6: cs = 6; goto _test_eof;
- _test_eof7: cs = 7; goto _test_eof;
- _test_eof8: cs = 8; goto _test_eof;
- _test_eof9: cs = 9; goto _test_eof;
-
- _test_eof: {}
- _out: {}
- }
-
-#line 780 "parser.rl"
-
- if (cs >= JSON_first_final && p == pe) {
- return result;
- } else {
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
- return Qnil;
- }
-}
-
-
-
-#line 1934 "parser.c"
-enum {JSON_quirks_mode_start = 1};
-enum {JSON_quirks_mode_first_final = 10};
-enum {JSON_quirks_mode_error = 0};
-
-enum {JSON_quirks_mode_en_main = 1};
-
-
-#line 805 "parser.rl"
-
-
-static VALUE cParser_parse_quirks_mode(VALUE self)
-{
- char *p, *pe;
- int cs = EVIL;
- VALUE result = Qnil;
- GET_PARSER;
-
-
-#line 1953 "parser.c"
- {
- cs = JSON_quirks_mode_start;
- }
-
-#line 815 "parser.rl"
- p = json->source;
- pe = p + json->len;
-
-#line 1962 "parser.c"
+#line 1843 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1992,9 +1873,9 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 797 "parser.rl"
+#line 709 "parser.rl"
{
- char *np = JSON_parse_value(json, p, pe, &result);
+ char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
}
goto st10;
@@ -2002,7 +1883,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 2006 "parser.c"
+#line 1887 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -2091,30 +1972,13 @@ case 9:
_out: {}
}
-#line 818 "parser.rl"
-
- if (cs >= JSON_quirks_mode_first_final && p == pe) {
- return result;
- } else {
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
- return Qnil;
- }
-}
+#line 736 "parser.rl"
-/*
- * call-seq: parse()
- *
- * Parses the current JSON text _source_ and returns the complete data
- * structure as a result.
- */
-static VALUE cParser_parse(VALUE self)
-{
- GET_PARSER;
-
- if (json->quirks_mode) {
- return cParser_parse_quirks_mode(self);
+ if (cs >= JSON_first_final && p == pe) {
+ return result;
} else {
- return cParser_parse_strict(self);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ return Qnil;
}
}
@@ -2172,18 +2036,6 @@ static VALUE cParser_source(VALUE self)
return rb_str_dup(json->Vsource);
}
-/*
- * call-seq: quirks_mode?()
- *
- * Returns a true, if this parser is in quirks_mode, false otherwise.
- */
-static VALUE cParser_quirks_mode_p(VALUE self)
-{
- GET_PARSER;
- return json->quirks_mode ? Qtrue : Qfalse;
-}
-
-
void Init_parser(void)
{
rb_require("json/common");
@@ -2196,7 +2048,6 @@ void Init_parser(void)
rb_define_method(cParser, "initialize", cParser_initialize, -1);
rb_define_method(cParser, "parse", cParser_parse, 0);
rb_define_method(cParser, "source", cParser_source, 0);
- rb_define_method(cParser, "quirks_mode?", cParser_quirks_mode_p, 0);
CNaN = rb_const_get(mJSON, rb_intern("NaN"));
CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
@@ -2210,7 +2061,6 @@ void Init_parser(void)
i_max_nesting = rb_intern("max_nesting");
i_allow_nan = rb_intern("allow_nan");
i_symbolize_names = rb_intern("symbolize_names");
- i_quirks_mode = rb_intern("quirks_mode");
i_object_class = rb_intern("object_class");
i_array_class = rb_intern("array_class");
i_match = rb_intern("match");
@@ -2220,15 +2070,6 @@ void Init_parser(void)
i_aset = rb_intern("[]=");
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
-#ifdef HAVE_RUBY_ENCODING_H
- UTF_8 = rb_utf8_encoding();
- UTF_16BE = rb_enc_find("utf-16be");
- UTF_16LE = rb_enc_find("utf-16le");
- UTF_32BE = rb_enc_find("utf-32be");
- UTF_32LE = rb_enc_find("utf-32le");
-#else
- i_iconv = rb_intern("iconv");
-#endif
}
/*
diff --git a/ext/json/parser/parser.h b/ext/json/parser/parser.h
index abcc2571f1..1d46831965 100644
--- a/ext/json/parser/parser.h
+++ b/ext/json/parser/parser.h
@@ -34,11 +34,9 @@ typedef struct JSON_ParserStruct {
char *memo;
VALUE create_id;
int max_nesting;
- int current_nesting;
int allow_nan;
int parsing_name;
int symbolize_names;
- int quirks_mode;
VALUE object_class;
VALUE array_class;
int create_additions;
@@ -58,11 +56,11 @@ typedef struct JSON_ParserStruct {
static UTF32 unescape_unicode(const unsigned char *p);
static int convert_UTF32_to_UTF8(char *buf, UTF32 ch);
-static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result);
-static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
+static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result);
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result);
-static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result);
+static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting);
static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd);
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
static VALUE convert_encoding(VALUE source);
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl
index 2fa0caee7a..c67634b8ba 100644
--- a/ext/json/parser/parser.rl
+++ b/ext/json/parser/parser.rl
@@ -2,7 +2,7 @@
#include "parser.h"
#if defined HAVE_RUBY_ENCODING_H
-# define EXC_ENCODING UTF_8,
+# define EXC_ENCODING rb_utf8_encoding(),
# ifndef HAVE_RB_ENC_RAISE
static void
enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
@@ -87,17 +87,11 @@ static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
return len;
}
-#ifdef HAVE_RUBY_ENCODING_H
-static rb_encoding *UTF_8, *UTF_16BE, *UTF_16LE, *UTF_32BE, *UTF_32LE;
-#else
-static ID i_iconv;
-#endif
-
static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
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_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
i_match_string, i_aset, i_aref, i_leftshift;
@@ -137,7 +131,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
action parse_value {
VALUE v = Qnil;
- char *np = JSON_parse_value(json, fpc, pe, &v);
+ char *np = JSON_parse_value(json, fpc, pe, &v, current_nesting);
if (np == NULL) {
fhold; fbreak;
} else {
@@ -170,14 +164,14 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
) @exit;
}%%
-static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
+static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
{
int cs = EVIL;
VALUE last_name = Qnil;
VALUE object_class = json->object_class;
- if (json->max_nesting && json->current_nesting > json->max_nesting) {
- rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
+ if (json->max_nesting && current_nesting > json->max_nesting) {
+ rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
}
*result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
@@ -243,7 +237,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
action parse_number {
char *np;
- if(pe > fpc + 9 - json->quirks_mode && !strncmp(MinusInfinity, fpc, 9)) {
+ if(pe > fpc + 8 && !strncmp(MinusInfinity, fpc, 9)) {
if (json->allow_nan) {
*result = CMinusInfinity;
fexec p + 10;
@@ -261,23 +255,19 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
action parse_array {
char *np;
- json->current_nesting++;
- np = JSON_parse_array(json, fpc, pe, result);
- json->current_nesting--;
+ np = JSON_parse_array(json, fpc, pe, result, current_nesting + 1);
if (np == NULL) { fhold; fbreak; } else fexec np;
}
action parse_object {
char *np;
- json->current_nesting++;
- np = JSON_parse_object(json, fpc, pe, result);
- json->current_nesting--;
+ np = JSON_parse_object(json, fpc, pe, result, current_nesting + 1);
if (np == NULL) { fhold; fbreak; } else fexec np;
}
action exit { fhold; fbreak; }
-main := (
+main := ignore* (
Vnull @parse_null |
Vfalse @parse_false |
Vtrue @parse_true |
@@ -287,10 +277,10 @@ main := (
begin_string >parse_string |
begin_array >parse_array |
begin_object >parse_object
- ) %*exit;
+ ) ignore* %*exit;
}%%
-static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
+static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
{
int cs = EVIL;
@@ -377,7 +367,7 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
action parse_value {
VALUE v = Qnil;
- char *np = JSON_parse_value(json, fpc, pe, &v);
+ char *np = JSON_parse_value(json, fpc, pe, &v, current_nesting);
if (np == NULL) {
fhold; fbreak;
} else {
@@ -400,13 +390,13 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
end_array @exit;
}%%
-static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
+static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
{
int cs = EVIL;
VALUE array_class = json->array_class;
- if (json->max_nesting && json->current_nesting > json->max_nesting) {
- rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
+ if (json->max_nesting && current_nesting > json->max_nesting) {
+ rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
}
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
@@ -548,6 +538,8 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
+ } else {
+ rb_str_resize(*result, RSTRING_LEN(*result));
}
if (cs >= JSON_string_first_final) {
return p + 1;
@@ -570,41 +562,13 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
static VALUE convert_encoding(VALUE source)
{
- const char *ptr = RSTRING_PTR(source);
- long len = RSTRING_LEN(source);
- if (len < 2) {
- rb_raise(eParserError, "A JSON text must at least contain two octets!");
- }
#ifdef HAVE_RUBY_ENCODING_H
- {
- rb_encoding *enc = rb_enc_get(source);
- if (enc == rb_ascii8bit_encoding()) {
- if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
- source = rb_str_conv_enc(source, UTF_32BE, rb_utf8_encoding());
- } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
- source = rb_str_conv_enc(source, UTF_16BE, rb_utf8_encoding());
- } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
- source = rb_str_conv_enc(source, UTF_32LE, rb_utf8_encoding());
- } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
- source = rb_str_conv_enc(source, UTF_16LE, rb_utf8_encoding());
- } else {
- source = rb_str_dup(source);
- FORCE_UTF8(source);
- }
- } else {
- source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
- }
- }
-#else
- if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32be"), source);
- } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16be"), source);
- } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32le"), source);
- } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16le"), source);
- }
+ rb_encoding *enc = rb_enc_get(source);
+ if (enc == rb_ascii8bit_encoding()) {
+ FORCE_UTF8(source);
+ } else {
+ source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
+ }
#endif
return source;
}
@@ -627,8 +591,9 @@ static VALUE convert_encoding(VALUE source)
* defiance of RFC 4627 to be parsed by the Parser. This option defaults to
* false.
* * *symbolize_names*: If set to true, returns symbols for the names
- * (keys) in a JSON object. Otherwise strings are returned, which is also
- * the default.
+ * (keys) in a JSON object. Otherwise strings are returned, which is
+ * also the default. It's not possible to use this option in
+ * conjunction with the *create_additions* option.
* * *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 false.
@@ -679,19 +644,17 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->symbolize_names = 0;
}
- tmp = ID2SYM(i_quirks_mode);
- if (option_given_p(opts, tmp)) {
- VALUE quirks_mode = rb_hash_aref(opts, tmp);
- json->quirks_mode = RTEST(quirks_mode) ? 1 : 0;
- } else {
- json->quirks_mode = 0;
- }
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
} else {
json->create_additions = 0;
}
+ if (json->symbolize_names && json->create_additions) {
+ rb_raise(rb_eArgError,
+ "options :symbolize_names and :create_additions cannot be "
+ " used in conjunction");
+ }
tmp = ID2SYM(i_create_id);
if (option_given_p(opts, tmp)) {
json->create_id = rb_hash_aref(opts, tmp);
@@ -728,11 +691,8 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
json->object_class = Qnil;
json->array_class = Qnil;
}
+ source = convert_encoding(StringValue(source));
StringValue(source);
- if (!json->quirks_mode) {
- source = convert_encoding(source);
- }
- json->current_nesting = 0;
json->len = RSTRING_LEN(source);
json->source = RSTRING_PTR(source);;
json->Vsource = source;
@@ -746,56 +706,8 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
include JSON_common;
- action parse_object {
- char *np;
- json->current_nesting = 1;
- np = JSON_parse_object(json, fpc, pe, &result);
- if (np == NULL) { fhold; fbreak; } else fexec np;
- }
-
- action parse_array {
- char *np;
- json->current_nesting = 1;
- np = JSON_parse_array(json, fpc, pe, &result);
- if (np == NULL) { fhold; fbreak; } else fexec np;
- }
-
- main := ignore* (
- begin_object >parse_object |
- begin_array >parse_array
- ) ignore*;
-}%%
-
-static VALUE cParser_parse_strict(VALUE self)
-{
- char *p, *pe;
- int cs = EVIL;
- VALUE result = Qnil;
- GET_PARSER;
-
- %% write init;
- p = json->source;
- pe = p + json->len;
- %% write exec;
-
- if (cs >= JSON_first_final && p == pe) {
- return result;
- } else {
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
- return Qnil;
- }
-}
-
-
-%%{
- machine JSON_quirks_mode;
-
- write data;
-
- include JSON_common;
-
action parse_value {
- char *np = JSON_parse_value(json, fpc, pe, &result);
+ char *np = JSON_parse_value(json, fpc, pe, &result, 0);
if (np == NULL) { fhold; fbreak; } else fexec np;
}
@@ -804,26 +716,6 @@ static VALUE cParser_parse_strict(VALUE self)
) ignore*;
}%%
-static VALUE cParser_parse_quirks_mode(VALUE self)
-{
- char *p, *pe;
- int cs = EVIL;
- VALUE result = Qnil;
- GET_PARSER;
-
- %% write init;
- p = json->source;
- pe = p + json->len;
- %% write exec;
-
- if (cs >= JSON_quirks_mode_first_final && p == pe) {
- return result;
- } else {
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
- return Qnil;
- }
-}
-
/*
* call-seq: parse()
*
@@ -832,12 +724,21 @@ static VALUE cParser_parse_quirks_mode(VALUE self)
*/
static VALUE cParser_parse(VALUE self)
{
+ char *p, *pe;
+ int cs = EVIL;
+ VALUE result = Qnil;
GET_PARSER;
- if (json->quirks_mode) {
- return cParser_parse_quirks_mode(self);
+ %% write init;
+ p = json->source;
+ pe = p + json->len;
+ %% write exec;
+
+ if (cs >= JSON_first_final && p == pe) {
+ return result;
} else {
- return cParser_parse_strict(self);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ return Qnil;
}
}
@@ -895,18 +796,6 @@ static VALUE cParser_source(VALUE self)
return rb_str_dup(json->Vsource);
}
-/*
- * call-seq: quirks_mode?()
- *
- * Returns a true, if this parser is in quirks_mode, false otherwise.
- */
-static VALUE cParser_quirks_mode_p(VALUE self)
-{
- GET_PARSER;
- return json->quirks_mode ? Qtrue : Qfalse;
-}
-
-
void Init_parser(void)
{
rb_require("json/common");
@@ -919,7 +808,6 @@ void Init_parser(void)
rb_define_method(cParser, "initialize", cParser_initialize, -1);
rb_define_method(cParser, "parse", cParser_parse, 0);
rb_define_method(cParser, "source", cParser_source, 0);
- rb_define_method(cParser, "quirks_mode?", cParser_quirks_mode_p, 0);
CNaN = rb_const_get(mJSON, rb_intern("NaN"));
CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
@@ -933,7 +821,6 @@ void Init_parser(void)
i_max_nesting = rb_intern("max_nesting");
i_allow_nan = rb_intern("allow_nan");
i_symbolize_names = rb_intern("symbolize_names");
- i_quirks_mode = rb_intern("quirks_mode");
i_object_class = rb_intern("object_class");
i_array_class = rb_intern("array_class");
i_match = rb_intern("match");
@@ -943,15 +830,6 @@ void Init_parser(void)
i_aset = rb_intern("[]=");
i_aref = rb_intern("[]");
i_leftshift = rb_intern("<<");
-#ifdef HAVE_RUBY_ENCODING_H
- UTF_8 = rb_utf8_encoding();
- UTF_16BE = rb_enc_find("utf-16be");
- UTF_16LE = rb_enc_find("utf-16le");
- UTF_32BE = rb_enc_find("utf-32be");
- UTF_32LE = rb_enc_find("utf-32le");
-#else
- i_iconv = rb_intern("iconv");
-#endif
}
/*
diff --git a/ext/json/parser/prereq.mk b/ext/json/parser/prereq.mk
deleted file mode 100644
index be7bcb4319..0000000000
--- a/ext/json/parser/prereq.mk
+++ /dev/null
@@ -1,10 +0,0 @@
-RAGEL = ragel
-
-.SUFFIXES: .rl
-
-.rl.c:
- $(RAGEL) -G2 $<
- $(BASERUBY) -pli -e '$$_.sub!(/[ \t]+$$/, "")' \
- -e '$$_.sub!(/^static const int (JSON_.*=.*);$$/, "enum {\\1};")' $@
-
-parser.c: