json 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

@@ -27,7 +27,7 @@ static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
27
27
  static VALUE CNaN, CInfinity, CMinusInfinity;
28
28
 
29
29
  static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
30
- i_chr, i_max_nesting, i_allow_nan;
30
+ i_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class;
31
31
 
32
32
  #define MinusInfinity "-Infinity"
33
33
 
@@ -40,6 +40,8 @@ typedef struct JSON_ParserStruct {
40
40
  int max_nesting;
41
41
  int current_nesting;
42
42
  int allow_nan;
43
+ VALUE object_class;
44
+ VALUE array_class;
43
45
  } JSON_Parser;
44
46
 
45
47
  static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result);
@@ -118,12 +120,13 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
118
120
  {
119
121
  int cs = EVIL;
120
122
  VALUE last_name = Qnil;
123
+ VALUE object_class = json->object_class;
121
124
 
122
125
  if (json->max_nesting && json->current_nesting > json->max_nesting) {
123
126
  rb_raise(eNestingError, "nesting of %d is to deep", json->current_nesting);
124
127
  }
125
128
 
126
- *result = rb_hash_new();
129
+ *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
127
130
 
128
131
  %% write init;
129
132
  %% write exec;
@@ -330,11 +333,12 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
330
333
  static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
331
334
  {
332
335
  int cs = EVIL;
336
+ VALUE array_class = json->array_class;
333
337
 
334
338
  if (json->max_nesting && json->current_nesting > json->max_nesting) {
335
339
  rb_raise(eNestingError, "nesting of %d is to deep", json->current_nesting);
336
340
  }
337
- *result = rb_ary_new();
341
+ *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
338
342
 
339
343
  %% write init;
340
344
  %% write exec;
@@ -500,6 +504,8 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
500
504
  * * *create_additions*: If set to false, the Parser doesn't create
501
505
  * additions even if a matchin class and create_id was found. This option
502
506
  * defaults to true.
507
+ * * *object_class*: Defaults to Hash
508
+ * * *array_class*: Defaults to Array
503
509
  */
504
510
  static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
505
511
  {
@@ -549,11 +555,25 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
549
555
  } else {
550
556
  json->create_id = rb_funcall(mJSON, i_create_id, 0);
551
557
  }
558
+ tmp = ID2SYM(i_object_class);
559
+ if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
560
+ json->object_class = rb_hash_aref(opts, tmp);
561
+ } else {
562
+ json->object_class = Qnil;
563
+ }
564
+ tmp = ID2SYM(i_array_class);
565
+ if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
566
+ json->array_class = rb_hash_aref(opts, tmp);
567
+ } else {
568
+ json->array_class = Qnil;
569
+ }
552
570
  }
553
571
  } else {
554
572
  json->max_nesting = 19;
555
573
  json->allow_nan = 0;
556
574
  json->create_id = rb_funcall(mJSON, i_create_id, 0);
575
+ json->object_class = Qnil;
576
+ json->array_class = Qnil;
557
577
  }
558
578
  json->current_nesting = 0;
