json_pure 1.4.5 → 1.4.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ 2010-08-09 (1.4.6)
2
+ * Fixed oversight reported in http://github.com/flori/json/issues/closed#issue/23,
3
+ always create a new object from the state prototype.
4
+ * Made pure and ext api more similar again.
1
5
  2010-08-07 (1.4.5)
2
6
  * Manage data structure nesting depth in state object during generation. This
3
7
  should reduce problems with to_json method definіtions that only have one
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.5
1
+ 1.4.6
@@ -14,7 +14,7 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
14
14
  static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
15
15
  i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
16
16
  i_pack, i_unpack, i_create_id, i_extend, i_key_p, i_aref, i_send,
17
- i_respond_to_p, i_match, i_keys, i_depth;
17
+ i_respond_to_p, i_match, i_keys, i_depth, i_dup;
18
18
 
19
19
  /*
20
20
  * Copyright 2001-2004 Unicode, Inc.
@@ -748,7 +748,7 @@ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
748
748
  VALUE key, key_to_s, keys;
749
749
  if (max_nesting != 0 && depth > max_nesting) {
750
750
  fbuffer_free(buffer);
751
- rb_raise(eNestingError, "nesting of %ld is too deep", depth);
751
+ rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
752
752
  }
753
753
  fbuffer_append_char(buffer, '{');
754
754
  keys = rb_funcall(obj, i_keys, 0);
@@ -794,7 +794,7 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
794
794
  int i, j;
795
795
  if (max_nesting != 0 && depth > max_nesting) {
796
796
  fbuffer_free(buffer);
797
- rb_raise(eNestingError, "nesting of %ld is too deep", depth);
797
+ rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
798
798
  }
799
799
  fbuffer_append_char(buffer, '[');
800
800
  if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
@@ -1043,7 +1043,7 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1043
1043
  if (NIL_P(CJSON_SAFE_STATE_PROTOTYPE)) {
1044
1044
  CJSON_SAFE_STATE_PROTOTYPE = rb_const_get(mJSON, i_SAFE_STATE_PROTOTYPE);
1045
1045
  }
1046
- return CJSON_SAFE_STATE_PROTOTYPE;
1046
+ return rb_funcall(CJSON_SAFE_STATE_PROTOTYPE, i_dup, 0);
1047
1047
  }
1048
1048
  }
1049
1049
 
@@ -1406,6 +1406,7 @@ void Init_generator()
1406
1406
  i_respond_to_p = rb_intern("respond_to?");
1407
1407
  i_match = rb_intern("match");
1408
1408
  i_keys = rb_intern("keys");
1409
+ i_dup = rb_intern("dup");
1409
1410
  #ifdef HAVE_RUBY_ENCODING_H
1410
1411
  CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1411
1412
  i_encoding = rb_intern("encoding");
@@ -112,7 +112,7 @@ module JSON
112
112
  when Hash
113
113
  new(opts)
114
114
  else
115
- SAFE_STATE_PROTOTYPE
115
+ SAFE_STATE_PROTOTYPE.dup
116
116
  end
117
117
  end
118
118
 
@@ -167,7 +167,7 @@ module JSON
167
167
  # generated JSON.
168
168
  attr_accessor :depth
169
169
 
170
- def check_max_nesting(depth) # :nodoc:
170
+ def check_max_nesting # :nodoc:
171
171
  return if @max_nesting.zero?
172
172
  current_nesting = depth + 1
173
173
  current_nesting > @max_nesting and
@@ -252,51 +252,41 @@ module JSON
252
252
  # _state_ is a JSON::State object, that can also be used to configure the
253
253
  # produced JSON string output further.
254
254
  # _depth_ is used to find out nesting depth, to indent accordingly.
255
- def to_json(state = nil, depth = 0, *)
256
- if state
257
- state = State.from_state(state)
258
- state.check_max_nesting(depth)
259
- end
260
- json_transform(state, depth)
255
+ def to_json(state = nil, *)
256
+ state = State.from_state(state)
257
+ state.check_max_nesting
258
+ json_transform(state)
261
259
  end
262
260
 
263
261
  private
264
262
 
265
- def json_shift(state, depth)
266
- state and not state.object_nl.empty? or return ''
267
- state.indent * depth
263
+ def json_shift(state)
264
+ state.object_nl.empty? or return ''
265
+ state.indent * state.depth
268
266
  end
269
267
 
270
- def json_transform(state, depth)
268
+ def json_transform(state)
271
269
  delim = ','
272
- if state
273
- delim << state.object_nl
274
- result = '{'
275
- result << state.object_nl
276
- depth += 1
277
- first = true
278
- indent = state && !state.object_nl.empty?
279
- each { |key,value|
280
- result << delim unless first
281
- result << state.indent * depth if indent
282
- result << key.to_s.to_json(state, depth)
283
- result << state.space_before
284
- result << ':'
285
- result << state.space
286
- result << value.to_json(state, depth)
287
- first = false
288
- }
289
- depth -= 1
290
- result << state.object_nl
291
- result << state.indent * depth if indent if indent
292
- result << '}'
293
- else
294
- result = '{'
295
- result << map { |key,value|
296
- key.to_s.to_json << ':' << value.to_json
297
- }.join(delim)
298
- result << '}'
299
- end
270
+ delim << state.object_nl
271
+ result = '{'
272
+ result << state.object_nl
273
+ depth = state.depth += 1
274
+ first = true
275
+ indent = !state.object_nl.empty?
276
+ each { |key,value|
277
+ result << delim unless first
278
+ result << state.indent * depth if indent
279
+ result << key.to_s.to_json(state)
280
+ result << state.space_before
281
+ result << ':'
282
+ result << state.space
283
+ result << value.to_json(state)
284
+ first = false
285
+ }
286
+ depth = state.depth -= 1
287
+ result << state.object_nl
288
+ result << state.indent * depth if indent if indent
289
+ result << '}'
300
290
  result
301
291
  end
302
292
  end
@@ -306,39 +296,32 @@ module JSON
306
296
  # this Array instance.
307
297
  # _state_ is a JSON::State object, that can also be used to configure the
308
298
  # produced JSON string output further.
309
- # _depth_ is used to find out nesting depth, to indent accordingly.
310
- def to_json(state = nil, depth = 0, *)
311
- if state
312
- state = State.from_state(state)
313
- state.check_max_nesting(depth)
314
- end
315
- json_transform(state, depth)
299
+ def to_json(state = nil, *)
300
+ state = State.from_state(state)
301
+ state.check_max_nesting
302
+ json_transform(state)
316
303
  end
317
304
 
318
305
  private
319
306
 
320
- def json_transform(state, depth)
307
+ def json_transform(state)
321
308
  delim = ','
322
- if state
323
- delim << state.array_nl
324
- result = '['
325
- result << state.array_nl
326
- depth += 1
327
- first = true
328
- indent = state && !state.array_nl.empty?
329
- each { |value|
330
- result << delim unless first
331
- result << state.indent * depth if indent
332
- result << value.to_json(state, depth)
333
- first = false
334
- }
335
- depth -= 1
336
- result << state.array_nl
309
+ delim << state.array_nl
310
+ result = '['
311
+ result << state.array_nl
312
+ depth = state.depth += 1
313
+ first = true
314
+ indent = !state.array_nl.empty?
315
+ each { |value|
316
+ result << delim unless first
337
317
  result << state.indent * depth if indent
338
- result << ']'
339
- else
340
- '[' << map { |value| value.to_json }.join(delim) << ']'
341
- end
318
+ result << value.to_json(state)
319
+ first = false
320
+ }
321
+ depth = state.depth -= 1
322
+ result << state.array_nl
323
+ result << state.indent * depth if indent
324
+ result << ']'
342
325
  end
343
326
  end
344
327
 
@@ -350,15 +333,16 @@ module JSON
350
333
  module Float
351
334
  # Returns a JSON string representation for this Float number.
352
335
  def to_json(state = nil, *)
336
+ state = State.from_state(state)
353
337
  case
354
338
  when infinite?
355
- if state && state.allow_nan?
339
+ if state.allow_nan?
356
340
  to_s
357
341
  else
358
342
  raise GeneratorError, "#{self} not allowed in JSON"
359
343
  end
360
344
  when nan?
361
- if state && state.allow_nan?
345
+ if state.allow_nan?
362
346
  to_s
363
347
  else
364
348
  raise GeneratorError, "#{self} not allowed in JSON"
@@ -374,9 +358,8 @@ module JSON
374
358
  # This string should be encoded with UTF-8 A call to this method
375
359
  # returns a JSON string encoded with UTF16 big endian characters as
376
360
  # \u????.
377
- def to_json(*args)
378
- state, = *args
379
- state ||= State.from_state(state)
361
+ def to_json(state = nil, *args)
362
+ state = State.from_state(state)
380
363
  if encoding == ::Encoding::UTF_8
381
364
  string = self
382
365
  else
@@ -392,9 +375,8 @@ module JSON
392
375
  # This string should be encoded with UTF-8 A call to this method
393
376
  # returns a JSON string encoded with UTF16 big endian characters as
394
377
  # \u????.
395
- def to_json(*args)
396
- state, = *args
397
- state ||= State.from_state(state)
378
+ def to_json(state = nil, *args)
379
+ state = State.from_state(state)
398
380
  if state.ascii_only?
399
381
  '"' << JSON.utf8_to_json_ascii(self) << '"'
400
382
  else
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.4.5'
3
+ VERSION = '1.4.6'
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:
@@ -164,4 +164,18 @@ EOT
164
164
  assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
165
165
  assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
166
166
  end
167
+
168
+ def test_depth
169
+ ary = []; ary << ary
170
+ assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
171
+ assert_raises(JSON::NestingError) { JSON.generate(ary) }
172
+ assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
173
+ assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
174
+ assert_raises(JSON::NestingError) { JSON.pretty_generate(ary) }
175
+ assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
176
+ s = JSON.state.new
177
+ assert_equal 0, s.depth
178
+ assert_raises(JSON::NestingError) { ary.to_json(s) }
179
+ assert_equal 19, s.depth
180
+ end
167
181
  end
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_pure
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 4
9
- - 5
10
- version: 1.4.5
4
+ version: 1.4.6
11
5
  platform: ruby
12
6
  authors:
13
7
  - Florian Frank
@@ -15,7 +9,7 @@ autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
11
 
18
- date: 2010-08-07 00:00:00 +02:00
12
+ date: 2010-08-09 00:00:00 +02:00
19
13
  default_executable: edit_json.rb
20
14
  dependencies: []
21
15
 
@@ -158,27 +152,21 @@ rdoc_options:
158
152
  require_paths:
159
153
  - lib
160
154
  required_ruby_version: !ruby/object:Gem::Requirement
161
- none: false
162
155
  requirements:
163
156
  - - ">="
164
157
  - !ruby/object:Gem::Version
165
- hash: 3
166
- segments:
167
- - 0
168
158
  version: "0"
159
+ version:
169
160
  required_rubygems_version: !ruby/object:Gem::Requirement
170
- none: false
171
161
  requirements:
172
162
  - - ">="
173
163
  - !ruby/object:Gem::Version
174
- hash: 3
175
- segments:
176
- - 0
177
164
  version: "0"
165
+ version:
178
166
  requirements: []
179
167
 
180
168
  rubyforge_project: json
181
- rubygems_version: 1.3.7
169
+ rubygems_version: 1.3.5
182
170
  signing_key:
183
171
  specification_version: 3
184
172
  summary: JSON Implementation for Ruby