mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/json: Merge json gem 1.5.4+ (2149f4185c598fb97db1).
[Bug #5173] [ruby-core:38866] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33122 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2dd9d721ed
commit
b14c060dda
13 changed files with 728 additions and 288 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Tue Aug 30 11:06:19 2011 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* ext/json: Merge json gem 1.5.4+ (2149f4185c598fb97db1).
|
||||||
|
[Bug #5173] [ruby-core:38866]
|
||||||
|
|
||||||
Tue Aug 30 09:57:50 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
|
Tue Aug 30 09:57:50 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
|
||||||
|
|
||||||
* lib/thread.rb (Queue#pop): fix a race against Thread.wakeup.
|
* lib/thread.rb (Queue#pop): fix a race against Thread.wakeup.
|
||||||
|
|
|
@ -13,8 +13,8 @@ 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,
|
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_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
|
||||||
i_pack, i_unpack, i_create_id, i_extend, i_key_p, i_aref, i_send,
|
i_quirks_mode, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
|
||||||
i_respond_to_p, i_match, i_keys, i_depth, i_dup;
|
i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth, i_dup;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright 2001-2004 Unicode, Inc.
|
* Copyright 2001-2004 Unicode, Inc.
|
||||||
|
@ -349,6 +349,16 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
||||||
|
{
|
||||||
|
const char *newstr = StringValuePtr(str);
|
||||||
|
unsigned long len = RSTRING_LEN(str);
|
||||||
|
|
||||||
|
RB_GC_GUARD(str);
|
||||||
|
|
||||||
|
fbuffer_append(fb, newstr, len);
|
||||||
|
}
|
||||||
|
|
||||||
static void fbuffer_append_char(FBuffer *fb, char newchr)
|
static void fbuffer_append_char(FBuffer *fb, char newchr)
|
||||||
{
|
{
|
||||||
fbuffer_inc_capa(fb, 1);
|
fbuffer_inc_capa(fb, 1);
|
||||||
|
@ -688,6 +698,8 @@ static VALUE cState_configure(VALUE self, VALUE opts)
|
||||||
state->allow_nan = RTEST(tmp);
|
state->allow_nan = RTEST(tmp);
|
||||||
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
|
tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
|
||||||
state->ascii_only = RTEST(tmp);
|
state->ascii_only = RTEST(tmp);
|
||||||
|
tmp = rb_hash_aref(opts, ID2SYM(i_quirks_mode));
|
||||||
|
state->quirks_mode = RTEST(tmp);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -708,6 +720,7 @@ 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_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_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_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_max_nesting), LONG2FIX(state->max_nesting));
|
||||||
rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
|
rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
|
||||||
return result;
|
return result;
|
||||||
|
@ -852,7 +865,7 @@ static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
|
||||||
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE tmp = rb_funcall(obj, i_to_s, 0);
|
VALUE tmp = rb_funcall(obj, i_to_s, 0);
|
||||||
fbuffer_append(buffer, RSTRING_PAIR(tmp));
|
fbuffer_append_str(buffer, tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
||||||
|
@ -869,7 +882,7 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
|
||||||
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
|
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fbuffer_append(buffer, RSTRING_PAIR(tmp));
|
fbuffer_append_str(buffer, tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
|
||||||
|
@ -897,7 +910,7 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
|
||||||
} else if (rb_respond_to(obj, i_to_json)) {
|
} else if (rb_respond_to(obj, i_to_json)) {
|
||||||
tmp = rb_funcall(obj, i_to_json, 1, Vstate);
|
tmp = rb_funcall(obj, i_to_json, 1, Vstate);
|
||||||
Check_Type(tmp, T_STRING);
|
Check_Type(tmp, T_STRING);
|
||||||
fbuffer_append(buffer, RSTRING_PAIR(tmp));
|
fbuffer_append_str(buffer, tmp);
|
||||||
} else {
|
} else {
|
||||||
tmp = rb_funcall(obj, i_to_s, 0);
|
tmp = rb_funcall(obj, i_to_s, 0);
|
||||||
Check_Type(tmp, T_STRING);
|
Check_Type(tmp, T_STRING);
|
||||||
|
@ -961,12 +974,15 @@ static VALUE cState_generate(VALUE self, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE result = cState_partial_generate(self, obj);
|
VALUE result = cState_partial_generate(self, obj);
|
||||||
VALUE re, args[2];
|
VALUE re, args[2];
|
||||||
|
GET_STATE(self);
|
||||||
|
if (!state->quirks_mode) {
|
||||||
args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
|
args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
|
||||||
args[1] = CRegexp_MULTILINE;
|
args[1] = CRegexp_MULTILINE;
|
||||||
re = rb_class_new_instance(2, args, rb_cRegexp);
|
re = rb_class_new_instance(2, args, rb_cRegexp);
|
||||||
if (NIL_P(rb_funcall(re, i_match, 1, result))) {
|
if (NIL_P(rb_funcall(re, i_match, 1, result))) {
|
||||||
rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
|
rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1287,6 +1303,29 @@ static VALUE cState_ascii_only_p(VALUE self)
|
||||||
return state->ascii_only ? Qtrue : Qfalse;
|
return state->ascii_only ? Qtrue : Qfalse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
* call-seq: depth
|
||||||
*
|
*
|
||||||
|
@ -1345,6 +1384,9 @@ void Init_generator()
|
||||||
rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
|
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, "allow_nan?", cState_allow_nan_p, 0);
|
||||||
rb_define_method(cState, "ascii_only?", cState_ascii_only_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, 0);
|
||||||
rb_define_method(cState, "depth=", cState_depth_set, 1);
|
rb_define_method(cState, "depth=", cState_depth_set, 1);
|
||||||
rb_define_method(cState, "configure", cState_configure, 1);
|
rb_define_method(cState, "configure", cState_configure, 1);
|
||||||
|
@ -1392,6 +1434,7 @@ void Init_generator()
|
||||||
i_max_nesting = rb_intern("max_nesting");
|
i_max_nesting = rb_intern("max_nesting");
|
||||||
i_allow_nan = rb_intern("allow_nan");
|
i_allow_nan = rb_intern("allow_nan");
|
||||||
i_ascii_only = rb_intern("ascii_only");
|
i_ascii_only = rb_intern("ascii_only");
|
||||||
|
i_quirks_mode = rb_intern("quirks_mode");
|
||||||
i_depth = rb_intern("depth");
|
i_depth = rb_intern("depth");
|
||||||
i_pack = rb_intern("pack");
|
i_pack = rb_intern("pack");
|
||||||
i_unpack = rb_intern("unpack");
|
i_unpack = rb_intern("unpack");
|
||||||
|
|
|
@ -45,7 +45,10 @@
|
||||||
#define RSTRING_LEN(string) RSTRING(string)->len
|
#define RSTRING_LEN(string) RSTRING(string)->len
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define RSTRING_PAIR(string) RSTRING_PTR(string), RSTRING_LEN(string)
|
/* We don't need to guard objects for rbx, so let's do nothing at all. */
|
||||||
|
#ifndef RB_GC_GUARD
|
||||||
|
#define RB_GC_GUARD(object)
|
||||||
|
#endif
|
||||||
|
|
||||||
/* fbuffer implementation */
|
/* fbuffer implementation */
|
||||||
|
|
||||||
|
@ -123,6 +126,7 @@ typedef struct JSON_Generator_StateStruct {
|
||||||
long max_nesting;
|
long max_nesting;
|
||||||
char allow_nan;
|
char allow_nan;
|
||||||
char ascii_only;
|
char ascii_only;
|
||||||
|
char quirks_mode;
|
||||||
long depth;
|
long depth;
|
||||||
} JSON_Generator_State;
|
} JSON_Generator_State;
|
||||||
|
|
||||||
|
|
|
@ -7,13 +7,13 @@
|
||||||
#
|
#
|
||||||
# Built on two universally available structures:
|
# Built on two universally available structures:
|
||||||
# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
|
# 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
|
||||||
# 2. An orderd list of values. More commonly named as an _array_, vector, sequence, or list.
|
# 2. An ordered list of values. More commonly called an _array_, vector, sequence or list.
|
||||||
#
|
#
|
||||||
# To read more about JSON visit: http://json.org
|
# To read more about JSON visit: http://json.org
|
||||||
#
|
#
|
||||||
# == Parsing JSON
|
# == Parsing JSON
|
||||||
#
|
#
|
||||||
# To parse a JSON string received by another application, or generated within
|
# To parse a JSON string received by another application or generated within
|
||||||
# your existing application:
|
# your existing application:
|
||||||
#
|
#
|
||||||
# require 'json'
|
# require 'json'
|
||||||
|
@ -42,8 +42,8 @@
|
||||||
# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
|
# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
|
||||||
#
|
#
|
||||||
# <tt>JSON.generate</tt> only allows objects or arrays to be converted
|
# <tt>JSON.generate</tt> only allows objects or arrays to be converted
|
||||||
# to JSON syntax. While <tt>to_json</tt> accepts many Ruby classes
|
# to JSON syntax. <tt>to_json</tt>, however, accepts many Ruby classes
|
||||||
# even though it only acts a method for serialization:
|
# even though it acts only as a method for serialization:
|
||||||
#
|
#
|
||||||
# require 'json'
|
# require 'json'
|
||||||
#
|
#
|
||||||
|
|
|
@ -5,6 +5,8 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
||||||
require 'json'
|
require 'json'
|
||||||
end
|
end
|
||||||
require 'date'
|
require 'date'
|
||||||
|
require 'complex'
|
||||||
|
require 'rational'
|
||||||
|
|
||||||
# Symbol serialization/deserialization
|
# Symbol serialization/deserialization
|
||||||
class Symbol
|
class Symbol
|
||||||
|
@ -241,3 +243,39 @@ class Regexp
|
||||||
as_json.to_json
|
as_json.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Rational
|
||||||
|
def self.json_create(object)
|
||||||
|
Rational(object['n'], object['d'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def as_json(*)
|
||||||
|
{
|
||||||
|
JSON.create_id => self.class.name,
|
||||||
|
'n' => numerator,
|
||||||
|
'd' => denominator,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_json(*)
|
||||||
|
as_json.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Complex
|
||||||
|
def self.json_create(object)
|
||||||
|
Complex(object['r'], object['i'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def as_json(*)
|
||||||
|
{
|
||||||
|
JSON.create_id => self.class.name,
|
||||||
|
'r' => real,
|
||||||
|
'i' => imag,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_json(*)
|
||||||
|
as_json.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -2,11 +2,11 @@ require 'json/version'
|
||||||
|
|
||||||
module JSON
|
module JSON
|
||||||
class << self
|
class << self
|
||||||
# If _object_ is string-like parse the string and return the parsed result
|
# 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
|
# as a Ruby data structure. Otherwise generate a JSON text from the Ruby
|
||||||
# data structure object and return it.
|
# data structure object and return it.
|
||||||
#
|
#
|
||||||
# The _opts_ argument is passed through to generate/parse respectively, see
|
# The _opts_ argument is passed through to generate/parse respectively. See
|
||||||
# generate and parse for their documentation.
|
# generate and parse for their documentation.
|
||||||
def [](object, opts = {})
|
def [](object, opts = {})
|
||||||
if object.respond_to? :to_str
|
if object.respond_to? :to_str
|
||||||
|
@ -16,7 +16,7 @@ module JSON
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the JSON parser class, that is used by JSON. This might be either
|
# Returns the JSON parser class that is used by JSON. This is either
|
||||||
# JSON::Ext::Parser or JSON::Pure::Parser.
|
# JSON::Ext::Parser or JSON::Pure::Parser.
|
||||||
attr_reader :parser
|
attr_reader :parser
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ module JSON
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the constant located at _path_. The format of _path_ has to be
|
# Return the constant located at _path_. The format of _path_ has to be
|
||||||
# either ::A::B::C or A::B::C. In any case A has to be located at the top
|
# either ::A::B::C or A::B::C. In any case, A has to be located at the top
|
||||||
# level (absolute namespace path?). If there doesn't exist a constant at
|
# level (absolute namespace path?). If there doesn't exist a constant at
|
||||||
# the given path, an ArgumentError is raised.
|
# the given path, an ArgumentError is raised.
|
||||||
def deep_const_get(path) # :nodoc:
|
def deep_const_get(path) # :nodoc:
|
||||||
|
@ -81,15 +81,15 @@ module JSON
|
||||||
$VERBOSE = old
|
$VERBOSE = old
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the JSON generator modul, that is used by JSON. This might be
|
# Returns the JSON generator module that is used by JSON. This is
|
||||||
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
||||||
attr_reader :generator
|
attr_reader :generator
|
||||||
|
|
||||||
# Returns the JSON generator state class, that is used by JSON. This might
|
# Returns the JSON generator state class that is used by JSON. This is
|
||||||
# be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
||||||
attr_accessor :state
|
attr_accessor :state
|
||||||
|
|
||||||
# This is create identifier, that is used to decide, if the _json_create_
|
# This is create identifier, which is used to decide if the _json_create_
|
||||||
# hook of a class should be called. It defaults to 'json_class'.
|
# hook of a class should be called. It defaults to 'json_class'.
|
||||||
attr_accessor :create_id
|
attr_accessor :create_id
|
||||||
end
|
end
|
||||||
|
@ -104,10 +104,10 @@ module JSON
|
||||||
# The base exception for JSON errors.
|
# The base exception for JSON errors.
|
||||||
class JSONError < StandardError; end
|
class JSONError < StandardError; end
|
||||||
|
|
||||||
# This exception is raised, if a parser error occurs.
|
# This exception is raised if a parser error occurs.
|
||||||
class ParserError < JSONError; end
|
class ParserError < JSONError; end
|
||||||
|
|
||||||
# This exception is raised, if the nesting of parsed datastructures is too
|
# This exception is raised if the nesting of parsed data structures is too
|
||||||
# deep.
|
# deep.
|
||||||
class NestingError < ParserError; end
|
class NestingError < ParserError; end
|
||||||
|
|
||||||
|
@ -115,13 +115,13 @@ module JSON
|
||||||
class CircularDatastructure < NestingError; end
|
class CircularDatastructure < NestingError; end
|
||||||
# :startdoc:
|
# :startdoc:
|
||||||
|
|
||||||
# This exception is raised, if a generator or unparser error occurs.
|
# This exception is raised if a generator or unparser error occurs.
|
||||||
class GeneratorError < JSONError; end
|
class GeneratorError < JSONError; end
|
||||||
# For backwards compatibility
|
# For backwards compatibility
|
||||||
UnparserError = GeneratorError
|
UnparserError = GeneratorError
|
||||||
|
|
||||||
# This exception is raised, if the required unicode support is missing on the
|
# This exception is raised if the required unicode support is missing on the
|
||||||
# system. Usually this means, that the iconv library is not installed.
|
# system. Usually this means that the iconv library is not installed.
|
||||||
class MissingUnicodeSupport < JSONError; end
|
class MissingUnicodeSupport < JSONError; end
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
@ -131,16 +131,16 @@ module JSON
|
||||||
# _opts_ can have the following
|
# _opts_ can have the following
|
||||||
# keys:
|
# keys:
|
||||||
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
||||||
# structures. Disable depth checking with :max_nesting => false, it defaults
|
# structures. Disable depth checking with :max_nesting => false. It defaults
|
||||||
# to 19.
|
# to 19.
|
||||||
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
# * *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 4627 to be parsed by the Parser. This option defaults
|
||||||
# to false.
|
# to false.
|
||||||
# * *symbolize_names*: If set to true, returns symbols for the names
|
# * *symbolize_names*: If set to true, returns symbols for the names
|
||||||
# (keys) in a JSON object. Otherwise strings are returned, which is also
|
# (keys) in a JSON object. Otherwise strings are returned. Strings are
|
||||||
# the default.
|
# the default.
|
||||||
# * *create_additions*: If set to false, the Parser doesn't create
|
# * *create_additions*: If set to false, the Parser doesn't create
|
||||||
# additions even if a matchin class and create_id was found. This option
|
# additions even if a matching class and create_id was found. This option
|
||||||
# defaults to true.
|
# defaults to true.
|
||||||
# * *object_class*: Defaults to Hash
|
# * *object_class*: Defaults to Hash
|
||||||
# * *array_class*: Defaults to Array
|
# * *array_class*: Defaults to Array
|
||||||
|
@ -149,19 +149,19 @@ module JSON
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
||||||
# The bang version of the parse method, defaults to the more dangerous values
|
# The bang version of the parse method defaults to the more dangerous values
|
||||||
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
||||||
#
|
#
|
||||||
# _opts_ can have the following keys:
|
# _opts_ can have the following keys:
|
||||||
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
||||||
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
|
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
|
||||||
# methods defaults to not doing max depth checking: This can be dangerous,
|
# methods defaults to not doing max depth checking: This can be dangerous
|
||||||
# if someone wants to fill up your stack.
|
# if someone wants to fill up your stack.
|
||||||
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
|
# * *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 4627 to be parsed by the Parser. This option defaults
|
||||||
# to true.
|
# to true.
|
||||||
# * *create_additions*: If set to false, the Parser doesn't create
|
# * *create_additions*: If set to false, the Parser doesn't create
|
||||||
# additions even if a matchin class and create_id was found. This option
|
# additions even if a matching class and create_id was found. This option
|
||||||
# defaults to true.
|
# defaults to true.
|
||||||
def parse!(source, opts = {})
|
def parse!(source, opts = {})
|
||||||
opts = {
|
opts = {
|
||||||
|
@ -188,7 +188,7 @@ module JSON
|
||||||
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
||||||
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
||||||
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
||||||
# generated, otherwise an exception is thrown, if these values are
|
# generated, otherwise an exception is thrown if these values are
|
||||||
# encountered. This options defaults to false.
|
# encountered. This options defaults to false.
|
||||||
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
||||||
# structures from which JSON is to be generated. Disable depth checking
|
# structures from which JSON is to be generated. Disable depth checking
|
||||||
|
@ -196,9 +196,13 @@ module JSON
|
||||||
#
|
#
|
||||||
# See also the fast_generate for the fastest creation method with the least
|
# See also the fast_generate for the fastest creation method with the least
|
||||||
# amount of sanity checks, and the pretty_generate method for some
|
# amount of sanity checks, and the pretty_generate method for some
|
||||||
# defaults for a pretty output.
|
# defaults for pretty output.
|
||||||
def generate(obj, opts = nil)
|
def generate(obj, opts = nil)
|
||||||
|
if State === opts
|
||||||
|
state, opts = opts, nil
|
||||||
|
else
|
||||||
state = SAFE_STATE_PROTOTYPE.dup
|
state = SAFE_STATE_PROTOTYPE.dup
|
||||||
|
end
|
||||||
if opts
|
if opts
|
||||||
if opts.respond_to? :to_hash
|
if opts.respond_to? :to_hash
|
||||||
opts = opts.to_hash
|
opts = opts.to_hash
|
||||||
|
@ -223,9 +227,13 @@ module JSON
|
||||||
# This method disables the checks for circles in Ruby objects.
|
# This method disables the checks for circles in Ruby objects.
|
||||||
#
|
#
|
||||||
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
||||||
# _obj_ argument, because this will cause JSON to go into an infinite loop.
|
# _obj_ argument because this will cause JSON to go into an infinite loop.
|
||||||
def fast_generate(obj, opts = nil)
|
def fast_generate(obj, opts = nil)
|
||||||
|
if State === opts
|
||||||
|
state, opts = opts, nil
|
||||||
|
else
|
||||||
state = FAST_STATE_PROTOTYPE.dup
|
state = FAST_STATE_PROTOTYPE.dup
|
||||||
|
end
|
||||||
if opts
|
if opts
|
||||||
if opts.respond_to? :to_hash
|
if opts.respond_to? :to_hash
|
||||||
opts = opts.to_hash
|
opts = opts.to_hash
|
||||||
|
@ -249,10 +257,14 @@ module JSON
|
||||||
# The returned document is a prettier form of the document returned by
|
# The returned document is a prettier form of the document returned by
|
||||||
# #unparse.
|
# #unparse.
|
||||||
#
|
#
|
||||||
# The _opts_ argument can be used to configure the generator, see the
|
# The _opts_ argument can be used to configure the generator. See the
|
||||||
# generate method for a more detailed explanation.
|
# generate method for a more detailed explanation.
|
||||||
def pretty_generate(obj, opts = nil)
|
def pretty_generate(obj, opts = nil)
|
||||||
|
if State === opts
|
||||||
|
state, opts = opts, nil
|
||||||
|
else
|
||||||
state = PRETTY_STATE_PROTOTYPE.dup
|
state = PRETTY_STATE_PROTOTYPE.dup
|
||||||
|
end
|
||||||
if opts
|
if opts
|
||||||
if opts.respond_to? :to_hash
|
if opts.respond_to? :to_hash
|
||||||
opts = opts.to_hash
|
opts = opts.to_hash
|
||||||
|
@ -273,7 +285,7 @@ module JSON
|
||||||
# :startdoc:
|
# :startdoc:
|
||||||
|
|
||||||
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
||||||
# either be a string-like object, an IO like object, or an object responding
|
# either be a string-like object, an IO-like object, or an object responding
|
||||||
# to the read method. If _proc_ was given, it will be called with any nested
|
# to the read method. If _proc_ was given, it will be called with any nested
|
||||||
# Ruby object as an argument recursively in depth first order.
|
# Ruby object as an argument recursively in depth first order.
|
||||||
#
|
#
|
||||||
|
@ -312,10 +324,10 @@ module JSON
|
||||||
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
||||||
# the result.
|
# the result.
|
||||||
#
|
#
|
||||||
# If anIO (an IO like object or an object that responds to the write method)
|
# If anIO (an IO-like object or an object that responds to the write method)
|
||||||
# was given, the resulting JSON is written to it.
|
# was given, the resulting JSON is written to it.
|
||||||
#
|
#
|
||||||
# If the number of nested arrays or objects exceeds _limit_ an ArgumentError
|
# If the number of nested arrays or objects exceeds _limit_, an ArgumentError
|
||||||
# exception is raised. This argument is similar (but not exactly the
|
# exception is raised. This argument is similar (but not exactly the
|
||||||
# same!) to the _limit_ argument in Marshal.dump.
|
# same!) to the _limit_ argument in Marshal.dump.
|
||||||
#
|
#
|
||||||
|
@ -396,11 +408,11 @@ module ::Kernel
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# If _object_ is string-like parse the string and return the parsed result as
|
# 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
|
# a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
|
||||||
# structure object and return it.
|
# structure object and return it.
|
||||||
#
|
#
|
||||||
# The _opts_ argument is passed through to generate/parse respectively, see
|
# The _opts_ argument is passed through to generate/parse respectively. See
|
||||||
# generate and parse for their documentation.
|
# generate and parse for their documentation.
|
||||||
def JSON(object, *args)
|
def JSON(object, *args)
|
||||||
if object.respond_to? :to_str
|
if object.respond_to? :to_str
|
||||||
|
@ -413,10 +425,10 @@ end
|
||||||
|
|
||||||
# Extends any Class to include _json_creatable?_ method.
|
# Extends any Class to include _json_creatable?_ method.
|
||||||
class ::Class
|
class ::Class
|
||||||
# Returns true, if this class can be used to create an instance
|
# Returns true if this class can be used to create an instance
|
||||||
# from a serialised JSON string. The class has to implement a class
|
# from a serialised JSON string. The class has to implement a class
|
||||||
# method _json_create_ that expects a hash as first parameter, which includes
|
# method _json_create_ that expects a hash as first parameter. The hash
|
||||||
# the required data.
|
# should include the required data.
|
||||||
def json_creatable?
|
def json_creatable?
|
||||||
respond_to?(:json_create)
|
respond_to?(:json_create)
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,21 +4,8 @@ module JSON
|
||||||
# This module holds all the modules/classes that implement JSON's
|
# This module holds all the modules/classes that implement JSON's
|
||||||
# functionality as C extensions.
|
# functionality as C extensions.
|
||||||
module Ext
|
module Ext
|
||||||
begin
|
|
||||||
if defined?(RUBY_ENGINE) == 'constant' and RUBY_ENGINE == 'ruby' and RUBY_VERSION =~ /\A1\.9\./
|
|
||||||
require 'json/ext/1.9/parser'
|
|
||||||
require 'json/ext/1.9/generator'
|
|
||||||
elsif !defined?(RUBY_ENGINE) && RUBY_VERSION =~ /\A1\.8\./
|
|
||||||
require 'json/ext/1.8/parser'
|
|
||||||
require 'json/ext/1.8/generator'
|
|
||||||
else
|
|
||||||
require 'json/ext/parser'
|
require 'json/ext/parser'
|
||||||
require 'json/ext/generator'
|
require 'json/ext/generator'
|
||||||
end
|
|
||||||
rescue LoadError
|
|
||||||
require 'json/ext/parser'
|
|
||||||
require 'json/ext/generator'
|
|
||||||
end
|
|
||||||
$DEBUG and warn "Using Ext extension for JSON."
|
$DEBUG and warn "Using Ext extension for JSON."
|
||||||
JSON.parser = Parser
|
JSON.parser = Parser
|
||||||
JSON.generator = Generator
|
JSON.generator = Generator
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -44,6 +44,7 @@ typedef struct JSON_ParserStruct {
|
||||||
int allow_nan;
|
int allow_nan;
|
||||||
int parsing_name;
|
int parsing_name;
|
||||||
int symbolize_names;
|
int symbolize_names;
|
||||||
|
int quirks_mode;
|
||||||
VALUE object_class;
|
VALUE object_class;
|
||||||
VALUE array_class;
|
VALUE array_class;
|
||||||
int create_additions;
|
int create_additions;
|
||||||
|
|
|
@ -76,8 +76,9 @@ static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
|
||||||
static VALUE CNaN, CInfinity, CMinusInfinity;
|
static VALUE CNaN, CInfinity, CMinusInfinity;
|
||||||
|
|
||||||
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
|
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_object_class,
|
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode,
|
||||||
i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift;
|
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
|
||||||
|
i_match_string, i_aset, i_leftshift;
|
||||||
|
|
||||||
%%{
|
%%{
|
||||||
machine JSON_common;
|
machine JSON_common;
|
||||||
|
@ -138,13 +139,14 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
|
||||||
|
|
||||||
action exit { fhold; fbreak; }
|
action exit { fhold; fbreak; }
|
||||||
|
|
||||||
a_pair = ignore* begin_name >parse_name
|
pair = ignore* begin_name >parse_name ignore* name_separator ignore* begin_value >parse_value;
|
||||||
ignore* name_separator ignore*
|
next_pair = ignore* value_separator pair;
|
||||||
begin_value >parse_value;
|
|
||||||
|
|
||||||
main := begin_object
|
main := (
|
||||||
(a_pair (ignore* value_separator a_pair)*)?
|
begin_object
|
||||||
ignore* end_object @exit;
|
(pair (next_pair)*)? ignore*
|
||||||
|
end_object
|
||||||
|
) @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)
|
||||||
|
@ -178,6 +180,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
%%{
|
%%{
|
||||||
machine JSON_value;
|
machine JSON_value;
|
||||||
include JSON_common;
|
include JSON_common;
|
||||||
|
@ -214,7 +217,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
|
||||||
|
|
||||||
action parse_number {
|
action parse_number {
|
||||||
char *np;
|
char *np;
|
||||||
if(pe > fpc + 9 && !strncmp(MinusInfinity, fpc, 9)) {
|
if(pe > fpc + 9 - json->quirks_mode && !strncmp(MinusInfinity, fpc, 9)) {
|
||||||
if (json->allow_nan) {
|
if (json->allow_nan) {
|
||||||
*result = CMinusInfinity;
|
*result = CMinusInfinity;
|
||||||
fexec p + 10;
|
fexec p + 10;
|
||||||
|
@ -282,7 +285,7 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
|
||||||
|
|
||||||
action exit { fhold; fbreak; }
|
action exit { fhold; fbreak; }
|
||||||
|
|
||||||
main := '-'? ('0' | [1-9][0-9]*) (^[0-9] @exit);
|
main := '-'? ('0' | [1-9][0-9]*) (^[0-9]? @exit);
|
||||||
}%%
|
}%%
|
||||||
|
|
||||||
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
|
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
|
||||||
|
@ -313,7 +316,7 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
|
||||||
main := '-'? (
|
main := '-'? (
|
||||||
(('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?)
|
(('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?)
|
||||||
| (('0' | [1-9][0-9]*) ([Ee] [+\-]?[0-9]+))
|
| (('0' | [1-9][0-9]*) ([Ee] [+\-]?[0-9]+))
|
||||||
) (^[0-9Ee.\-] @exit );
|
) (^[0-9Ee.\-]? @exit );
|
||||||
}%%
|
}%%
|
||||||
|
|
||||||
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
|
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
|
||||||
|
@ -521,34 +524,6 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
%%{
|
|
||||||
machine JSON;
|
|
||||||
|
|
||||||
write data;
|
|
||||||
|
|
||||||
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*;
|
|
||||||
}%%
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Document-class: JSON::Ext::Parser
|
* Document-class: JSON::Ext::Parser
|
||||||
*
|
*
|
||||||
|
@ -630,8 +605,6 @@ static VALUE convert_encoding(VALUE source)
|
||||||
*/
|
*/
|
||||||
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
||||||
{
|
{
|
||||||
char *ptr;
|
|
||||||
long len;
|
|
||||||
VALUE source, opts;
|
VALUE source, opts;
|
||||||
GET_PARSER_INIT;
|
GET_PARSER_INIT;
|
||||||
|
|
||||||
|
@ -639,9 +612,6 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
||||||
rb_raise(rb_eTypeError, "already initialized instance");
|
rb_raise(rb_eTypeError, "already initialized instance");
|
||||||
}
|
}
|
||||||
rb_scan_args(argc, argv, "11", &source, &opts);
|
rb_scan_args(argc, argv, "11", &source, &opts);
|
||||||
source = convert_encoding(StringValue(source));
|
|
||||||
ptr = RSTRING_PTR(source);
|
|
||||||
len = RSTRING_LEN(source);
|
|
||||||
if (!NIL_P(opts)) {
|
if (!NIL_P(opts)) {
|
||||||
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
|
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
|
||||||
if (NIL_P(opts)) {
|
if (NIL_P(opts)) {
|
||||||
|
@ -671,6 +641,13 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
||||||
} else {
|
} else {
|
||||||
json->symbolize_names = 0;
|
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);
|
tmp = ID2SYM(i_create_additions);
|
||||||
if (option_given_p(opts, tmp)) {
|
if (option_given_p(opts, tmp)) {
|
||||||
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
|
json->create_additions = RTEST(rb_hash_aref(opts, tmp));
|
||||||
|
@ -711,20 +688,44 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
||||||
json->object_class = Qnil;
|
json->object_class = Qnil;
|
||||||
json->array_class = Qnil;
|
json->array_class = Qnil;
|
||||||
}
|
}
|
||||||
|
if (!json->quirks_mode) {
|
||||||
|
source = convert_encoding(StringValue(source));
|
||||||
|
}
|
||||||
json->current_nesting = 0;
|
json->current_nesting = 0;
|
||||||
json->len = len;
|
json->len = RSTRING_LEN(source);
|
||||||
json->source = ptr;
|
json->source = RSTRING_PTR(source);;
|
||||||
json->Vsource = source;
|
json->Vsource = source;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
%%{
|
||||||
* call-seq: parse()
|
machine JSON;
|
||||||
*
|
|
||||||
* Parses the current JSON text _source_ and returns the complete data
|
write data;
|
||||||
* structure as a result.
|
|
||||||
*/
|
include JSON_common;
|
||||||
static VALUE cParser_parse(VALUE self)
|
|
||||||
|
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;
|
char *p, *pe;
|
||||||
int cs = EVIL;
|
int cs = EVIL;
|
||||||
|
@ -744,6 +745,62 @@ static VALUE cParser_parse(VALUE self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
%%{
|
||||||
|
machine JSON_quirks_mode;
|
||||||
|
|
||||||
|
write data;
|
||||||
|
|
||||||
|
include JSON_common;
|
||||||
|
|
||||||
|
action parse_value {
|
||||||
|
char *np = JSON_parse_value(json, fpc, pe, &result);
|
||||||
|
if (np == NULL) { fhold; fbreak; } else fexec np;
|
||||||
|
}
|
||||||
|
|
||||||
|
main := ignore* (
|
||||||
|
begin_value >parse_value
|
||||||
|
) 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_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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);
|
||||||
|
} else {
|
||||||
|
return cParser_parse_strict(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static JSON_Parser *JSON_allocate()
|
static JSON_Parser *JSON_allocate()
|
||||||
{
|
{
|
||||||
JSON_Parser *json = ALLOC(JSON_Parser);
|
JSON_Parser *json = ALLOC(JSON_Parser);
|
||||||
|
@ -783,6 +840,18 @@ static VALUE cParser_source(VALUE self)
|
||||||
return rb_str_dup(json->Vsource);
|
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 Init_parser()
|
||||||
{
|
{
|
||||||
rb_require("json/common");
|
rb_require("json/common");
|
||||||
|
@ -795,6 +864,7 @@ void Init_parser()
|
||||||
rb_define_method(cParser, "initialize", cParser_initialize, -1);
|
rb_define_method(cParser, "initialize", cParser_initialize, -1);
|
||||||
rb_define_method(cParser, "parse", cParser_parse, 0);
|
rb_define_method(cParser, "parse", cParser_parse, 0);
|
||||||
rb_define_method(cParser, "source", cParser_source, 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"));
|
CNaN = rb_const_get(mJSON, rb_intern("NaN"));
|
||||||
CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
|
CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
|
||||||
|
@ -808,6 +878,7 @@ void Init_parser()
|
||||||
i_max_nesting = rb_intern("max_nesting");
|
i_max_nesting = rb_intern("max_nesting");
|
||||||
i_allow_nan = rb_intern("allow_nan");
|
i_allow_nan = rb_intern("allow_nan");
|
||||||
i_symbolize_names = rb_intern("symbolize_names");
|
i_symbolize_names = rb_intern("symbolize_names");
|
||||||
|
i_quirks_mode = rb_intern("quirks_mode");
|
||||||
i_object_class = rb_intern("object_class");
|
i_object_class = rb_intern("object_class");
|
||||||
i_array_class = rb_intern("array_class");
|
i_array_class = rb_intern("array_class");
|
||||||
i_match = rb_intern("match");
|
i_match = rb_intern("match");
|
||||||
|
|
|
@ -104,6 +104,42 @@ class TC_JSON < Test::Unit::TestCase
|
||||||
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_parse_json_primitive_values
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('') }
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) }
|
||||||
|
parser = JSON::Parser.new('null')
|
||||||
|
assert_equal false, parser.quirks_mode?
|
||||||
|
assert_raise(JSON::ParserError) { parser.parse }
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('null') }
|
||||||
|
assert_equal nil, JSON.parse('null', :quirks_mode => true)
|
||||||
|
parser = JSON::Parser.new('null', :quirks_mode => true)
|
||||||
|
assert_equal true, parser.quirks_mode?
|
||||||
|
assert_equal nil, parser.parse
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('false') }
|
||||||
|
assert_equal false, JSON.parse('false', :quirks_mode => true)
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('true') }
|
||||||
|
assert_equal true, JSON.parse('true', :quirks_mode => true)
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('23') }
|
||||||
|
assert_equal 23, JSON.parse('23', :quirks_mode => true)
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('1') }
|
||||||
|
assert_equal 1, JSON.parse('1', :quirks_mode => true)
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('3.141') }
|
||||||
|
assert_in_delta 3.141, JSON.parse('3.141', :quirks_mode => true), 1E-3
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('18446744073709551616') }
|
||||||
|
assert_equal 2 ** 64, JSON.parse('18446744073709551616', :quirks_mode => true)
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('"foo"') }
|
||||||
|
assert_equal 'foo', JSON.parse('"foo"', :quirks_mode => true)
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('NaN', :allow_nan => true) }
|
||||||
|
assert JSON.parse('NaN', :quirks_mode => true, :allow_nan => true).nan?
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('Infinity', :allow_nan => true) }
|
||||||
|
assert JSON.parse('Infinity', :quirks_mode => true, :allow_nan => true).infinite?
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('-Infinity', :allow_nan => true) }
|
||||||
|
assert JSON.parse('-Infinity', :quirks_mode => true, :allow_nan => true).infinite?
|
||||||
|
assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]', :quirks_mode => true) }
|
||||||
|
end
|
||||||
|
|
||||||
if Array.method_defined?(:permutation)
|
if Array.method_defined?(:permutation)
|
||||||
def test_parse_more_complex_arrays
|
def test_parse_more_complex_arrays
|
||||||
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
||||||
|
|
|
@ -164,4 +164,9 @@ class TC_JSONAddition < Test::Unit::TestCase
|
||||||
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
|
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
|
||||||
assert_equal d, JSON.parse(d.to_json)
|
assert_equal d, JSON.parse(d.to_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_rational_complex
|
||||||
|
assert_equal Rational(2, 9), JSON(JSON(Rational(2, 9)))
|
||||||
|
assert_equal Complex(2, 9), JSON(JSON(Complex(2, 9)))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -50,6 +50,7 @@ EOT
|
||||||
parsed_json = parse(json)
|
parsed_json = parse(json)
|
||||||
assert_equal({"1"=>2}, parsed_json)
|
assert_equal({"1"=>2}, parsed_json)
|
||||||
assert_raise(GeneratorError) { generate(666) }
|
assert_raise(GeneratorError) { generate(666) }
|
||||||
|
assert_equal '666', generate(666, :quirks_mode => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_generate_pretty
|
def test_generate_pretty
|
||||||
|
@ -67,6 +68,7 @@ EOT
|
||||||
parsed_json = parse(json)
|
parsed_json = parse(json)
|
||||||
assert_equal({"1"=>2}, parsed_json)
|
assert_equal({"1"=>2}, parsed_json)
|
||||||
assert_raise(GeneratorError) { pretty_generate(666) }
|
assert_raise(GeneratorError) { pretty_generate(666) }
|
||||||
|
assert_equal '666', pretty_generate(666, :quirks_mode => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fast_generate
|
def test_fast_generate
|
||||||
|
@ -79,9 +81,24 @@ EOT
|
||||||
parsed_json = parse(json)
|
parsed_json = parse(json)
|
||||||
assert_equal({"1"=>2}, parsed_json)
|
assert_equal({"1"=>2}, parsed_json)
|
||||||
assert_raise(GeneratorError) { fast_generate(666) }
|
assert_raise(GeneratorError) { fast_generate(666) }
|
||||||
|
assert_equal '666', fast_generate(666, :quirks_mode => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_own_state
|
||||||
|
state = State.new
|
||||||
|
json = generate(@hash, state)
|
||||||
|
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
||||||
|
parsed_json = parse(json)
|
||||||
|
assert_equal(@hash, parsed_json)
|
||||||
|
json = generate({1=>2}, state)
|
||||||
|
assert_equal('{"1":2}', json)
|
||||||
|
parsed_json = parse(json)
|
||||||
|
assert_equal({"1"=>2}, parsed_json)
|
||||||
|
assert_raise(GeneratorError) { generate(666, state) }
|
||||||
|
state.quirks_mode = true
|
||||||
|
assert state.quirks_mode?
|
||||||
|
assert_equal '666', generate(666, state)
|
||||||
|
end
|
||||||
|
|
||||||
def test_states
|
def test_states
|
||||||
json = generate({1=>2}, nil)
|
json = generate({1=>2}, nil)
|
||||||
|
@ -107,6 +124,7 @@ EOT
|
||||||
:allow_nan => false,
|
:allow_nan => false,
|
||||||
:array_nl => "\n",
|
:array_nl => "\n",
|
||||||
:ascii_only => false,
|
:ascii_only => false,
|
||||||
|
:quirks_mode => false,
|
||||||
:depth => 0,
|
:depth => 0,
|
||||||
:indent => " ",
|
:indent => " ",
|
||||||
:max_nesting => 19,
|
:max_nesting => 19,
|
||||||
|
@ -122,6 +140,7 @@ EOT
|
||||||
:allow_nan => false,
|
:allow_nan => false,
|
||||||
:array_nl => "",
|
:array_nl => "",
|
||||||
:ascii_only => false,
|
:ascii_only => false,
|
||||||
|
:quirks_mode => false,
|
||||||
:depth => 0,
|
:depth => 0,
|
||||||
:indent => "",
|
:indent => "",
|
||||||
:max_nesting => 19,
|
:max_nesting => 19,
|
||||||
|
@ -137,6 +156,7 @@ EOT
|
||||||
:allow_nan => false,
|
:allow_nan => false,
|
||||||
:array_nl => "",
|
:array_nl => "",
|
||||||
:ascii_only => false,
|
:ascii_only => false,
|
||||||
|
:quirks_mode => false,
|
||||||
:depth => 0,
|
:depth => 0,
|
||||||
:indent => "",
|
:indent => "",
|
||||||
:max_nesting => 0,
|
:max_nesting => 0,
|
||||||
|
@ -177,4 +197,17 @@ EOT
|
||||||
assert_raises(JSON::NestingError) { ary.to_json(s) }
|
assert_raises(JSON::NestingError) { ary.to_json(s) }
|
||||||
assert_equal 19, s.depth
|
assert_equal 19, s.depth
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_gc
|
||||||
|
bignum_too_long_to_embed_as_string = 1234567890123456789012345
|
||||||
|
expect = bignum_too_long_to_embed_as_string.to_s
|
||||||
|
stress, GC.stress = GC.stress, true
|
||||||
|
|
||||||
|
10.times do |i|
|
||||||
|
tmp = bignum_too_long_to_embed_as_string.to_json
|
||||||
|
assert_equal expect, tmp
|
||||||
|
end
|
||||||
|
ensure
|
||||||
|
GC.stress = stress
|
||||||
|
end if GC.respond_to?(:stress=)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue