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>
|
||||
|
||||
* 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,
|
||||
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_respond_to_p, i_match, i_keys, i_depth, i_dup;
|
||||
i_quirks_mode, 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_dup;
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
fbuffer_inc_capa(fb, 1);
|
||||
|
@ -688,6 +698,8 @@ 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;
|
||||
}
|
||||
|
||||
|
@ -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_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));
|
||||
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)
|
||||
{
|
||||
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)
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
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)
|
||||
|
@ -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)) {
|
||||
tmp = rb_funcall(obj, i_to_json, 1, Vstate);
|
||||
Check_Type(tmp, T_STRING);
|
||||
fbuffer_append(buffer, RSTRING_PAIR(tmp));
|
||||
fbuffer_append_str(buffer, tmp);
|
||||
} else {
|
||||
tmp = rb_funcall(obj, i_to_s, 0);
|
||||
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 re, args[2];
|
||||
GET_STATE(self);
|
||||
if (!state->quirks_mode) {
|
||||
args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
|
||||
args[1] = CRegexp_MULTILINE;
|
||||
re = rb_class_new_instance(2, args, rb_cRegexp);
|
||||
if (NIL_P(rb_funcall(re, i_match, 1, result))) {
|
||||
rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1287,6 +1303,29 @@ static VALUE cState_ascii_only_p(VALUE self)
|
|||
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
|
||||
*
|
||||
|
@ -1345,6 +1384,9 @@ void Init_generator()
|
|||
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, "configure", cState_configure, 1);
|
||||
|
@ -1392,6 +1434,7 @@ void Init_generator()
|
|||
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_pack = rb_intern("pack");
|
||||
i_unpack = rb_intern("unpack");
|
||||
|
|
|
@ -45,7 +45,10 @@
|
|||
#define RSTRING_LEN(string) RSTRING(string)->len
|
||||
#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 */
|
||||
|
||||
|
@ -123,6 +126,7 @@ typedef struct JSON_Generator_StateStruct {
|
|||
long max_nesting;
|
||||
char allow_nan;
|
||||
char ascii_only;
|
||||
char quirks_mode;
|
||||
long depth;
|
||||
} JSON_Generator_State;
|
||||
|
||||
|
|
|
@ -7,13 +7,13 @@
|
|||
#
|
||||
# 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.
|
||||
# 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
|
||||
#
|
||||
# == 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:
|
||||
#
|
||||
# require 'json'
|
||||
|
@ -42,8 +42,8 @@
|
|||
# puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
|
||||
#
|
||||
# <tt>JSON.generate</tt> only allows objects or arrays to be converted
|
||||
# to JSON syntax. While <tt>to_json</tt> accepts many Ruby classes
|
||||
# even though it only acts a method for serialization:
|
||||
# to JSON syntax. <tt>to_json</tt>, however, accepts many Ruby classes
|
||||
# even though it acts only as a method for serialization:
|
||||
#
|
||||
# require 'json'
|
||||
#
|
||||
|
|
|
@ -5,6 +5,8 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|||
require 'json'
|
||||
end
|
||||
require 'date'
|
||||
require 'complex'
|
||||
require 'rational'
|
||||
|
||||
# Symbol serialization/deserialization
|
||||
class Symbol
|
||||
|
@ -241,3 +243,39 @@ class Regexp
|
|||
as_json.to_json
|
||||
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
|
||||
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
|
||||
# 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.
|
||||
def [](object, opts = {})
|
||||
if object.respond_to? :to_str
|
||||
|
@ -16,7 +16,7 @@ module JSON
|
|||
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.
|
||||
attr_reader :parser
|
||||
|
||||
|
@ -28,7 +28,7 @@ module JSON
|
|||
end
|
||||
|
||||
# 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
|
||||
# the given path, an ArgumentError is raised.
|
||||
def deep_const_get(path) # :nodoc:
|
||||
|
@ -81,15 +81,15 @@ module JSON
|
|||
$VERBOSE = old
|
||||
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.
|
||||
attr_reader :generator
|
||||
|
||||
# Returns the JSON generator state class, that is used by JSON. This might
|
||||
# be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
||||
# Returns the JSON generator state class that is used by JSON. This is
|
||||
# either JSON::Ext::Generator::State or JSON::Pure::Generator::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'.
|
||||
attr_accessor :create_id
|
||||
end
|
||||
|
@ -104,10 +104,10 @@ module JSON
|
|||
# The base exception for JSON errors.
|
||||
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
|
||||
|
||||
# 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.
|
||||
class NestingError < ParserError; end
|
||||
|
||||
|
@ -115,13 +115,13 @@ module JSON
|
|||
class CircularDatastructure < NestingError; end
|
||||
# :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
|
||||
# For backwards compatibility
|
||||
UnparserError = GeneratorError
|
||||
|
||||
# This exception is raised, if the required unicode support is missing on the
|
||||
# system. Usually this means, that the iconv library is not installed.
|
||||
# This exception is raised if the required unicode support is missing on the
|
||||
# system. Usually this means that the iconv library is not installed.
|
||||
class MissingUnicodeSupport < JSONError; end
|
||||
|
||||
module_function
|
||||
|
@ -131,16 +131,16 @@ 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
|
||||
# structures. Disable depth checking with :max_nesting => false. It defaults
|
||||
# to 19.
|
||||
# * *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
|
||||
# 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
|
||||
# (keys) in a JSON object. Otherwise strings are returned. Strings are
|
||||
# the default.
|
||||
# * *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.
|
||||
# * *object_class*: Defaults to Hash
|
||||
# * *array_class*: Defaults to Array
|
||||
|
@ -149,19 +149,19 @@ module JSON
|
|||
end
|
||||
|
||||
# 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.
|
||||
#
|
||||
# _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,
|
||||
# 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
|
||||
# to true.
|
||||
# * *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.
|
||||
def parse!(source, opts = {})
|
||||
opts = {
|
||||
|
@ -188,7 +188,7 @@ module JSON
|
|||
# * *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: ''),
|
||||
# * *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.
|
||||
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
||||
# 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
|
||||
# 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)
|
||||
if State === opts
|
||||
state, opts = opts, nil
|
||||
else
|
||||
state = SAFE_STATE_PROTOTYPE.dup
|
||||
end
|
||||
if opts
|
||||
if opts.respond_to? :to_hash
|
||||
opts = opts.to_hash
|
||||
|
@ -223,9 +227,13 @@ module JSON
|
|||
# This method disables the checks for circles in Ruby objects.
|
||||
#
|
||||
# *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)
|
||||
if State === opts
|
||||
state, opts = opts, nil
|
||||
else
|
||||
state = FAST_STATE_PROTOTYPE.dup
|
||||
end
|
||||
if opts
|
||||
if opts.respond_to? :to_hash
|
||||
opts = opts.to_hash
|
||||
|
@ -249,10 +257,14 @@ module JSON
|
|||
# The returned document is a prettier form of the document returned by
|
||||
# #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.
|
||||
def pretty_generate(obj, opts = nil)
|
||||
if State === opts
|
||||
state, opts = opts, nil
|
||||
else
|
||||
state = PRETTY_STATE_PROTOTYPE.dup
|
||||
end
|
||||
if opts
|
||||
if opts.respond_to? :to_hash
|
||||
opts = opts.to_hash
|
||||
|
@ -273,7 +285,7 @@ module JSON
|
|||
# :startdoc:
|
||||
|
||||
# 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
|
||||
# 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
|
||||
# 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.
|
||||
#
|
||||
# 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
|
||||
# same!) to the _limit_ argument in Marshal.dump.
|
||||
#
|
||||
|
@ -396,11 +408,11 @@ module ::Kernel
|
|||
nil
|
||||
end
|
||||
|
||||
# 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
|
||||
# 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
|
||||
# The _opts_ argument is passed through to generate/parse respectively. See
|
||||
# generate and parse for their documentation.
|
||||
def JSON(object, *args)
|
||||
if object.respond_to? :to_str
|
||||
|
@ -413,10 +425,10 @@ end
|
|||
|
||||
# Extends any Class to include _json_creatable?_ method.
|
||||
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
|
||||
# method _json_create_ that expects a hash as first parameter, which includes
|
||||
# the required data.
|
||||
# method _json_create_ that expects a hash as first parameter. The hash
|
||||
# should include the required data.
|
||||
def json_creatable?
|
||||
respond_to?(:json_create)
|
||||
end
|
||||
|
|
|
@ -4,21 +4,8 @@ module JSON
|
|||
# This module holds all the modules/classes that implement JSON's
|
||||
# functionality as C extensions.
|
||||
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/generator'
|
||||
end
|
||||
rescue LoadError
|
||||
require 'json/ext/parser'
|
||||
require 'json/ext/generator'
|
||||
end
|
||||
$DEBUG and warn "Using Ext extension for JSON."
|
||||
JSON.parser = Parser
|
||||
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 parsing_name;
|
||||
int symbolize_names;
|
||||
int quirks_mode;
|
||||
VALUE object_class;
|
||||
VALUE array_class;
|
||||
int create_additions;
|
||||
|
|
|
@ -76,8 +76,9 @@ 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_object_class,
|
||||
i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift;
|
||||
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_quirks_mode,
|
||||
i_object_class, i_array_class, i_key_p, i_deep_const_get, i_match,
|
||||
i_match_string, i_aset, i_leftshift;
|
||||
|
||||
%%{
|
||||
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; }
|
||||
|
||||
a_pair = ignore* begin_name >parse_name
|
||||
ignore* name_separator ignore*
|
||||
begin_value >parse_value;
|
||||
pair = ignore* begin_name >parse_name ignore* name_separator ignore* begin_value >parse_value;
|
||||
next_pair = ignore* value_separator pair;
|
||||
|
||||
main := begin_object
|
||||
(a_pair (ignore* value_separator a_pair)*)?
|
||||
ignore* end_object @exit;
|
||||
main := (
|
||||
begin_object
|
||||
(pair (next_pair)*)? ignore*
|
||||
end_object
|
||||
) @exit;
|
||||
}%%
|
||||
|
||||
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;
|
||||
include JSON_common;
|
||||
|
@ -214,7 +217,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
|
|||
|
||||
action parse_number {
|
||||
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) {
|
||||
*result = CMinusInfinity;
|
||||
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; }
|
||||
|
||||
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)
|
||||
|
@ -313,7 +316,7 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
|
|||
main := '-'? (
|
||||
(('0' | [1-9][0-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)
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -630,8 +605,6 @@ static VALUE convert_encoding(VALUE source)
|
|||
*/
|
||||
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
char *ptr;
|
||||
long len;
|
||||
VALUE source, opts;
|
||||
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_scan_args(argc, argv, "11", &source, &opts);
|
||||
source = convert_encoding(StringValue(source));
|
||||
ptr = RSTRING_PTR(source);
|
||||
len = RSTRING_LEN(source);
|
||||
if (!NIL_P(opts)) {
|
||||
opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
|
||||
if (NIL_P(opts)) {
|
||||
|
@ -671,6 +641,13 @@ 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));
|
||||
|
@ -711,20 +688,44 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
|
|||
json->object_class = Qnil;
|
||||
json->array_class = Qnil;
|
||||
}
|
||||
if (!json->quirks_mode) {
|
||||
source = convert_encoding(StringValue(source));
|
||||
}
|
||||
json->current_nesting = 0;
|
||||
json->len = len;
|
||||
json->source = ptr;
|
||||
json->len = RSTRING_LEN(source);
|
||||
json->source = RSTRING_PTR(source);;
|
||||
json->Vsource = source;
|
||||
return 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)
|
||||
%%{
|
||||
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*;
|
||||
}%%
|
||||
|
||||
static VALUE cParser_parse_strict(VALUE self)
|
||||
{
|
||||
char *p, *pe;
|
||||
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()
|
||||
{
|
||||
JSON_Parser *json = ALLOC(JSON_Parser);
|
||||
|
@ -783,6 +840,18 @@ 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()
|
||||
{
|
||||
rb_require("json/common");
|
||||
|
@ -795,6 +864,7 @@ void Init_parser()
|
|||
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"));
|
||||
|
@ -808,6 +878,7 @@ void Init_parser()
|
|||
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");
|
||||
|
|
|
@ -104,6 +104,42 @@ class TC_JSON < Test::Unit::TestCase
|
|||
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
||||
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)
|
||||
def test_parse_more_complex_arrays
|
||||
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))
|
||||
assert_equal d, JSON.parse(d.to_json)
|
||||
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
|
||||
|
|
|
@ -50,6 +50,7 @@ EOT
|
|||
parsed_json = parse(json)
|
||||
assert_equal({"1"=>2}, parsed_json)
|
||||
assert_raise(GeneratorError) { generate(666) }
|
||||
assert_equal '666', generate(666, :quirks_mode => true)
|
||||
end
|
||||
|
||||
def test_generate_pretty
|
||||
|
@ -67,6 +68,7 @@ EOT
|
|||
parsed_json = parse(json)
|
||||
assert_equal({"1"=>2}, parsed_json)
|
||||
assert_raise(GeneratorError) { pretty_generate(666) }
|
||||
assert_equal '666', pretty_generate(666, :quirks_mode => true)
|
||||
end
|
||||
|
||||
def test_fast_generate
|
||||
|
@ -79,9 +81,24 @@ EOT
|
|||
parsed_json = parse(json)
|
||||
assert_equal({"1"=>2}, parsed_json)
|
||||
assert_raise(GeneratorError) { fast_generate(666) }
|
||||
assert_equal '666', fast_generate(666, :quirks_mode => true)
|
||||
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
|
||||
json = generate({1=>2}, nil)
|
||||
|
@ -107,6 +124,7 @@ EOT
|
|||
:allow_nan => false,
|
||||
:array_nl => "\n",
|
||||
:ascii_only => false,
|
||||
:quirks_mode => false,
|
||||
:depth => 0,
|
||||
:indent => " ",
|
||||
:max_nesting => 19,
|
||||
|
@ -122,6 +140,7 @@ EOT
|
|||
:allow_nan => false,
|
||||
:array_nl => "",
|
||||
:ascii_only => false,
|
||||
:quirks_mode => false,
|
||||
:depth => 0,
|
||||
:indent => "",
|
||||
:max_nesting => 19,
|
||||
|
@ -137,6 +156,7 @@ EOT
|
|||
:allow_nan => false,
|
||||
:array_nl => "",
|
||||
:ascii_only => false,
|
||||
:quirks_mode => false,
|
||||
:depth => 0,
|
||||
:indent => "",
|
||||
:max_nesting => 0,
|
||||
|
@ -177,4 +197,17 @@ EOT
|
|||
assert_raises(JSON::NestingError) { ary.to_json(s) }
|
||||
assert_equal 19, s.depth
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue