Skip to content
This repository
Browse code

update json to v1.7.5

  • Loading branch information...
commit 914dd77899ccc28ae2fd32e2bd4f378788b978c3 1 parent f0dd768
Watson Watson1978 authored
31 ext/json/fbuffer/fbuffer.h
@@ -2,8 +2,8 @@
2 2 #ifndef _FBUFFER_H_
3 3 #define _FBUFFER_H_
4 4
5   -#include <assert.h>
6 5 #include "ruby.h"
  6 +#include <assert.h>
7 7
8 8 #ifdef __MACRUBY__
9 9 /* We cannot use the GC memory functions here
@@ -19,6 +19,27 @@
19 19 # define ruby_xfree(x) free(x)
20 20 #endif
21 21
  22 +#ifndef RHASH_SIZE
  23 +#define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
  24 +#endif
  25 +
  26 +#ifndef RFLOAT_VALUE
  27 +#define RFLOAT_VALUE(val) (RFLOAT(val)->value)
  28 +#endif
  29 +
  30 +#ifndef RARRAY_PTR
  31 +#define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
  32 +#endif
  33 +#ifndef RARRAY_LEN
  34 +#define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
  35 +#endif
  36 +#ifndef RSTRING_PTR
  37 +#define RSTRING_PTR(string) RSTRING(string)->ptr
  38 +#endif
  39 +#ifndef RSTRING_LEN
  40 +#define RSTRING_LEN(string) RSTRING(string)->len
  41 +#endif
  42 +
22 43 #ifdef HAVE_RUBY_ENCODING_H
23 44 #include "ruby/encoding.h"
24 45 #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
@@ -49,10 +70,14 @@ static FBuffer *fbuffer_alloc(unsigned long initial_length);
49 70 static void fbuffer_free(FBuffer *fb);
50 71 static void fbuffer_clear(FBuffer *fb);
51 72 static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
  73 +#ifdef JSON_GENERATOR
52 74 static void fbuffer_append_long(FBuffer *fb, long number);
  75 +#endif
53 76 static void fbuffer_append_char(FBuffer *fb, char newchr);
  77 +#ifdef JSON_GENERATOR
54 78 static FBuffer *fbuffer_dup(FBuffer *fb);
55 79 static VALUE fbuffer_to_s(FBuffer *fb);
  80 +#endif
56 81
57 82 static FBuffer *fbuffer_alloc(unsigned long initial_length)
58 83 {
@@ -101,6 +126,7 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
101 126 }
102 127 }
103 128
  129 +#ifdef JSON_GENERATOR
104 130 static void fbuffer_append_str(FBuffer *fb, VALUE str)
105 131 {
106 132 const char *newstr = StringValuePtr(str);
@@ -110,6 +136,7 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
110 136
111 137 fbuffer_append(fb, newstr, len);
112 138 }
  139 +#endif
113 140
114 141 static void fbuffer_append_char(FBuffer *fb, char newchr)
115 142 {
@@ -118,6 +145,7 @@ static void fbuffer_append_char(FBuffer *fb, char newchr)
118 145 fb->len++;
119 146 }
120 147
  148 +#ifdef JSON_GENERATOR
121 149 static void freverse(char *start, char *end)
122 150 {
123 151 char c;
@@ -168,3 +196,4 @@ static VALUE fbuffer_to_s(FBuffer *fb)
168 196 return result;
169 197 }
170 198 #endif
  199 +#endif
1  ext/json/generator/extconf.rb
@@ -10,4 +10,5 @@
10 10 end
11 11 end
12 12
  13 +$defs << "-DJSON_GENERATOR"
13 14 create_makefile 'json/ext/generator'
25 ext/json/generator/generator.c
@@ -853,6 +853,21 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
853 853 }
854 854
855 855 /*
  856 + * This function returns true if string is either a JSON array or JSON object.
  857 + * It might suffer from false positives, e. g. syntactically incorrect JSON in
  858 + * the string or certain UTF-8 characters on the right hand side.
  859 + */
  860 +static int isArrayOrObject(VALUE string)
  861 +{
  862 + long string_len = RSTRING_LEN(string);
  863 + char *p = RSTRING_PTR(string), *q = p + string_len - 1;
  864 + if (string_len < 2) return 0;
  865 + for (; p < q && isspace(*p); p++);
  866 + for (; q > p && isspace(*q); q--);
  867 + return (*p == '[' && *q == ']') || (*p == '{' && *q == '}');
  868 +}
  869 +
  870 +/*
856 871 * call-seq: generate(obj)
857 872 *
858 873 * Generates a valid JSON document from object +obj+ and returns the
@@ -862,15 +877,9 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
862 877 static VALUE cState_generate(VALUE self, VALUE obj)
863 878 {
864 879 VALUE result = cState_partial_generate(self, obj);
865   - VALUE re, args[2];
866 880 GET_STATE(self);
867   - if (!state->quirks_mode) {
868   - args[0] = rb_str_new2("\\A\\s*(?:\\[.*\\]|\\{.*\\})\\s*\\Z");
869   - args[1] = CRegexp_MULTILINE;
870   - re = rb_class_new_instance(2, args, rb_cRegexp);
871   - if (NIL_P(rb_funcall(re, i_match, 1, result))) {
872   - rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
873   - }
  881 + if (!state->quirks_mode && !isArrayOrObject(result)) {
  882 + rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
874 883 }
875 884 return result;
876 885 }
22 ext/json/generator/generator.h
@@ -4,6 +4,7 @@
4 4 #include <string.h>
5 5 #include <assert.h>
6 6 #include <math.h>
  7 +#include <ctype.h>
7 8
8 9 #include "ruby.h"
9 10
@@ -15,27 +16,6 @@
15 16
16 17 #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
17 18
18   -#ifndef RHASH_SIZE
19   -#define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries)
20   -#endif
21   -
22   -#ifndef RFLOAT_VALUE
23   -#define RFLOAT_VALUE(val) (RFLOAT(val)->value)
24   -#endif
25   -
26   -#ifndef RARRAY_PTR
27   -#define RARRAY_PTR(ARRAY) RARRAY(ARRAY)->ptr
28   -#endif
29   -#ifndef RARRAY_LEN
30   -#define RARRAY_LEN(ARRAY) RARRAY(ARRAY)->len
31   -#endif
32   -#ifndef RSTRING_PTR
33   -#define RSTRING_PTR(string) RSTRING(string)->ptr
34   -#endif
35   -#ifndef RSTRING_LEN
36   -#define RSTRING_LEN(string) RSTRING(string)->len
37   -#endif
38   -
39 19 /* unicode defintions */
40 20
41 21 #define UNI_STRICT_CONVERSION 1
5 ext/json/lib/json/add/time.rb
@@ -20,10 +20,13 @@ def self.json_create(object)
20 20 # Returns a hash, that will be turned into a JSON object and represent this
21 21 # object.
22 22 def as_json(*)
  23 + nanoseconds = [ tv_usec * 1000 ]
  24 + respond_to?(:tv_nsec) and nanoseconds << tv_nsec
  25 + nanoseconds = nanoseconds.max
