json_pure 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
 
data/lib/json.rb CHANGED
@@ -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
data/lib/json/version.rb CHANGED
@@ -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
data/tests/test_json.rb CHANGED
@@ -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_pure
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,144 +17,146 @@ description: ""
17
17
  email: flori@ping.de
18
18
  executables:
19
19
  - edit_json.rb
20
+ - prettify_json.rb
20
21
  extensions: []
21
22
 
22
23
  extra_rdoc_files: []
23
24
 
24
25
  files:
25
- - install.rb
26
- - lib
27
- - lib/json.rb
28
- - lib/json
29
- - lib/json/Array.xpm
30
- - lib/json/FalseClass.xpm
31
- - lib/json/json.xpm
32
- - lib/json/editor.rb
33
- - lib/json/Hash.xpm
34
- - lib/json/Key.xpm
35
- - lib/json/common.rb
36
- - lib/json/String.xpm
37
- - lib/json/pure
38
- - lib/json/pure/generator.rb
39
- - lib/json/pure/parser.rb
40
- - lib/json/Numeric.xpm
41
- - lib/json/ext.rb
42
- - lib/json/pure.rb
43
- - lib/json/NilClass.xpm
44
- - lib/json/add
45
- - lib/json/add/rails.rb
46
- - lib/json/add/core.rb
47
- - lib/json/TrueClass.xpm
48
- - lib/json/version.rb
49
- - ext
50
- - ext/json
51
- - ext/json/ext
52
- - ext/json/ext/parser
53
- - ext/json/ext/parser/unicode.h
54
- - ext/json/ext/parser/parser.c
55
- - ext/json/ext/parser/extconf.rb
56
- - ext/json/ext/parser/unicode.c
57
- - ext/json/ext/parser/parser.rl
58
- - ext/json/ext/generator
59
- - ext/json/ext/generator/unicode.h
60
- - ext/json/ext/generator/extconf.rb
61
- - ext/json/ext/generator/generator.c
62
- - ext/json/ext/generator/unicode.c
63
- - README
64
- - diagrams
65
- - CHANGES
66
- - RUBY
67
- - TODO
68
- - VERSION
26
+ - GPL
69
27
  - tests
28
+ - tests/test_json_unicode.rb
70
29
  - tests/test_json.rb
71
30
  - tests/test_json_addition.rb
72
31
  - tests/fixtures
73
- - tests/fixtures/fail11.json
74
- - tests/fixtures/fail5.json
75
- - tests/fixtures/fail10.json
32
+ - tests/fixtures/fail1.json
33
+ - tests/fixtures/fail18.json
34
+ - tests/fixtures/fail13.json
76
35
  - tests/fixtures/fail3.json
77
- - tests/fixtures/pass15.json
78
- - tests/fixtures/fail9.json
79
- - tests/fixtures/fail22.json
36
+ - tests/fixtures/fail10.json
80
37
  - tests/fixtures/fail6.json
81
- - tests/fixtures/pass2.json
82
38
  - tests/fixtures/fail20.json
83
- - tests/fixtures/fail19.json
84
- - tests/fixtures/fail12.json
85
- - tests/fixtures/fail7.json
86
- - tests/fixtures/fail4.json
87
- - tests/fixtures/fail1.json
88
- - tests/fixtures/fail24.json
89
- - tests/fixtures/fail21.json
90
39
  - tests/fixtures/pass1.json
40
+ - tests/fixtures/fail28.json
41
+ - tests/fixtures/fail21.json
42
+ - tests/fixtures/pass26.json
43
+ - tests/fixtures/fail22.json
44
+ - tests/fixtures/pass15.json
45
+ - tests/fixtures/fail11.json
46
+ - tests/fixtures/fail19.json
91
47
  - tests/fixtures/fail2.json
92
- - tests/fixtures/fail25.json
48
+ - tests/fixtures/fail8.json
93
49
  - tests/fixtures/pass16.json
50
+ - tests/fixtures/fail24.json
51
+ - tests/fixtures/fail23.json
94
52
  - tests/fixtures/pass3.json
95
- - tests/fixtures/fail18.json
96
- - tests/fixtures/fail28.json
97
- - tests/fixtures/fail13.json
98
- - tests/fixtures/fail27.json
53
+ - tests/fixtures/fail7.json
54
+ - tests/fixtures/pass2.json
99
55
  - tests/fixtures/pass17.json
100
- - tests/fixtures/pass26.json
101
- - tests/fixtures/fail23.json
102
56
  - tests/fixtures/fail14.json
