summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-10 08:01:04 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-10 08:01:04 +0000
commita2e497d5ede45bd4f4a57f494027020d7bd1733b (patch)
tree9773233b59c71615a1b88fd5807b30d2e0f09345
parenta119b9d146fea877acc1e9ba5df0702163ce917a (diff)
* ext/json: Merge json gem 1.5.4+ (f7f78896607b6f6226cd).
[Bug #4700] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/json/generator/generator.c2
-rw-r--r--ext/json/generator/generator.h6
-rw-r--r--ext/json/lib/json.rb52
-rw-r--r--ext/json/lib/json/add/core.rb147
-rw-r--r--ext/json/lib/json/common.rb31
-rw-r--r--ext/json/lib/json/ext.rb19
-rw-r--r--ext/json/lib/json/version.rb2
-rw-r--r--ext/json/parser/extconf.rb7
-rw-r--r--ext/json/parser/parser.c208
-rw-r--r--ext/json/parser/parser.h15
-rw-r--r--ext/json/parser/parser.rl72
-rw-r--r--test/json/setup_variant.rb11
-rwxr-xr-xtest/json/test_json.rb60
-rwxr-xr-xtest/json/test_json_addition.rb15
-rw-r--r--test/json/test_json_encoding.rb6
-rwxr-xr-xtest/json/test_json_fixtures.rb17
-rwxr-xr-xtest/json/test_json_generate.rb7
-rw-r--r--test/json/test_json_string_matching.rb40
-rwxr-xr-xtest/json/test_json_unicode.rb6
20 files changed, 537 insertions, 191 deletions
diff --git a/ChangeLog b/ChangeLog
index 62b5874..54025b3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Jul 10 15:58:12 2011 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * ext/json: Merge json gem 1.5.4+ (f7f78896607b6f6226cd).
+ [Bug #4700]
+
Sun Jul 10 16:41:32 2011 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
* vm_core.h (typedef struct rb_vm_struct): create a new
diff --git a/ext/json/generator/generator.c b/ext/json/generator/generator.c
index c72ab61..4e44178 100644
--- a/ext/json/generator/generator.c
+++ b/ext/json/generator/generator.c
@@ -556,6 +556,7 @@ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self)
/*
* call-seq: to_json(*)
*
+ * Returns a JSON string for nil: 'null'.
*/
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self)
{
@@ -1347,6 +1348,7 @@ void Init_generator()
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);
+ rb_define_alias(cState, "merge", "configure");
rb_define_method(cState, "to_h", cState_to_h, 0);
rb_define_method(cState, "[]", cState_aref, 1);
rb_define_method(cState, "generate", cState_generate, 1);
diff --git a/ext/json/generator/generator.h b/ext/json/generator/generator.h
index b31218a..ee496fe 100644
--- a/ext/json/generator/generator.h
+++ b/ext/json/generator/generator.h
@@ -78,9 +78,9 @@ static VALUE fbuffer_to_s(FBuffer *fb);
#define UNI_STRICT_CONVERSION 1
-typedef unsigned long UTF32; /* at least 32 bits */
-typedef unsigned short UTF16; /* at least 16 bits */
-typedef unsigned char UTF8; /* typically 8 bits */
+typedef unsigned long UTF32; /* at least 32 bits */
+typedef unsigned short UTF16; /* at least 16 bits */
+typedef unsigned char UTF8; /* typically 8 bits */
#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
#define UNI_MAX_BMP (UTF32)0x0000FFFF
diff --git a/ext/json/lib/json.rb b/ext/json/lib/json.rb
index 789b0de..d7bc1a2 100644
--- a/ext/json/lib/json.rb
+++ b/ext/json/lib/json.rb
@@ -1,3 +1,55 @@
+##
+# = JavaScript Object Notation (JSON)
+#
+# JSON is a lightweight data-interchange format. It is easy for us
+# humans to read and write. Plus, equally simple for machines to generate or parse.
+# JSON is completely language agnostic, making it the ideal interchange format.
+#
+# 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.
+#
+# To read more about JSON visit: http://json.org
+#
+# == Parsing JSON
+#
+# To parse a JSON string received by another application, or generated within
+# your existing application:
+#
+# require 'json'
+#
+# my_hash = JSON.parse('{"hello": "goodbye"}')
+# puts my_hash["hello"] => "goodbye"
+#
+# Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
+# the argument to be a string and can't convert objects like a hash or array.
+#
+# Ruby converts your string into a hash
+#
+# == Generating JSON
+#
+# Creating a JSON string for communication or serialization is
+# just as simple.
+#
+# require 'json'
+#
+# my_hash = {:hello => "goodbye"}
+# puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
+#
+# Or an alternative way:
+#
+# require 'json'
+# 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:
+#
+# require 'json'
+#
+# 1.to_json => "1"
+#
+
require 'json/common'
module JSON
require 'json/version'
diff --git a/ext/json/lib/json/add/core.rb b/ext/json/lib/json/add/core.rb
index 03a00dd..e9850af 100644
--- a/ext/json/lib/json/add/core.rb
+++ b/ext/json/lib/json/add/core.rb
@@ -1,26 +1,37 @@
# This file contains implementations of ruby core's custom objects for
# serialisation/deserialisation.
-unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
- ::JSON::JSON_LOADED
+unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
require 'json'
end
require 'date'
+# Symbol serialization/deserialization
class Symbol
- def to_json(*a)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
- 's' => to_s,
- }.to_json(*a)
+ 's' => to_s,
+ }
end
+ # Stores class name (Symbol) with String representation of Symbol as a JSON string.
+ def to_json(*a)
+ as_json.to_json(*a)
+ end
+
+ # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
def self.json_create(o)
o['s'].to_sym
end
end
+# Time serialization/deserialization
class Time
+
+ # Deserializes JSON string by converting time since epoch to Time
def self.json_create(object)
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
object['n'] = usec * 1000
@@ -32,34 +43,59 @@ class Time
end
end
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
- 's' => tv_sec,
- 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
- }.to_json(*args)
+ 's' => tv_sec,
+ 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
+ }
+ end
+
+ # Stores class name (Time) with number of seconds since epoch and number of
+ # microseconds for Time as JSON string
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
+# Date serialization/deserialization
class Date
+
+ # Deserializes JSON string by converting Julian year <tt>y</tt>, month
+ # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
def self.json_create(object)
civil(*object.values_at('y', 'm', 'd', 'sg'))
end
alias start sg unless method_defined?(:start)
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
'y' => year,
'm' => month,
'd' => day,
'sg' => start,
- }.to_json(*args)
+ }
+ end
+
+ # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
+ # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
+# DateTime serialization/deserialization
class DateTime
+
+ # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
+ # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
+ # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
def self.json_create(object)
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
of_a, of_b = object['of'].split('/')
@@ -74,7 +110,9 @@ class DateTime
alias start sg unless method_defined?(:start)
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
'y' => year,
@@ -85,64 +123,121 @@ class DateTime
'S' => sec,
'of' => offset.to_s,
'sg' => start,
- }.to_json(*args)
+ }
+ end
+
+ # Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
+ # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
+ # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
+# Range serialization/deserialization
class Range
+
+ # Deserializes JSON string by constructing new Range object with arguments
+ # <tt>a</tt> serialized by <tt>to_json</tt>.
def self.json_create(object)
new(*object['a'])
end
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
- JSON.create_id => self.class.name,
- 'a' => [ first, last, exclude_end? ]
- }.to_json(*args)
+ JSON.create_id => self.class.name,
+ 'a' => [ first, last, exclude_end? ]
+ }
+ end
+
+ # Stores class name (Range) with JSON array of arguments <tt>a</tt> which
+ # include <tt>first</tt> (integer), <tt>last</tt> (integer), and
+ # <tt>exclude_end?</tt> (boolean) as JSON string.
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
+# Struct serialization/deserialization
class Struct
+
+ # Deserializes JSON string by constructing new Struct object with values
+ # <tt>v</tt> serialized by <tt>to_json</tt>.
def self.json_create(object)
new(*object['v'])
end
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
klass = self.class.name
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
{
JSON.create_id => klass,
- 'v' => values,
- }.to_json(*args)
+ 'v' => values,
+ }
+ end
+
+ # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string.
+ # Only named structs are supported.
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
+# Exception serialization/deserialization
class Exception
+
+ # Deserializes JSON string by constructing new Exception object with message
+ # <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
def self.json_create(object)
result = new(object['m'])
result.set_backtrace object['b']
result
end
- def to_json(*args)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
- 'm' => message,
- 'b' => backtrace,
- }.to_json(*args)
+ 'm' => message,
+ 'b' => backtrace,
+ }
+ end
+
+ # Stores class name (Exception) with message <tt>m</tt> and backtrace array
+ # <tt>b</tt> as JSON string
+ def to_json(*args)
+ as_json.to_json(*args)
end
end
+# Regexp serialization/deserialization
class Regexp
+
+ # Deserializes JSON string by constructing new Regexp object with source
+ # <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by
+ # <tt>to_json</tt>
def self.json_create(object)
new(object['s'], object['o'])
end
- def to_json(*)
+ # Returns a hash, that will be turned into a JSON object and represent this
+ # object.
+ def as_json(*)
{
JSON.create_id => self.class.name,
'o' => options,
's' => source,
- }.to_json
+ }
+ end
+
+ # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
+ # (Regexp or String) as JSON string
+ def to_json(*)
+ as_json.to_json
end
end
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index dd9fef8..1a5f048 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/lib/json/common.rb
@@ -23,7 +23,7 @@ module JSON
# Set the JSON parser class _parser_ to be used by JSON.
def parser=(parser) # :nodoc:
@parser = parser
- remove_const :Parser if const_defined? :Parser
+ remove_const :Parser if JSON.const_defined_in?(self, :Parser)
const_set :Parser, parser
end
@@ -34,8 +34,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 p.const_defined?(c) then p.const_get(c)
+ when c.empty? then p
+ when JSON.const_defined_in?(p, c) then p.const_get(c)
else
begin
p.const_missing(c)
@@ -292,6 +292,7 @@ module JSON
result
end
+ # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
def recurse_proc(result, &proc)
case result
when Array
@@ -340,17 +341,38 @@ 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
+
# Shortuct for iconv.
- if String.method_defined?(:encode)
+ 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
+ end
end
module ::Kernel
@@ -389,6 +411,7 @@ module ::Kernel
end
end
+# Extends any Class to include _json_creatable?_ method.
class ::Class
# Returns true, if this class can be used to create an instance
# from a serialised JSON string. The class has to implement a class
diff --git a/ext/json/lib/json/ext.rb b/ext/json/lib/json/ext.rb
index a5e3148..1fbc3fd 100644
--- a/ext/json/lib/json/ext.rb
+++ b/ext/json/lib/json/ext.rb
@@ -4,12 +4,25 @@ module JSON
# This module holds all the modules/classes that implement JSON's
# functionality as C extensions.
module Ext
- require 'json/ext/parser'
- require 'json/ext/generator'
+ 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
end
- JSON_LOADED = true unless const_defined?(:JSON_LOADED)
+ JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
end
diff --git a/ext/json/lib/json/version.rb b/ext/json/lib/json/version.rb
index beff08b..2175ac0 100644
--- a/ext/json/lib/json/version.rb
+++ b/ext/json/lib/json/version.rb
@@ -1,6 +1,6 @@
module JSON
# JSON version
- VERSION = '1.5.0'
+ VERSION = '1.5.4'
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/extconf.rb b/ext/json/parser/extconf.rb
index 64d1716..4a10dd2 100644
--- a/ext/json/parser/extconf.rb
+++ b/ext/json/parser/extconf.rb
@@ -1,5 +1,10 @@
require 'mkmf'
require 'rbconfig'
-have_header("re.h")
+if RUBY_VERSION < "1.9"
+ have_header("re.h")
+else
+ have_header("ruby/re.h")
+ have_header("ruby/encoding.h")
+end
create_makefile 'json/ext/parser'
diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index 1e168ee..af57b81 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class,
- i_array_class, i_key_p, i_deep_const_get;
+ i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift;
#line 108 "parser.rl"
@@ -94,7 +94,7 @@ static const int JSON_object_error = 0;
static const int JSON_object_en_main = 1;
-#line 144 "parser.rl"
+#line 148 "parser.rl"
static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -115,7 +115,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
cs = JSON_object_start;
}
-#line 159 "parser.rl"
+#line 163 "parser.rl"
#line 121 "parser.c"
{
@@ -145,7 +145,7 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 127 "parser.rl"
+#line 131 "parser.rl"
{
char *np;
json->parsing_name = 1;
@@ -232,7 +232,11 @@ tr11:
if (np == NULL) {
p--; {p++; cs = 9; goto _out;}
} else {
- rb_hash_aset(*result, last_name, v);
+ if (NIL_P(json->object_class)) {
+ rb_hash_aset(*result, last_name, v);
+ } else {
+ rb_funcall(*result, i_aset, 2, last_name, v);
+ }
{p = (( np))-1;}
}
}
@@ -241,7 +245,7 @@ st9:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 245 "parser.c"
+#line 249 "parser.c"
switch( (*p) ) {
case 13: goto st9;
case 32: goto st9;
@@ -330,14 +334,14 @@ case 18:
goto st9;
goto st18;
tr4:
-#line 135 "parser.rl"
+#line 139 "parser.rl"
{ p--; {p++; cs = 27; goto _out;} }
goto st27;
st27:
if ( ++p == pe )
goto _test_eof27;
case 27:
-#line 341 "parser.c"
+#line 345 "parser.c"
goto st0;
st19:
if ( ++p == pe )
@@ -435,10 +439,10 @@ case 26:
_out: {}
}
-#line 160 "parser.rl"
+#line 164 "parser.rl"
if (cs >= JSON_object_first_final) {
- if (RTEST(json->create_id)) {
+ if (json->create_additions) {
VALUE klassname = rb_hash_aref(*result, json->create_id);
if (!NIL_P(klassname)) {
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
@@ -454,7 +458,7 @@ case 26:
}
-#line 458 "parser.c"
+#line 462 "parser.c"
static const int JSON_value_start = 1;
static const int JSON_value_first_final = 21;
static const int JSON_value_error = 0;
@@ -462,7 +466,7 @@ static const int JSON_value_error = 0;
static const int JSON_value_en_main = 1;
-#line 258 "parser.rl"
+#line 262 "parser.rl"
static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -470,14 +474,14 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 474 "parser.c"
+#line 478 "parser.c"
{
cs = JSON_value_start;
}
-#line 265 "parser.rl"
+#line 269 "parser.rl"
-#line 481 "parser.c"
+#line 485 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -502,14 +506,14 @@ st0:
cs = 0;
goto _out;
tr0:
-#line 206 "parser.rl"
+#line 210 "parser.rl"
{
char *np = JSON_parse_string(json, p, pe, result);
if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
}
goto st21;
tr2:
-#line 211 "parser.rl"
+#line 215 "parser.rl"
{
char *np;
if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) {
@@ -529,7 +533,7 @@ tr2:
}
goto st21;
tr5:
-#line 229 "parser.rl"
+#line 233 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -539,7 +543,7 @@ tr5:
}
goto st21;
tr9:
-#line 237 "parser.rl"
+#line 241 "parser.rl"
{
char *np;
json->current_nesting++;
@@ -549,7 +553,7 @@ tr9:
}
goto st21;
tr16:
-#line 199 "parser.rl"
+#line 203 "parser.rl"
{
if (json->allow_nan) {
*result = CInfinity;
@@ -559,7 +563,7 @@ tr16:
}
goto st21;
tr18:
-#line 192 "parser.rl"
+#line 196 "parser.rl"
{
if (json->allow_nan) {
*result = CNaN;
@@ -569,19 +573,19 @@ tr18:
}
goto st21;
tr22:
-#line 186 "parser.rl"
+#line 190 "parser.rl"
{
*result = Qfalse;
}
goto st21;
tr25:
-#line 183 "parser.rl"
+#line 187 "parser.rl"
{
*result = Qnil;
}
goto st21;
tr28:
-#line 189 "parser.rl"
+#line 193 "parser.rl"
{
*result = Qtrue;
}
@@ -590,9 +594,9 @@ st21:
if ( ++p == pe )
goto _test_eof21;
case 21:
-#line 245 "parser.rl"
+#line 249 "parser.rl"
{ p--; {p++; cs = 21; goto _out;} }
-#line 596 "parser.c"
+#line 600 "parser.c"
goto st0;
st2:
if ( ++p == pe )
@@ -753,7 +757,7 @@ case 20:
_out: {}
}
-#line 266 "parser.rl"
+#line 270 "parser.rl"
if (cs >= JSON_value_first_final) {
return p;
@@ -763,7 +767,7 @@ case 20:
}
-#line 767 "parser.c"
+#line 771 "parser.c"
static const int JSON_integer_start = 1;
static const int JSON_integer_first_final = 5;
static const int JSON_integer_error = 0;
@@ -771,7 +775,7 @@ static const int JSON_integer_error = 0;
static const int JSON_integer_en_main = 1;
-#line 282 "parser.rl"
+#line 286 "parser.rl"
static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -779,15 +783,15 @@ static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *res
int cs = EVIL;
-#line 783 "parser.c"
+#line 787 "parser.c"
{
cs = JSON_integer_start;
}
-#line 289 "parser.rl"
+#line 293 "parser.rl"
json->memo = p;
-#line 791 "parser.c"
+#line 795 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -821,14 +825,14 @@ case 3:
goto st0;
goto tr4;
tr4:
-#line 279 "parser.rl"
+#line 283 "parser.rl"
{ p--; {p++; cs = 5; goto _out;} }
goto st5;
st5:
if ( ++p == pe )
goto _test_eof5;
case 5:
-#line 832 "parser.c"
+#line 836 "parser.c"
goto st0;
st4:
if ( ++p == pe )
@@ -847,7 +851,7 @@ case 4:
_out: {}
}
-#line 291 "parser.rl"
+#line 295 "parser.rl"
if (cs >= JSON_integer_first_final) {
long len = p - json->memo;
@@ -859,7 +863,7 @@ case 4:
}
-#line 863 "parser.c"
+#line 867 "parser.c"
static const int JSON_float_start = 1;
static const int JSON_float_first_final = 10;
static const int JSON_float_error = 0;
@@ -867,7 +871,7 @@ static const int JSON_float_error = 0;
static const int JSON_float_en_main = 1;
-#line 313 "parser.rl"
+#line 317 "parser.rl"
static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -875,15 +879,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
int cs = EVIL;
-#line 879 "parser.c"
+#line 883 "parser.c"
{
cs = JSON_float_start;
}
-#line 320 "parser.rl"
+#line 324 "parser.rl"
json->memo = p;
-#line 887 "parser.c"
+#line 891 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -941,14 +945,14 @@ case 5:
goto st0;
goto tr7;
tr7:
-#line 307 "parser.rl"
+#line 311 "parser.rl"
{ p--; {p++; cs = 10; goto _out;} }
goto st10;
st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 952 "parser.c"
+#line 956 "parser.c"
goto st0;
st6:
if ( ++p == pe )
@@ -1009,7 +1013,7 @@ case 9:
_out: {}
}
-#line 322 "parser.rl"
+#line 326 "parser.rl"
if (cs >= JSON_float_first_final) {
long len = p - json->memo;
@@ -1022,7 +1026,7 @@ case 9:
-#line 1026 "parser.c"
+#line 1030 "parser.c"
static const int JSON_array_start = 1;
static const int JSON_array_first_final = 17;
static const int JSON_array_error = 0;
@@ -1030,7 +1034,7 @@ static const int JSON_array_error = 0;
static const int JSON_array_en_main = 1;
-#line 358 "parser.rl"
+#line 366 "parser.rl"
static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
@@ -1044,14 +1048,14 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
*result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
-#line 1048 "parser.c"
+#line 1052 "parser.c"
{
cs = JSON_array_start;
}
-#line 371 "parser.rl"
+#line 379 "parser.rl"
-#line 1055 "parser.c"
+#line 1059 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1090,14 +1094,18 @@ case 2:
goto st2;
goto st0;
tr2:
-#line 339 "parser.rl"
+#line 343 "parser.rl"
{
VALUE v = Qnil;
char *np = JSON_parse_value(json, p, pe, &v);
if (np == NULL) {
p--; {p++; cs = 3; goto _out;}
} else {
- rb_ary_push(*result, v);
+ if (NIL_P(json->array_class)) {
+ rb_ary_push(*result, v);
+ } else {
+ rb_funcall(*result, i_leftshift, 1, v);
+ }
{p = (( np))-1;}
}
}
@@ -1106,7 +1114,7 @@ st3:
if ( ++p == pe )
goto _test_eof3;
case 3:
-#line 1110 "parser.c"
+#line 1118 "parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st3;
@@ -1206,14 +1214,14 @@ case 12:
goto st3;
goto st12;
tr4:
-#line 350 "parser.rl"
+#line 358 "parser.rl"
{ p--; {p++; cs = 17; goto _out;} }
goto st17;
st17:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 1217 "parser.c"
+#line 1225 "parser.c"
goto st0;
st13:
if ( ++p == pe )
@@ -1269,7 +1277,7 @@ case 16:
_out: {}
}
-#line 372 "parser.rl"
+#line 380 "parser.rl"
if(cs >= JSON_array_first_final) {
return p + 1;
@@ -1350,7 +1358,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1354 "parser.c"
+#line 1362 "parser.c"
static const int JSON_string_start = 1;
static const int JSON_string_first_final = 8;
static const int JSON_string_error = 0;
@@ -1358,24 +1366,37 @@ static const int JSON_string_error = 0;
static const int JSON_string_en_main = 1;
-#line 471 "parser.rl"
+#line 479 "parser.rl"
+
+static int
+match_i(VALUE regexp, VALUE klass, VALUE memo)
+{
+ if (regexp == Qundef) return ST_STOP;
+ if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
+ RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
+ rb_ary_push(memo, klass);
+ return ST_STOP;
+ }
+ return ST_CONTINUE;
+}
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
+ VALUE match_string;
*result = rb_str_buf_new(0);
-#line 1371 "parser.c"
+#line 1392 "parser.c"
{
cs = JSON_string_start;
}
-#line 479 "parser.rl"
+#line 500 "parser.rl"
json->memo = p;
-#line 1379 "parser.c"
+#line 1400 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1400,7 +1421,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 457 "parser.rl"
+#line 465 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1411,14 +1432,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 468 "parser.rl"
+#line 476 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1422 "parser.c"
+#line 1443 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1494,7 +1515,18 @@ case 7:
_out: {}
}
-#line 481 "parser.rl"
+#line 502 "parser.rl"
+
+ if (json->create_additions && RTEST(match_string = json->match_string)) {
+ VALUE klass;
+ VALUE memo = rb_ary_new2(2);
+ rb_ary_push(memo, *result);
+ rb_hash_foreach(match_string, match_i, memo);
+ klass = rb_ary_entry(memo, 1);
+ if (RTEST(klass)) {
+ *result = rb_funcall(klass, i_json_create, 1, *result);
+ }
+ }
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
@@ -1508,7 +1540,7 @@ case 7:
-#line 1512 "parser.c"
+#line 1544 "parser.c"
static const int JSON_start = 1;
static const int JSON_first_final = 10;
static const int JSON_error = 0;
@@ -1516,7 +1548,7 @@ static const int JSON_error = 0;
static const int JSON_en_main = 1;
-#line 518 "parser.rl"
+#line 550 "parser.rl"
/*
@@ -1631,26 +1663,25 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
tmp = ID2SYM(i_allow_nan);
if (option_given_p(opts, tmp)) {
- VALUE allow_nan = rb_hash_aref(opts, tmp);
- json->allow_nan = RTEST(allow_nan) ? 1 : 0;
+ json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
} else {
json->allow_nan = 0;
}
tmp = ID2SYM(i_symbolize_names);
if (option_given_p(opts, tmp)) {
- VALUE symbolize_names = rb_hash_aref(opts, tmp);
- json->symbolize_names = RTEST(symbolize_names) ? 1 : 0;
+ json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
} else {
json->symbolize_names = 0;
}
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
- VALUE create_additions = rb_hash_aref(opts, tmp);
- if (RTEST(create_additions)) {
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
- } else {
- json->create_id = Qnil;
- }
+ json->create_additions = RTEST(rb_hash_aref(opts, tmp));
+ } else {
+ json->create_additions = 1;
+ }
+ tmp = ID2SYM(i_create_id);
+ if (option_given_p(opts, tmp)) {
+ json->create_id = rb_hash_aref(opts, tmp);
} else {
json->create_id = rb_funcall(mJSON, i_create_id, 0);
}
@@ -1666,10 +1697,18 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->array_class = Qnil;
}
+ tmp = ID2SYM(i_match_string);
+ if (option_given_p(opts, tmp)) {
+ VALUE match_string = rb_hash_aref(opts, tmp);
+ json->match_string = RTEST(match_string) ? match_string : Qnil;
+ } else {
+ json->match_string = Qnil;
+ }
}
} else {
json->max_nesting = 19;
json->allow_nan = 0;
+ json->create_additions = 1;
json->create_id = rb_funcall(mJSON, i_create_id, 0);
json->object_class = Qnil;
json->array_class = Qnil;
@@ -1695,16 +1734,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1699 "parser.c"
+#line 1738 "parser.c"
{
cs = JSON_start;
}
-#line 696 "parser.rl"
+#line 735 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1708 "parser.c"
+#line 1747 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1760,7 +1799,7 @@ case 5:
goto st1;
goto st5;
tr3:
-#line 507 "parser.rl"
+#line 539 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1769,7 +1808,7 @@ tr3:
}
goto st10;
tr4:
-#line 500 "parser.rl"
+#line 532 "parser.rl"
{
char *np;
json->current_nesting = 1;
@@ -1781,7 +1820,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1785 "parser.c"
+#line 1824 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1838,7 +1877,7 @@ case 9:
_out: {}
}
-#line 699 "parser.rl"
+#line 738 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
@@ -1861,6 +1900,7 @@ static void JSON_mark(JSON_Parser *json)
rb_gc_mark_maybe(json->create_id);
rb_gc_mark_maybe(json->object_class);
rb_gc_mark_maybe(json->array_class);
+ rb_gc_mark_maybe(json->match_string);
}
static void JSON_free(JSON_Parser *json)
@@ -1913,8 +1953,12 @@ void Init_parser()
i_symbolize_names = rb_intern("symbolize_names");
i_object_class = rb_intern("object_class");
i_array_class = rb_intern("array_class");
+ i_match = rb_intern("match");
+ i_match_string = rb_intern("match_string");
i_key_p = rb_intern("key?");
i_deep_const_get = rb_intern("deep_const_get");
+ i_aset = rb_intern("[]=");
+ i_leftshift = rb_intern("<<");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
diff --git a/ext/json/parser/parser.h b/ext/json/parser/parser.h
index a344da0..f2ce5a4 100644
--- a/ext/json/parser/parser.h
+++ b/ext/json/parser/parser.h
@@ -9,18 +9,23 @@
#ifdef HAVE_RUBY_ENCODING_H
#include "ruby/encoding.h"
-#define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
+#define FORCE_UTF8(obj) ((obj) = rb_enc_associate(rb_str_dup(obj), rb_utf8_encoding()))
#else
#define FORCE_UTF8(obj)
#endif
+#ifdef HAVE_RUBY_ST_H
+#include "ruby/st.h"
+#else
+#include "st.h"
+#endif
#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
/* unicode */
-typedef unsigned long UTF32; /* at least 32 bits */
-typedef unsigned short UTF16; /* at least 16 bits */
-typedef unsigned char UTF8; /* typically 8 bits */
+typedef unsigned long UTF32; /* at least 32 bits */
+typedef unsigned short UTF16; /* at least 16 bits */
+typedef unsigned char UTF8; /* typically 8 bits */
#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
#define UNI_SUR_HIGH_START (UTF32)0xD800
@@ -41,6 +46,8 @@ typedef struct JSON_ParserStruct {
int symbolize_names;
VALUE object_class;
VALUE array_class;
+ int create_additions;
+ VALUE match_string;
} JSON_Parser;
#define GET_PARSER \
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl
index 9531e3a..21b445e 100644
--- a/ext/json/parser/parser.rl
+++ b/ext/json/parser/parser.rl
@@ -77,7 +77,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class,
- i_array_class, i_key_p, i_deep_const_get;
+ i_array_class, i_key_p, i_deep_const_get, i_match, i_match_string, i_aset, i_leftshift;
%%{
machine JSON_common;
@@ -119,7 +119,11 @@ static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
if (np == NULL) {
fhold; fbreak;
} else {
- rb_hash_aset(*result, last_name, v);
+ if (NIL_P(json->object_class)) {
+ rb_hash_aset(*result, last_name, v);
+ } else {
+ rb_funcall(*result, i_aset, 2, last_name, v);
+ }
fexec np;
}
}
@@ -159,7 +163,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
%% write exec;
if (cs >= JSON_object_first_final) {
- if (RTEST(json->create_id)) {
+ if (json->create_additions) {
VALUE klassname = rb_hash_aref(*result, json->create_id);
if (!NIL_P(klassname)) {
VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
@@ -342,7 +346,11 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
if (np == NULL) {
fhold; fbreak;
} else {
- rb_ary_push(*result, v);
+ if (NIL_P(json->array_class)) {
+ rb_ary_push(*result, v);
+ } else {
+ rb_funcall(*result, i_leftshift, 1, v);
+ }
fexec np;
}
}
@@ -470,15 +478,39 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
main := '"' ((^([\"\\] | 0..0x1f) | '\\'[\"\\/bfnrt] | '\\u'[0-9a-fA-F]{4} | '\\'^([\"\\/bfnrtu]|0..0x1f))* %parse_string) '"' @exit;
}%%
+static int
+match_i(VALUE regexp, VALUE klass, VALUE memo)
+{
+ if (regexp == Qundef) return ST_STOP;
+ if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
+ RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
+ rb_ary_push(memo, klass);
+ return ST_STOP;
+ }
+ return ST_CONTINUE;
+}
+
static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
{
int cs = EVIL;
+ VALUE match_string;
*result = rb_str_buf_new(0);
%% write init;
json->memo = p;
%% write exec;
+ if (json->create_additions && RTEST(match_string = json->match_string)) {
+ VALUE klass;
+ VALUE memo = rb_ary_new2(2);
+ rb_ary_push(memo, *result);
+ rb_hash_foreach(match_string, match_i, memo);
+ klass = rb_ary_entry(memo, 1);
+ if (RTEST(klass)) {
+ *result = rb_funcall(klass, i_json_create, 1, *result);
+ }
+ }
+
if (json->symbolize_names && json->parsing_name) {
*result = rb_str_intern(*result);
}
@@ -629,26 +661,25 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
tmp = ID2SYM(i_allow_nan);
if (option_given_p(opts, tmp)) {
- VALUE allow_nan = rb_hash_aref(opts, tmp);
- json->allow_nan = RTEST(allow_nan) ? 1 : 0;
+ json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
} else {
json->allow_nan = 0;
}
tmp = ID2SYM(i_symbolize_names);
if (option_given_p(opts, tmp)) {
- VALUE symbolize_names = rb_hash_aref(opts, tmp);
- json->symbolize_names = RTEST(symbolize_names) ? 1 : 0;
+ json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
} else {
json->symbolize_names = 0;
}
tmp = ID2SYM(i_create_additions);
if (option_given_p(opts, tmp)) {
- VALUE create_additions = rb_hash_aref(opts, tmp);
- if (RTEST(create_additions)) {
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
- } else {
- json->create_id = Qnil;
- }
+ json->create_additions = RTEST(rb_hash_aref(opts, tmp));
+ } else {
+ json->create_additions = 1;
+ }
+ tmp = ID2SYM(i_create_id);
+ if (option_given_p(opts, tmp)) {
+ json->create_id = rb_hash_aref(opts, tmp);
} else {
json->create_id = rb_funcall(mJSON, i_create_id, 0);
}
@@ -664,10 +695,18 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
} else {
json->array_class = Qnil;
}
+ tmp = ID2SYM(i_match_string);
+ if (option_given_p(opts, tmp)) {
+ VALUE match_string = rb_hash_aref(opts, tmp);
+ json->match_string = RTEST(match_string) ? match_string : Qnil;
+ } else {
+ json->match_string = Qnil;
+ }
}
} else {
json->max_nesting = 19;
json->allow_nan = 0;
+ json->create_additions = 1;
json->create_id = rb_funcall(mJSON, i_create_id, 0);
json->object_class = Qnil;
json->array_class = Qnil;
@@ -718,6 +757,7 @@ static void JSON_mark(JSON_Parser *json)
rb_gc_mark_maybe(json->create_id);
rb_gc_mark_maybe(json->object_class);
rb_gc_mark_maybe(json->array_class);
+ rb_gc_mark_maybe(json->match_string);
}
static void JSON_free(JSON_Parser *json)
@@ -770,8 +810,12 @@ void Init_parser()
i_symbolize_names = rb_intern("symbolize_names");
i_object_class = rb_intern("object_class");
i_array_class = rb_intern("array_class");
+ i_match = rb_intern("match");
+ i_match_string = rb_intern("match_string");
i_key_p = rb_intern("key?");
i_deep_const_get = rb_intern("deep_const_get");
+ i_aset = rb_intern("[]=");
+ i_leftshift = rb_intern("<<");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
diff --git a/test/json/setup_variant.rb b/test/json/setup_variant.rb
new file mode 100644
index 0000000..2dab184
--- /dev/null
+++ b/test/json/setup_variant.rb
@@ -0,0 +1,11 @@
+case ENV['JSON']
+when 'pure'
+ $:.unshift 'lib'
+ require 'json/pure'
+when 'ext'
+ $:.unshift 'ext', 'lib'
+ require 'json/ext'
+else
+ $:.unshift 'ext', 'lib'
+ require 'json'
+end
diff --git a/test/json/test_json.rb b/test/json/test_json.rb
index 5f186f0..b367e90 100755
--- a/test/json/test_json.rb
+++ b/test/json/test_json.rb
@@ -2,11 +2,7 @@
# -*- coding: utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
unless Array.method_defined?(:permutation)
@@ -154,11 +150,20 @@ class TC_JSON < Test::Unit::TestCase
assert_equal(@ary,
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
',[false],[true]]'))
- assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
+ assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
end
- class SubArray < Array; end
+ class SubArray < Array
+ def <<(v)
+ @shifted = true
+ super
+ end
+
+ def shifted?
+ @shifted
+ end
+ end
class SubArray2 < Array
def to_json(*a)
@@ -175,9 +180,10 @@ class TC_JSON < Test::Unit::TestCase
end
def test_parse_array_custom_class
- res = parse('[]', :array_class => SubArray)
- assert_equal([], res)
+ res = parse('[1,2]', :array_class => SubArray)
+ assert_equal([1,2], res)
assert_equal(SubArray, res.class)
+ assert res.shifted?
end
def test_parse_object
@@ -188,6 +194,14 @@ class TC_JSON < Test::Unit::TestCase
end
class SubHash < Hash
+ def []=(k, v)
+ @item_set = true
+ super
+ end
+
+ def item_set?
+ @item_set
+ end
end
class SubHash2 < Hash
@@ -204,9 +218,10 @@ class TC_JSON < Test::Unit::TestCase
end
def test_parse_object_custom_class
- res = parse('{}', :object_class => SubHash2)
- assert_equal({}, res)
- assert_equal(SubHash2, res.class)
+ res = parse('{"foo":"bar"}', :object_class => SubHash)
+ assert_equal({"foo" => "bar"}, res)
+ assert_equal(SubHash, res.class)
+ assert res.item_set?
end
def test_generation_of_core_subclasses_with_new_to_json
@@ -392,23 +407,18 @@ EOT
assert_equal orig, JSON[json5][0]
end
- def test_allocate
- json = JSON::Parser.new("{}")
- assert_raises(TypeError, '[ruby-core:35079]') {json.__send__(:initialize, "{}")}
- json = JSON::Parser.allocate
- assert_raises(TypeError, '[ruby-core:35079]') {json.source}
+ if defined?(JSON::Ext::Parser)
+ def test_allocate
+ parser = JSON::Ext::Parser.new("{}")
+ assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")}
+ parser = JSON::Ext::Parser.allocate
+ assert_raise(TypeError, '[ruby-core:35079]') {parser.source}
+ end
end
def test_argument_encoding
source = "{}".force_encoding("ascii-8bit")
JSON::Parser.new(source)
assert_equal Encoding::ASCII_8BIT, source.encoding
- end
-
- def test_frozen_argument
- source = "{}".force_encoding("ascii-8bit")
- source.freeze
- parser = nil
- assert_nothing_raised {parser = JSON::Parser.new(source)}
- end
+ end if defined?(Encoding::ASCII_8BIT)
end
diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb
index 36b89e5..a8181e8 100755
--- a/test/json/test_json_addition.rb
+++ b/test/json/test_json_addition.rb
@@ -2,11 +2,7 @@
# -*- coding:utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
load 'json/add/core.rb'
require 'date'
@@ -36,6 +32,15 @@ class TC_JSONAddition < Test::Unit::TestCase
end
end
+ class A2 < A
+ def to_json(*args)
+ {
+ 'json_class' => self.class.name,
+ 'args' => [ @a ],
+ }.to_json(*args)
+ end
+ end
+
class B
def self.json_creatable?
false
diff --git a/test/json/test_json_encoding.rb b/test/json/test_json_encoding.rb
index cdeca58..7af5e63 100644
--- a/test/json/test_json_encoding.rb
+++ b/test/json/test_json_encoding.rb
@@ -2,11 +2,7 @@
# -*- coding: utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
class TC_JSONEncoding < Test::Unit::TestCase
include JSON
diff --git a/test/json/test_json_fixtures.rb b/test/json/test_json_fixtures.rb
index 95e57eb..e9df8f5 100755
--- a/test/json/test_json_fixtures.rb
+++ b/test/json/test_json_fixtures.rb
@@ -2,11 +2,7 @@
# -*- coding: utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
class TC_JSONFixtures < Test::Unit::TestCase
def setup
@@ -18,15 +14,20 @@ class TC_JSONFixtures < Test::Unit::TestCase
def test_passing
for name, source in @passed
- assert JSON.parse(source),
- "Did not pass for fixture '#{name}'"
+ begin
+ assert JSON.parse(source),
+ "Did not pass for fixture '#{name}': #{source.inspect}"
+ rescue => e
+ warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
+ raise e
+ end
end
end
def test_failing
for name, source in @failed
assert_raises(JSON::ParserError, JSON::NestingError,
- "Did not fail for fixture '#{name}'") do
+ "Did not fail for fixture '#{name}': #{source.inspect}") do
JSON.parse(source)
end
end
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb
index 5380a06..9b0cff4 100755
--- a/test/json/test_json_generate.rb
+++ b/test/json/test_json_generate.rb
@@ -2,11 +2,7 @@
# -*- coding: utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
class TC_JSONGenerate < Test::Unit::TestCase
include JSON
@@ -58,6 +54,7 @@ EOT
def test_generate_pretty
json = pretty_generate(@hash)
+ # hashes aren't (insertion) ordered on every ruby implementation assert_equal(@json3, json)
assert_equal(JSON.parse(@json3), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
diff --git a/test/json/test_json_string_matching.rb b/test/json/test_json_string_matching.rb
new file mode 100644
index 0000000..df26a68
--- /dev/null
+++ b/test/json/test_json_string_matching.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+
+require 'test/unit'
+require File.join(File.dirname(__FILE__), 'setup_variant')
+require 'stringio'
+require 'time'
+
+class TestJsonStringMatching < Test::Unit::TestCase
+ include JSON
+
+ class TestTime < ::Time
+ def self.json_create(string)
+ Time.parse(string)
+ end
+
+ def to_json(*)
+ %{"#{strftime('%FT%T%z')}"}
+ end
+
+ def ==(other)
+ to_i == other.to_i
+ end
+ end
+
+ def test_match_date
+ t = TestTime.new
+ t_json = [ t ].to_json
+ assert_equal [ t ],
+ JSON.parse(t_json,
+ :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime })
+ assert_equal [ t.strftime('%FT%T%z') ],
+ JSON.parse(t_json,
+ :match_string => { /\A\d{3}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime })
+ assert_equal [ t.strftime('%FT%T%z') ],
+ JSON.parse(t_json,
+ :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime },
+ :create_additions => false)
+ end
+end
diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb
index 505f5d5..ace56ca 100755
--- a/test/json/test_json_unicode.rb
+++ b/test/json/test_json_unicode.rb
@@ -2,11 +2,7 @@
# -*- coding: utf-8 -*-
require 'test/unit'
-case ENV['JSON']
-when 'pure' then require 'json/pure'
-when 'ext' then require 'json/ext'
-else require 'json'
-end
+require File.join(File.dirname(__FILE__), 'setup_variant')
class TC_JSONUnicode < Test::Unit::TestCase
include JSON