23 26 {
24 27 JSON.create_id => self.class.name,
25 28 's' => tv_sec,
26   - 'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
  29 + 'n' => nanoseconds,
27 30 }
28 31 end
29 32
14 ext/json/lib/json/common.rb
... ... @@ -1,4 +1,5 @@
1 1 require 'json/version'
  2 +require 'json/generic_object'
2 3
3 4 module JSON
4 5 class << self
@@ -110,7 +111,13 @@ def generator=(generator) # :nodoc:
110 111 MinusInfinity = -Infinity
111 112
112 113 # The base exception for JSON errors.
113   - class JSONError < StandardError; end
  114 + class JSONError < StandardError
  115 + def self.wrap(exception)
  116 + obj = new("Wrapped(#{exception.class}): #{exception.message.inspect}")
  117 + obj.set_backtrace exception.backtrace
  118 + obj
  119 + end
  120 + end
114 121
115 122 # This exception is raised if a parser error occurs.
116 123 class ParserError < JSONError; end
@@ -406,7 +413,10 @@ def self.swap!(string) # :nodoc:
406 413 end
407 414
408 415 # Shortuct for iconv.
409   - if ::String.method_defined?(:encode)
  416 + if ::String.method_defined?(:encode) &&
  417 + # XXX Rubinius doesn't support ruby 1.9 encoding yet
  418 + defined?(RUBY_ENGINE) && RUBY_ENGINE != 'rbx'
  419 + then