103
- - tests/fixtures/fail8.json
57
+ - tests/fixtures/fail9.json
58
+ - tests/fixtures/fail12.json
59
+ - tests/fixtures/fail27.json
60
+ - tests/fixtures/fail4.json
61
+ - tests/fixtures/fail5.json
62
+ - tests/fixtures/fail25.json
104
63
  - tests/test_json_generate.rb
105
64
  - tests/test_json_rails.rb
106
- - tests/test_json_unicode.rb
107
65
  - tests/test_json_fixtures.rb
66
+ - install.rb
67
+ - ext
68
+ - ext/json
69
+ - ext/json/ext
70
+ - ext/json/ext/generator
71
+ - ext/json/ext/generator/unicode.h
72
+ - ext/json/ext/generator/extconf.rb
73
+ - ext/json/ext/generator/generator.c
74
+ - ext/json/ext/generator/unicode.c
75
+ - ext/json/ext/parser
76
+ - ext/json/ext/parser/unicode.h
77
+ - ext/json/ext/parser/parser.rl
78
+ - ext/json/ext/parser/extconf.rb
79
+ - ext/json/ext/parser/parser.c
80
+ - ext/json/ext/parser/unicode.c
81
+ - VERSION
82
+ - README
83
+ - tools
84
+ - tools/server.rb
85
+ - tools/fuzz.rb
86
+ - lib
87
+ - lib/json.rb
88
+ - lib/json
89
+ - lib/json/add
90
+ - lib/json/add/core.rb
91
+ - lib/json/add/rails.rb
92
+ - lib/json/ext.rb
93
+ - lib/json/Key.xpm
94
+ - lib/json/Array.xpm
95
+ - lib/json/common.rb
96
+ - lib/json/ext
97
+ - lib/json/Hash.xpm
98
+ - lib/json/String.xpm
99
+ - lib/json/FalseClass.xpm
100
+ - lib/json/Numeric.xpm
101
+ - lib/json/version.rb
102
+ - lib/json/pure
103
+ - lib/json/pure/generator.rb
104
+ - lib/json/pure/parser.rb
105
+ - lib/json/pure.rb
106
+ - lib/json/NilClass.xpm
107
+ - lib/json/json.xpm
108
+ - lib/json/editor.rb
109
+ - lib/json/TrueClass.xpm
110
+ - TODO
111
+ - data
112
+ - data/prototype.js
113
+ - data/index.html
114
+ - data/example.json
115
+ - bin
116
+ - bin/prettify_json.rb
117
+ - bin/edit_json.rb
118
+ - diagrams
119
+ - doc-templates
120
+ - doc-templates/main.txt
121
+ - RUBY
108
122
  - benchmarks
109
- - benchmarks/generator_benchmark.rb
110
123
  - benchmarks/data-p4-3GHz-ruby18
111
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat
112
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat
113
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat
114
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat
124
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat
115
125
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat
116
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat
117
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log
118
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat
126
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat
119
127
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat
120
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat
121
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat
122
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log
128
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat
129
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat
130
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat
123
131
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log
124
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log
125
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat
126
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log
127
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat
128
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat
129
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat
130
132
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat
131
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat
132
- - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat
133
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log
134
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log
133
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat
134
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat
135
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat
136
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat
137
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat
135
138
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log
136
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat
139
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log
137
140
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat
141
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log
138
142
  - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log
139
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat
143
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log
144
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log
140
145
  - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat
141
- - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat
146
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log
147
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat
148
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat
149
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat
150
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat
151
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log
152
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat
153
+ - benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat
154
+ - benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat
142
155
  - benchmarks/data
143
156
  - benchmarks/parser_benchmark.rb
157
+ - benchmarks/generator_benchmark.rb
144
158
  - Rakefile
145
- - doc-templates
146
- - doc-templates/main.txt
147
- - GPL
148
- - data
149
- - data/example.json
150
- - data/index.html
151
- - data/prototype.js
152
- - bin
153
- - bin/edit_json.rb
154
- - bin/prettify_json.rb
155
- - tools
156
- - tools/fuzz.rb
157
- - tools/server.rb
159
+ - CHANGES
158
160
  has_rdoc: true
159
161
  homepage: http://json.rubyforge.org
160
162
  post_install_message:
@@ -186,9 +188,9 @@ signing_key:
186
188
  specification_version: 2
187
189
  summary: A JSON implementation in Ruby
188
190
  test_files:
191
+ - tests/test_json_unicode.rb
189
192
  - tests/test_json.rb
190
193
  - tests/test_json_addition.rb
191
194
  - tests/test_json_generate.rb
192
195
  - tests/test_json_rails.rb
193
- - tests/test_json_unicode.rb
194
196
  - tests/test_json_fixtures.rb