559
579
  /*
@@ -610,6 +630,8 @@ static void JSON_mark(JSON_Parser *json)
610
630
  {
611
631
  rb_gc_mark_maybe(json->Vsource);
612
632
  rb_gc_mark_maybe(json->create_id);
633
+ rb_gc_mark_maybe(json->object_class);
634
+ rb_gc_mark_maybe(json->array_class);
613
635
  }
614
636
 
615
637
  static void JSON_free(JSON_Parser *json)
@@ -659,4 +681,6 @@ void Init_parser()
659
681
  i_chr = rb_intern("chr");
660
682
  i_max_nesting = rb_intern("max_nesting");
661
683
  i_allow_nan = rb_intern("allow_nan");
684
+ i_object_class = rb_intern("object_class");
685
+ i_array_class = rb_intern("array_class");
662
686
  }
@@ -81,7 +81,7 @@ char *JSON_convert_UTF16_to_UTF8 (
81
81
  buf[1] = *p++;
82
82
  buf[2] = *p++;
83
83
  buf[3] = *p++;
84
- tmpPtr[i] = strtol(buf, NULL, 16);
84
+ tmpPtr[i] = (UTF16)strtol(buf, NULL, 16);
85
85
  p += 2;
86
86
  }
87
87
 
@@ -2,13 +2,9 @@ require 'json/common'
2
2
  module JSON
3
3
  require 'json/version'
4
4
 
5
- if VARIANT_BINARY
5
+ begin
6
6
  require 'json/ext'
7
- else
8
- begin
9
- require 'json/ext'
10
- rescue LoadError
11
- require 'json/pure'
12
- end
7
+ rescue LoadError
8
+ require 'json/pure'
13
9
  end
14
10
  end
@@ -61,6 +61,8 @@ module JSON
61
61
  # * *create_additions*: If set to false, the Parser doesn't create
62
62
  # additions even if a matchin class and create_id was found. This option
63
63
  # defaults to true.
64
+ # * *object_class*: Defaults to Hash
65
+ # * *array_class*: Defaults to Array
64
66
  def initialize(source, opts = {})
65
67
  super
66
68
  if !opts.key?(:max_nesting) # defaults to 19
@@ -74,6 +76,8 @@ module JSON
74
76
  ca = true
75
77
  ca = opts[:create_additions] if opts.key?(:create_additions)
76
78
  @create_id = ca ? JSON.create_id : nil
79
+ @object_class = opts[:object_class] || Hash
80
+ @array_class = opts[:array_class] || Array
77
81
  end
78
82
 
79
83
  alias source string
@@ -184,7 +188,7 @@ module JSON
184
188
  def parse_array
185
189
  raise NestingError, "nesting of #@current_nesting is to deep" if
186
190
  @max_nesting.nonzero? && @current_nesting > @max_nesting
187
- result = []
191
+ result = @array_class.new
188
192
  delim = false
189
193
  until eos?
190
194
  case
@@ -216,7 +220,7 @@ module JSON
216
220
  def parse_object
217
221
  raise NestingError, "nesting of #@current_nesting is to deep" if
218
222
  @max_nesting.nonzero? && @current_nesting > @max_nesting
219
- result = {}
223
+ result = @object_class.new
220
224
  delim = false
221
225
  until eos?
222
226
  case
@@ -1,9 +1,8 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.1.4'
3
+ VERSION = '1.1.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:
7
7
  VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
8
- VARIANT_BINARY = false
9
8
  end
@@ -150,6 +150,14 @@ class TC_JSON < Test::Unit::TestCase
150
150
  , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
151
151
  end
152
152
 
153
+ class SubArray < Array; end
154
+
155
+ def test_parse_array_custom_class
156
+ res = parse('[]', :array_class => SubArray)
157
+ assert_equal([], res)
158
+ assert_equal(SubArray, res.class)
159
+ end
160
+
153
161
  def test_parse_object
154
162
  assert_equal({}, parse('{}'))
155
163
  assert_equal({}, parse(' { } '))
@@ -157,6 +165,14 @@ class TC_JSON < Test::Unit::TestCase
157
165
  assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
158
166
  end
159
167
 
168
+ class SubHash < Hash; end
169
+
170
+ def test_parse_object_custom_class
171
+ res = parse('{}', :object_class => SubHash)
172
+ assert_equal({}, res)
173
+ assert_equal(SubHash, res.class)
174
+ end
175
+
160
176
  def test_parser_reset
161
177
  parser = Parser.new(@json)
162
178
  assert_equal(@hash, parser.parse)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-02 00:00:00 +02:00
12
+ date: 2009-05-10 00:00:00 +02:00
13
13
  default_executable: edit_json.rb
14
14
  dependencies: []
15
15
 
@@ -17,145 +17,147 @@ description: ""
17
17
  email: flori@ping.de
18
18
  executables:
19
19
  - edit_json.rb
20
+ - prettify_json.rb
20
21
  extensions:
21
- - ext/json/ext/parser/extconf.rb
22
22
  - ext/json/ext/generator/extconf.rb
23
+ - ext/json/ext/parser/extconf.rb
23
24
  extra_rdoc_files: []
24
25
 
25
26
  files:
26
- - install.rb
27
- - lib
28
- - lib/json.rb
29
- - lib/json
30
- - lib/json/Array.xpm
31
- - lib/json/FalseClass.xpm
32
- - lib/json/json.xpm
33
- - lib/json/editor.rb
34
- - lib/json/Hash.xpm
35
- - lib/json/Key.xpm
36
- - lib/json/common.rb
37
- - lib/json/String.xpm
38
- - lib/json/pure
39
- - lib/json/pure/generator.rb
40
- - lib/json/pure/parser.rb
41
- - lib/json/Numeric.xpm
42
- - lib/json/ext.rb
43
- - lib/json/pure.rb
44
- - lib/json/NilClass.xpm
45
- - lib/json/add
46
- - lib/json/add/rails.rb
47
- - lib/json/add/core.rb
48
- - lib/json/TrueClass.xpm
49
- - lib/json/version.rb
50
- - ext
51
- - ext/json
52
- - ext/json/ext
53
- - ext/json/ext/parser
54
- - ext/json/ext/parser/unicode.h
55
- - ext/json/ext/parser/parser.c
56
- - ext/json/ext/parser/extconf.rb
57
- - ext/json/ext/parser/unicode.c
58
- - ext/json/ext/parser/parser.rl
59
- - ext/json/ext/generator
60
- - ext/json/ext/generator/unicode.h
61
- - ext/json/ext/generator/extconf.rb
62
- - ext/json/ext/generator/generator.c
63
- - ext/json/ext/generator/unicode.c
64
- - README
65
- - diagrams
66
- - CHANGES
67
- - RUBY
68
- - TODO
69
- - VERSION
27
+ - GPL
70
28
  - tests
29
+ - tests/test_json_unicode.rb
71
30
  - tests/test_json.rb
72
31
  - tests/test_json_addition.rb
73
32
  - tests/fixtures
74
- - tests/fixtures/fail11.json
75
- - tests/fixtures/fail5.json
76
- - tests/fixtures/fail10.json
33
+ - tests/fixtures/fail1.json
34
+ - tests/fixtures/fail18.json
35
+ - tests/fixtures/fail13.json
77
36
  - tests/fixtures/fail3.json
78
- - tests/fixtures/pass15.json
79
- - tests/fixtures/fail9.json
80
- - tests/fixtures/fail22.json
37
+ - tests/fixtures/fail10.json
81
38
  - tests/fixtures/fail6.json
82
- - tests/fixtures/pass2.json
83
39
  - tests/fixtures/fail20.json
84
- - tests/fixtures/fail19.json
85
- - tests/fixtures/fail12.json
86
- - tests/fixtures/fail7.json
87
- - tests/fixtures/fail4.json
88
- - tests/fixtures/fail1.json
89
- - tests/fixtures/fail24.json
90
- - tests/fixtures/fail21.json
91
40
  - tests/fixtures/pass1.json
41
+ - tests/fixtures/fail28.json
42
+ - tests/fixtures/fail21.json
43
+ - tests/fixtures/pass26.json
44
+ - tests/fixtures/fail22.json
45
+ - tests/fixtures/pass15.json
46
+ - tests/fixtures/fail11.json
47
+ - tests/fixtures/fail19.json
92
48
  - tests/fixtures/fail2.json
93
- - tests/fixtures/fail25.json
49
+ - tests/fixtures/fail8.json
94
50
  - tests/fixtures/pass16.json
51
+ - tests/fixtures/fail24.json
52
+ - tests/fixtures/fail23.json
95
53
  - tests/fixtures/pass3.json
96
- - tests/fixtures/fail18.json
97
- - tests/fixtures/fail28.json
98
- - tests/fixtures/fail13.json
99
- - tests/fixtures/fail27.json
54
+ - tests/fixtures/fail7.json
55
+ - tests/fixtures/pass2.json
100
56
  - tests/fixtures/pass17.json
101
- - tests/fixtures/pass26.json
102
- - tests/fixtures/fail23.json
103
57
  - tests/fixtures/fail14.json
104
- - tests/fixtures/fail8.json
58
+ - tests/fixtures/fail9.json
59
+ - tests/fixtures/fail12.json
60
+ - tests/fixtures/fail27.json
61
+ - tests/fixtures/fail4.json
62
+ - tests/fixtures/fail5.json
63
+ - tests/fixtures/fail25.json
105
64
  - tests/test_json_generate.rb
106
65
  - tests/test_json_rails.rb
107
- - tests/test_json_unicode.rb
108
66
  - tests/test_json_fixtures.rb
67
+ - install.rb
68
+ - ext
69
+ - ext/json
70
+ - ext/json/ext
71
+ - ext/json/ext/generator
72
+ - ext/json/ext/generator/unicode.h
73
+ - ext/json/ext/generator/extconf.rb
74
+ - ext/json/ext/generator/generator.c
75
+ - ext/json/ext/generator/unicode.c
76
+ - ext/json/ext/parser
77
+ - ext/json/ext/parser/unicode.h
78
+ - ext/json/ext/parser/parser.rl
79
+ - ext/json/ext/parser/extconf.rb
80
+ - ext/json/ext/parser/parser.c
81
+ - ext/json/ext/parser/unicode.c
82
+ - VERSION
83
+ - README
84
+ - tools
85
+ - tools/server.rb
86
+ - tools/fuzz.rb
87
+ - lib
88
+ - lib/json.rb
89
+ - lib/json
90
+ - lib/json/add
91
+ - lib/json/add/core.rb
92
+ - lib/json/add/rails.rb
93
+ - lib/json/ext.rb
94
+ - lib/json/Key.xpm
95
+ - lib/json/Array.xpm
96
+ - lib/json/common.rb
97
+ - lib/json/ext
98
+ - lib/json/Hash.xpm
99
+ - lib/json/String.xpm
100
+ - lib/json/FalseClass.xpm
101
+ - lib/json/Numeric.xpm
102
+ - lib/json/version.rb
103
+ - lib/json/pure
104
+ - lib/json/pure/generator.rb
105
+ - lib/json/pure/parser.rb
106
+ - lib/json/pure.rb
107
+ - lib/json/NilClass.xpm
108
+ - lib/json/json.xpm
109
+ - lib/json/editor.rb
110
+ - lib/json/TrueClass.xpm
111
+ - TODO
112
+ - data
113
+ - data/prototype.js
114
+ - data/index.html
115
+ - data/example.json
116
+ - bin
117
+ - bin/prettify_json.rb
118
+ - bin/edit_json.rb
119
+ - diagrams
120
+ - doc-templates
121
+ - doc-templates/main.txt
122
+ - RUBY
109
123
  - benchmarks
110
- - benchmarks/generator_benchmark.rb
111
124
  - benchmarks/data-p4-3GHz-ruby18
112
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat
113
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat
114
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat
115
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat
125
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat
116
126
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat
117
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat
118
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log
119
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat
127
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat
120
128
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat
121
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat
122
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat
123
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log
129
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat
130
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat
131
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat
124
132
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log
125
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log
126
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat
127
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log
128
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat
129
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat
130
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat
131
133
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat
132
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat
133
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat
134
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log
135
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log
134
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat
135
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat
136
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat
137
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat
138
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat
136
139
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log
137
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat
140
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log
138
141
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat
142
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log
139
143
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log
140
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat
144
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log
145
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log
141
146
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat
142
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat
147
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log
148
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat
149
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat
150
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat
151
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat
152
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log
153
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat
154
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat
155
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat
143
156
  - benchmarks/data
144
157
  - benchmarks/parser_benchmark.rb
158
+ - benchmarks/generator_benchmark.rb
145
159
  - Rakefile
146
- - doc-templates
147
- - doc-templates/main.txt
148
- - GPL
149
- - data
150
- - data/example.json
151
- - data/index.html
152
- - data/prototype.js
153
- - bin
154
- - bin/edit_json.rb
155
- - bin/prettify_json.rb
156
- - tools
157
- - tools/fuzz.rb
158
- - tools/server.rb
160
+ - CHANGES
159
161
  has_rdoc: true
160
162
  homepage: http://json.rubyforge.org
161
163
  post_install_message:
@@ -189,9 +191,9 @@ signing_key:
189
191
  specification_version: 2
190
192
  summary: A JSON implementation as a Ruby extension
191
193
  test_files:
194
+ - tests/test_json_unicode.rb
192
195
  - tests/test_json.rb
193
196
  - tests/test_json_addition.rb
194
197
  - tests/test_json_generate.rb
195
198
  - tests/test_json_rails.rb
196
- - tests/test_json_unicode.rb
197
199
  - tests/test_json_fixtures.rb