410 420 # Encodes string using Ruby's _String.encode_
411 421 def self.iconv(to, from, string)
412 422 string.encode(to, from)
39 ext/json/lib/json/generic_object.rb
... ... @@ -0,0 +1,39 @@
  1 +require 'ostruct'
  2 +
  3 +module JSON
  4 + class GenericObject < OpenStruct
  5 + class << self
  6 + alias [] new
  7 +
  8 + def json_create(data)
  9 + data = data.dup
  10 + data.delete JSON.create_id
  11 + self[data]
  12 + end
  13 + end
  14 +
  15 + def to_hash
  16 + table
  17 + end
  18 +
  19 + def [](name)
  20 + table[name.to_sym]
  21 + end
  22 +
  23 + def []=(name, value)
  24 + __send__ "#{name}=", value
  25 + end
  26 +
  27 + def |(other)
  28 + self.class[other.to_hash.merge(to_hash)]
  29 + end
  30 +
  31 + def as_json(*)
  32 + { JSON.create_id => self.class.name }.merge to_hash
  33 + end
  34 +
  35 + def to_json(*a)
  36 + as_json.to_json(*a)
  37 + end
  38 + end
  39 +end
20 ext/json/lib/json/pure/generator.rb
@@ -41,7 +41,6 @@ module JSON
41 41 if defined?(::Encoding)
42 42 def utf8_to_json(string) # :nodoc:
43 43 string = string.dup
44   - string << '' # XXX workaround: avoid buffer sharing
45 44 string.force_encoding(::Encoding::ASCII_8BIT)
46 45 string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
47 46 string.force_encoding(::Encoding::UTF_8)
@@ -50,9 +49,8 @@ def utf8_to_json(string) # :nodoc:
50 49
51 50 def utf8_to_json_ascii(string) # :nodoc:
52 51 string = string.dup
53   - string << '' # XXX workaround: avoid buffer sharing
54 52 string.force_encoding(::Encoding::ASCII_8BIT)
55   - string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
  53 + string.gsub!(/["\\\x0-\x1f]/n) { MAP[$&] }
56 54 string.gsub!(/(
57 55 (?:
58 56 [\xc2-\xdf][\x80-\xbf] |
@@ -63,16 +61,18 @@ def utf8_to_json_ascii(string) # :nodoc:
63 61 )/nx) { |c|
64 62 c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
65 63 s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
  64 + s.force_encoding(::Encoding::ASCII_8BIT)
66 65 s.gsub!(/.{4}/n, '\\\\u\&')
  66 + s.force_encoding(::Encoding::UTF_8)
67 67 }
68 68 string.force_encoding(::Encoding::UTF_8)
69 69 string
70 70 rescue => e
71   - raise GeneratorError, "Caught #{e.class}: #{e}"
  71 + raise GeneratorError.wrap(e)
72 72 end
73 73 else
74 74 def utf8_to_json(string) # :nodoc:
75   - string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
  75 + string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
76 76 end
77 77
78 78 def utf8_to_json_ascii(string) # :nodoc:
@@ -91,7 +91,7 @@ def utf8_to_json_ascii(string) # :nodoc:
91 91 }
92 92 string
93 93 rescue => e
94   - raise GeneratorError, "Caught #{e.class}: #{e}"
  94 + raise GeneratorError.wrap(e)
95 95 end
96 96 end
97 97 module_function :utf8_to_json, :utf8_to_json_ascii
@@ -255,8 +255,12 @@ def to_h
255 255 # GeneratorError exception.
256 256 def generate(obj)
257 257 result = obj.to_json(self)
258   - if !@quirks_mode && result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
259   - raise GeneratorError, "only generation of JSON objects or arrays allowed"
  258 + unless @quirks_mode
  259 + unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
  260 + result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
  261 + then
  262 + raise GeneratorError, "only generation of JSON objects or arrays allowed"
  263 + end
260 264 end
261 265 result
262 266 end
2  ext/json/lib/json/version.rb
... ... @@ -1,6 +1,6 @@
1 1 module JSON
2 2 # JSON version
3   - VERSION = '1.6.5'
  3 + VERSION = '1.7.5'
4 4 VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5 5 VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6 6 VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
35 ext/json/parser/parser.c
@@ -1721,6 +1721,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1721 1721 source = convert_encoding(StringValue(source));
1722 1722 }
1723 1723 json->current_nesting = 0;
  1724 + StringValue(source);
1724 1725 json->len = RSTRING_LEN(source);
1725 1726 json->source = RSTRING_PTR(source);;
1726 1727 json->Vsource = source;
@@ -1728,7 +1729,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1728 1729 }
1729 1730
1730 1731
1731   -#line 1732 "parser.c"
  1732 +#line 1733 "parser.c"
1732 1733 static const int JSON_start = 1;
1733 1734 static const int JSON_first_final = 10;
1734 1735 static const int JSON_error = 0;
@@ -1736,7 +1737,7 @@ static const int JSON_error = 0;
1736 1737 static const int JSON_en_main = 1;
1737 1738
1738 1739
1739   -#line 739 "parser.rl"
  1740 +#line 740 "parser.rl"
1740 1741
1741 1742
1742 1743 static VALUE cParser_parse_strict(VALUE self)
@@ -1747,16 +1748,16 @@ static VALUE cParser_parse_strict(VALUE self)
1747 1748 GET_PARSER;
1748 1749
1749 1750
1750   -#line 1751 "parser.c"
  1751 +#line 1752 "parser.c"
1751 1752 {
1752 1753 cs = JSON_start;
1753 1754 }
1754 1755
1755   -#line 749 "parser.rl"
  1756 +#line 750 "parser.rl"
1756 1757 p = json->source;
1757 1758 pe = p + json->len;
1758 1759
1759   -#line 1760 "parser.c"
  1760 +#line 1761 "parser.c"
1760 1761 {
1761 1762 if ( p == pe )
1762 1763 goto _test_eof;
@@ -1812,7 +1813,7 @@ case 5:
1812 1813 goto st1;
1813 1814 goto st5;
1814 1815 tr3:
1815   -#line 728 "parser.rl"
  1816 +#line 729 "parser.rl"
1816 1817 {
1817 1818 char *np;
1818 1819 json->current_nesting = 1;
@@ -1821,7 +1822,7 @@ case 5:
1821 1822 }
1822 1823 goto st10;
1823 1824 tr4:
1824   -#line 721 "parser.rl"
  1825 +#line 722 "parser.rl"
1825 1826 {
1826 1827 char *np;
1827 1828 json->current_nesting = 1;
@@ -1833,7 +1834,7 @@ case 5:
1833 1834 if ( ++p == pe )
1834 1835 goto _test_eof10;
1835 1836 case 10:
1836   -#line 1837 "parser.c"
  1837 +#line 1838 "parser.c"
1837 1838 switch( (*p) ) {
1838 1839 case 13: goto st10;
1839 1840 case 32: goto st10;
@@ -1890,7 +1891,7 @@ case 9:
1890 1891 _out: {}
1891 1892 }
1892 1893
1893   -#line 752 "parser.rl"
  1894 +#line 753 "parser.rl"
1894 1895
1895 1896 if (cs >= JSON_first_final && p == pe) {
1896 1897 return result;
@@ -1902,7 +1903,7 @@ case 9:
1902 1903
1903 1904
1904 1905
1905   -#line 1906 "parser.c"
  1906 +#line 1907 "parser.c"
1906 1907 static const int JSON_quirks_mode_start = 1;
1907 1908 static const int JSON_quirks_mode_first_final = 10;
1908 1909 static const int JSON_quirks_mode_error = 0;
@@ -1910,7 +1911,7 @@ static const int JSON_quirks_mode_error = 0;
1910 1911 static const int JSON_quirks_mode_en_main = 1;
1911 1912
1912 1913
1913   -#line 777 "parser.rl"
  1914 +#line 778 "parser.rl"
1914 1915
1915 1916
1916 1917 static VALUE cParser_parse_quirks_mode(VALUE self)
@@ -1921,16 +1922,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self)
1921 1922 GET_PARSER;
1922 1923
1923 1924
1924   -#line 1925 "parser.c"
  1925 +#line 1926 "parser.c"
1925 1926 {
1926 1927 cs = JSON_quirks_mode_start;
1927 1928 }
1928 1929
1929   -#line 787 "parser.rl"
  1930 +#line 788 "parser.rl"
1930 1931 p = json->source;
1931 1932 pe = p + json->len;
1932 1933
1933   -#line 1934 "parser.c"
  1934 +#line 1935 "parser.c"
1934 1935 {
1935 1936 if ( p == pe )
1936 1937 goto _test_eof;
@@ -1964,7 +1965,7 @@ case 1:
1964 1965 cs = 0;
1965 1966 goto _out;
1966 1967 tr2:
1967   -#line 769 "parser.rl"
  1968 +#line 770 "parser.rl"
1968 1969 {
1969 1970 char *np = JSON_parse_value(json, p, pe, &result);
1970 1971 if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1974,7 +1975,7 @@ cs = 0;
1974 1975 if ( ++p == pe )
1975 1976 goto _test_eof10;
1976 1977 case 10:
1977   -#line 1978 "parser.c"
  1978 +#line 1979 "parser.c"
1978 1979 switch( (*p) ) {
1979 1980 case 13: goto st10;
1980 1981 case 32: goto st10;
@@ -2063,7 +2064,7 @@ case 9:
2063 2064 _out: {}
2064 2065 }
2065 2066
2066   -#line 790 "parser.rl"
  2067 +#line 791 "parser.rl"
2067 2068
2068 2069 if (cs >= JSON_quirks_mode_first_final && p == pe) {
2069 2070 return result;
1  ext/json/parser/parser.rl
@@ -705,6 +705,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
705 705 source = convert_encoding(StringValue(source));
706 706 }
707 707 json->current_nesting = 0;
  708 + StringValue(source);
708 709 json->len = RSTRING_LEN(source);
709 710 json->source = RSTRING_PTR(source);;
710 711 json->Vsource = source;
20 test-mri/test/json/test_json.rb
@@ -21,7 +21,7 @@ def permutation
21 21 end
22 22 end
23 23
24   -class TC_JSON < Test::Unit::TestCase
  24 +class TestJSON < Test::Unit::TestCase
25 25 include JSON
26 26
27 27 def setup
@@ -109,6 +109,8 @@ def test_parse_simple_objects
109 109 def test_parse_json_primitive_values
110 110 assert_raise(JSON::ParserError) { JSON.parse('') }
111 111 assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
  112 + assert_raise(TypeError) { JSON::Parser.new(nil).parse }
  113 + assert_raise(TypeError) { JSON::Parser.new(nil, :quirks_mode => true).parse }
112 114 assert_raise(TypeError) { JSON.parse(nil) }
113 115 assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) }
114 116 assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
@@ -314,7 +316,17 @@ def test_parse_object_custom_non_hash_derived_class
314 316 assert res.item_set?
315 317 end
316 318
317   - def test_generation_of_core_subclasses_with_new_to_json
  319 + def test_parse_generic_object
  320 + res = parse('{"foo":"bar", "baz":{}}', :object_class => JSON::GenericObject)
  321 + assert_equal(JSON::GenericObject, res.class)
  322 + assert_equal "bar", res.foo
  323 + assert_equal "bar", res["foo"]
  324 + assert_equal "bar", res[:foo]
  325 + assert_equal "bar", res.to_hash[:foo]
  326 + assert_equal(JSON::GenericObject, res.baz.class)
  327 + end
  328 +
  329 + def test_generate_core_subclasses_with_new_to_json
318 330 obj = SubHash2["foo" => SubHash2["bar" => true]]
319 331 obj_json = JSON(obj)
320 332 obj_again = JSON(obj_json)
@@ -325,12 +337,12 @@ def test_generation_of_core_subclasses_with_new_to_json
325 337 assert_equal ["foo"], JSON(JSON(SubArray2["foo"]))
326 338 end
327 339
328   - def test_generation_of_core_subclasses_with_default_to_json
  340 + def test_generate_core_subclasses_with_default_to_json
329 341 assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
330 342 assert_equal '["foo"]', JSON(SubArray["foo"])
331 343 end
332 344
333   - def test_generation_of_core_subclasses
  345 + def test_generate_of_core_subclasses
334 346 obj = SubHash["foo" => SubHash["bar" => true]]
335 347 obj_json = JSON(obj)
336 348 obj_again = JSON(obj_json)
10 test-mri/test/json/test_json_addition.rb
@@ -10,7 +10,7 @@
10 10 require 'json/add/ostruct'
11 11 require 'date'
12 12
13   -class TC_JSONAddition < Test::Unit::TestCase
  13 +class TestJSONAddition < Test::Unit::TestCase
14 14 include JSON
15 15
16 16 class A
@@ -64,7 +64,7 @@ def self.json_creatable?
64 64
65 65 def to_json(*args)
66 66 {
67   - 'json_class' => 'TC_JSONAddition::Nix',
  67 + 'json_class' => 'TestJSONAddition::Nix',
68 68 }.to_json(*args)
69 69 end
70 70 end
@@ -88,7 +88,7 @@ def test_extended_json_disabled
88 88 a_hash = JSON.parse(json, :create_additions => false)
89 89 assert_kind_of Hash, a_hash
90 90 assert_equal(
91   - {"args"=>[666], "json_class"=>"TC_JSONAddition::A"}.sort_by { |k,| k },
  91 + {"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
92 92 a_hash.sort_by { |k,| k }
93 93 )
94 94 end
@@ -97,7 +97,7 @@ def test_extended_json_fail1
97 97 b = B.new
98 98 assert !B.json_creatable?
99 99 json = generate(b)
100   - assert_equal({ "json_class"=>"TC_JSONAddition::B" }, JSON.parse(json))
  100 + assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
101 101 end
102 102
103 103 def test_extended_json_fail2
@@ -119,7 +119,7 @@ def test_raw_strings
119 119 json_raw_object = raw.to_json_raw_object
120 120 hash = { 'json_class' => 'String', 'raw'=> raw_array }
121 121 assert_equal hash, json_raw_object
122   - assert_match(/\A\{.*\}\Z/, json)
  122 + assert_match(/\A\{.*\}\z/, json)
123 123 assert_match(/"json_class":"String"/, json)
124 124 assert_match(/"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json)
125 125 raw_again = JSON.parse(json)
2  test-mri/test/json/test_json_encoding.rb
@@ -4,7 +4,7 @@
4 4 require 'test/unit'
5 5 require File.join(File.dirname(__FILE__), 'setup_variant')
6 6
7   -class TC_JSONEncoding < Test::Unit::TestCase
  7 +class TestJSONEncoding < Test::Unit::TestCase
8 8 include JSON
9 9
10 10 def setup
2  test-mri/test/json/test_json_fixtures.rb
@@ -4,7 +4,7 @@
4 4 require 'test/unit'
5 5 require File.join(File.dirname(__FILE__), 'setup_variant')
6 6
7   -class TC_JSONFixtures < Test::Unit::TestCase
  7 +class TestJSONFixtures < Test::Unit::TestCase
8 8 def setup
9 9 fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json')
10 10 passed, failed = Dir[fixtures].partition { |f| f['pass'] }
36 test-mri/test/json/test_json_generate.rb
@@ -4,7 +4,7 @@
4 4 require 'test/unit'
5 5 require File.join(File.dirname(__FILE__), 'setup_variant')
6 6
7   -class TC_JSONGenerate < Test::Unit::TestCase
  7 +class TestJSONGenerate < Test::Unit::TestCase
8 8 include JSON
9 9
10 10 def setup
@@ -227,23 +227,25 @@ def test_gc
227 227 GC.stress = stress
228 228 end if GC.respond_to?(:stress=)
229 229
230   - def test_broken_bignum # [ruby-core:38867]
231   - pid = fork do
232   - Bignum.class_eval do
233   - def to_s
  230 + if defined?(JSON::Ext::Generator)
  231 + def test_broken_bignum # [ruby-core:38867]
  232 + pid = fork do
  233 + Bignum.class_eval do
  234 + def to_s
  235 + end
  236 + end
  237 + begin
  238 + JSON::Ext::Generator::State.new.generate(1<<64)
  239 + exit 1
  240 + rescue TypeError
  241 + exit 0
234 242 end
235 243 end
236   - begin
237   - JSON::Ext::Generator::State.new.generate(1<<64)
238   - exit 1
239   - rescue TypeError
240   - exit 0
241   - end
  244 + _, status = Process.waitpid2(pid)
  245 + assert status.success?
  246 + rescue NotImplementedError
  247 + # forking to avoid modifying core class of a parent process and
  248 + # introducing race conditions of tests are run in parallel
242 249 end
243   - _, status = Process.waitpid2(pid)
244   - assert status.success?
245   - rescue NotImplementedError
246   - # forking to avoid modifying core class of a parent process and
247   - # introducing race conditions of tests are run in parallel
248   - end if defined?(JSON::Ext)
  250 + end
249 251 end
35 test-mri/test/json/test_json_generic_object.rb
... ... @@ -0,0 +1,35 @@
  1 +#!/usr/bin/env ruby
  2 +# -*- coding: utf-8 -*-
  3 +
  4 +require 'test/unit'
  5 +require File.join(File.dirname(__FILE__), 'setup_variant')
  6 +class TestJSONGenericObject < Test::Unit::TestCase
  7 + include JSON
  8 +
  9 + def setup
  10 + @go = GenericObject[ :a => 1, :b => 2 ]
  11 + end
  12 +
  13 + def test_attributes
  14 + assert_equal 1, @go.a
  15 + assert_equal 1, @go[:a]
  16 + assert_equal 2, @go.b
  17 + assert_equal 2, @go[:b]
  18 + assert_nil @go.c
  19 + assert_nil @go[:c]
  20 + end
  21 +
  22 + def test_generate_json
  23 + assert_equal @go, JSON(JSON(@go))
  24 + end
  25 +
  26 + def test_parse_json
  27 + assert_equal @go, l = JSON('{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }')
  28 + assert_equal 1, l.a
  29 + assert_equal @go, l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject)
  30 + assert_equal 1, l.a
  31 + assert_equal GenericObject[:a => GenericObject[:b => 2]],
  32 + l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject)
  33 + assert_equal 2, l.a.b
  34 + end
  35 +end
8 test-mri/test/json/test_json_string_matching.rb
@@ -6,7 +6,7 @@
6 6 require 'stringio'
7 7 require 'time'
8 8
9   -class TestJsonStringMatching < Test::Unit::TestCase
  9 +class TestJSONStringMatching < Test::Unit::TestCase
10 10 include JSON
11 11
12 12 class TestTime < ::Time
@@ -28,13 +28,13 @@ def test_match_date
28 28 t_json = [ t ].to_json
29 29 assert_equal [ t ],
30 30 JSON.parse(t_json,
31   - :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime })
  31 + :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ => TestTime })
32 32 assert_equal [ t.strftime('%FT%T%z') ],
33 33 JSON.parse(t_json,
34   - :match_string => { /\A\d{3}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime })
  34 + :match_string => { /\A\d{3}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ => TestTime })
35 35 assert_equal [ t.strftime('%FT%T%z') ],
36 36 JSON.parse(t_json,
37   - :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\Z/ => TestTime },
  37 + :match_string => { /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/ => TestTime },
38 38 :create_additions => false)
39 39 end
40 40 end
2  test-mri/test/json/test_json_unicode.rb
@@ -4,7 +4,7 @@
4 4 require 'test/unit'
5 5 require File.join(File.dirname(__FILE__), 'setup_variant')
6 6
7   -class TC_JSONUnicode < Test::Unit::TestCase
  7 +class TestJSONUnicode < Test::Unit::TestCase
8 8 include JSON
9 9
10 10 def test_unicode

0 comments on commit 914dd77

Please sign in to comment.
Something went wrong with that request. Please try again.