json 1.4.6-java → 1.5.0-java

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.

@@ -1,8 +1,7 @@
1
1
  # This file contains implementations of ruby core's custom objects for
2
2
  # serialisation/deserialisation.
3
3
 
4
- unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
- ::JSON::JSON_LOADED
4
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
6
5
  require 'json'
7
6
  end
8
7
  require 'date'
@@ -1,58 +1,8 @@
1
- # This file contains implementations of rails custom objects for
2
- # serialisation/deserialisation.
1
+ # This file used to implementations of rails custom objects for
2
+ # serialisation/deserialisation and is obsoleted now.
3
3
 
4
- unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
- ::JSON::JSON_LOADED
4
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
6
5
  require 'json'
7
6
  end
8
7
 
9
- class Object
10
- def self.json_create(object)
11
- obj = new
12
- for key, value in object
13
- next if key == JSON.create_id
14
- instance_variable_set "@#{key}", value
15
- end
16
- obj
17
- end
18
-
19
- def to_json(*a)
20
- result = {
21
- JSON.create_id => self.class.name
22
- }
23
- instance_variables.inject(result) do |r, name|
24
- r[name[1..-1]] = instance_variable_get name
25
- r
26
- end
27
- result.to_json(*a)
28
- end
29
- end
30
-
31
- class Symbol
32
- def to_json(*a)
33
- to_s.to_json(*a)
34
- end
35
- end
36
-
37
- module Enumerable
38
- def to_json(*a)
39
- to_a.to_json(*a)
40
- end
41
- end
42
-
43
- # class Regexp
44
- # def to_json(*)
45
- # inspect
46
- # end
47
- # end
48
- #
49
- # The above rails definition has some problems:
50
- #
51
- # 1. { 'foo' => /bar/ }.to_json # => "{foo: /bar/}"
52
- # This isn't valid JSON, because the regular expression syntax is not
53
- # defined in RFC 4627. (And unquoted strings are disallowed there, too.)
54
- # Though it is valid Javascript.
55
- #
56
- # 2. { 'foo' => /bar/mix }.to_json # => "{foo: /bar/mix}"
57
- # This isn't even valid Javascript.
58
-
8
+ $DEBUG and warn "required json/add/rails which is obsolete now!"
@@ -1,5 +1,4 @@
1
1
  require 'json/version'
2
- require 'iconv'
3
2
 
4
3
  module JSON
5
4
  class << self
@@ -24,7 +23,7 @@ module JSON
24
23
  # Set the JSON parser class _parser_ to be used by JSON.
25
24
  def parser=(parser) # :nodoc:
26
25
  @parser = parser
27
- remove_const :Parser if const_defined? :Parser
26
+ remove_const :Parser if JSON.const_defined_in?(self, :Parser)
28
27
  const_set :Parser, parser
29
28
  end
30
29
 
@@ -35,13 +34,13 @@ module JSON
35
34
  def deep_const_get(path) # :nodoc:
36
35
  path.to_s.split(/::/).inject(Object) do |p, c|
37
36
  case
38
- when c.empty? then p
39
- when p.const_defined?(c) then p.const_get(c)
37
+ when c.empty? then p
38
+ when JSON.const_defined_in?(p, c) then p.const_get(c)
40
39
  else
41
40
  begin
42
41
  p.const_missing(c)
43
- rescue NameError
44
- raise ArgumentError, "can't find const #{path}"
42
+ rescue NameError => e
43
+ raise ArgumentError, "can't get const #{path}: #{e}"
45
44
  end
46
45
  end
47
46
  end
@@ -49,6 +48,7 @@ module JSON
49
48
 
50
49
  # Set the module _generator_ to be used by JSON.
51
50
  def generator=(generator) # :nodoc:
51
+ old, $VERBOSE = $VERBOSE, nil
52
52
  @generator = generator
53
53
  generator_methods = generator::GeneratorMethods
54
54
  for const in generator_methods.constants
@@ -77,6 +77,8 @@ module JSON
77
77
  :object_nl => "\n",
78
78
  :array_nl => "\n"
79
79
  )
80
+ ensure
81
+ $VERBOSE = old
80
82
  end
81
83
 
82
84
  # Returns the JSON generator modul, that is used by JSON. This might be
@@ -338,9 +340,35 @@ module JSON
338
340
  raise ArgumentError, "exceed depth limit"
339
341
  end
340
342
 
343
+ # Swap consecutive bytes of _string_ in place.
344
+ def self.swap!(string) # :nodoc:
345
+ 0.upto(string.size / 2) do |i|
346
+ break unless string[2 * i + 1]
347
+ string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
348
+ end
349
+ string
350
+ end
351
+
341
352
  # Shortuct for iconv.
342
- def self.iconv(to, from, string)
343
- Iconv.iconv(to, from, string).first
353
+ if ::String.method_defined?(:encode)
354
+ def self.iconv(to, from, string)
355
+ string.encode(to, from)
356
+ end
357
+ else
358
+ require 'iconv'
359
+ def self.iconv(to, from, string)
360
+ Iconv.iconv(to, from, string).first
361
+ end
362
+ end
363
+
364
+ if ::Object.method(:const_defined?).arity == 1
365
+ def self.const_defined_in?(modul, constant)
366
+ modul.const_defined?(constant)
367
+ end
368
+ else
369
+ def self.const_defined_in?(modul, constant)
370
+ modul.const_defined?(constant, false)
371
+ end
344
372
  end
345
373
  end
346
374
 
@@ -2,7 +2,6 @@
2
2
  # requires ruby-gtk to be installed.
3
3
 
4
4
  require 'gtk2'
5
- require 'iconv'
6
5
  require 'json'
7
6
  require 'rbconfig'
8
7
  require 'open-uri'
@@ -1272,8 +1271,7 @@ module JSON
1272
1271
  def parse_json(json)
1273
1272
  check_pretty_printed(json)
1274
1273
  if @encoding && !/^utf8$/i.match(@encoding)
1275
- iconverter = Iconv.new('utf8', @encoding)
1276
- json = iconverter.iconv(json)
1274
+ json = JSON.iconv 'utf-8', @encoding, json
1277
1275
  end
1278
1276
  JSON::parse(json, :max_nesting => false, :create_additions => false)
1279
1277
  end
@@ -6,10 +6,10 @@ module JSON
6
6
  module Ext
7
7
  require 'json/ext/parser'
8
8
  require 'json/ext/generator'
9
- $DEBUG and warn "Using c extension for JSON."
9
+ $DEBUG and warn "Using Ext extension for JSON."
10
10
  JSON.parser = Parser
11
11
  JSON.generator = Generator
12
12
  end
13
13
 
14
- JSON_LOADED = true
14
+ JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
15
15
  end
Binary file
Binary file
@@ -3,75 +3,13 @@ require 'json/pure/parser'
3
3
  require 'json/pure/generator'
4
4
 
5
5
  module JSON
6
- begin
7
- require 'iconv'
8
- # An iconv instance to convert from UTF8 to UTF16 Big Endian.
9
- UTF16toUTF8 = Iconv.new('utf-8', 'utf-16be') # :nodoc:
10
- # An iconv instance to convert from UTF16 Big Endian to UTF8.
11
- UTF8toUTF16 = Iconv.new('utf-16be', 'utf-8') # :nodoc:
12
- UTF8toUTF16.iconv('no bom')
13
- rescue LoadError
14
- raise MissingUnicodeSupport,
15
- "iconv couldn't be loaded, which is required for UTF-8/UTF-16 conversions"
16
- rescue Errno::EINVAL, Iconv::InvalidEncoding
17
- # Iconv doesn't support big endian utf-16. Let's try to hack this manually
18
- # into the converters.
19
- begin
20
- old_verbose, $VERBSOSE = $VERBOSE, nil
21
- # An iconv instance to convert from UTF8 to UTF16 Big Endian.
22
- UTF16toUTF8 = Iconv.new('utf-8', 'utf-16') # :nodoc:
23
- # An iconv instance to convert from UTF16 Big Endian to UTF8.
24
- UTF8toUTF16 = Iconv.new('utf-16', 'utf-8') # :nodoc:
25
- UTF8toUTF16.iconv('no bom')
26
- if UTF8toUTF16.iconv("\xe2\x82\xac") == "\xac\x20"
27
- swapper = Class.new do
28
- def initialize(iconv) # :nodoc:
29
- @iconv = iconv
30
- end
31
-
32
- def iconv(string) # :nodoc:
33
- result = @iconv.iconv(string)
34
- JSON.swap!(result)
35
- end
36
- end
37
- UTF8toUTF16 = swapper.new(UTF8toUTF16) # :nodoc:
38
- end
39
- if UTF16toUTF8.iconv("\xac\x20") == "\xe2\x82\xac"
40
- swapper = Class.new do
41
- def initialize(iconv) # :nodoc:
42
- @iconv = iconv
43
- end
44
-
45
- def iconv(string) # :nodoc:
46
- string = JSON.swap!(string.dup)
47
- @iconv.iconv(string)
48
- end
49
- end
50
- UTF16toUTF8 = swapper.new(UTF16toUTF8) # :nodoc:
51
- end
52
- rescue Errno::EINVAL, Iconv::InvalidEncoding
53
- raise MissingUnicodeSupport, "iconv doesn't seem to support UTF-8/UTF-16 conversions"
54
- ensure
55
- $VERBOSE = old_verbose
56
- end
57
- end
58
-
59
- # Swap consecutive bytes of _string_ in place.
60
- def self.swap!(string) # :nodoc:
61
- 0.upto(string.size / 2) do |i|
62
- break unless string[2 * i + 1]
63
- string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
64
- end
65
- string
66
- end
67
-
68
6
  # This module holds all the modules/classes that implement JSON's
69
7
  # functionality in pure ruby.
70
8
  module Pure
71
- $DEBUG and warn "Using pure library for JSON."
9
+ $DEBUG and warn "Using Pure library for JSON."
72
10
  JSON.parser = Parser
73
11
  JSON.generator = Generator
74
12
  end
75
13
 
76
- JSON_LOADED = true
14
+ JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
77
15
  end
@@ -62,12 +62,12 @@ module JSON
62
62
  [\x80-\xc1\xf5-\xff] # invalid
63
63
  )/nx) { |c|
64
64
  c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
65
- s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
65
+ s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
66
66
  s.gsub!(/.{4}/n, '\\\\u\&')
67
67
  }
68
68
  string.force_encoding(::Encoding::UTF_8)
69
69
  string
70
- rescue Iconv::Failure => e
70
+ rescue => e
71
71
  raise GeneratorError, "Caught #{e.class}: #{e}"
72
72
  end
73
73
  else
@@ -86,11 +86,11 @@ module JSON
86
86
  [\x80-\xc1\xf5-\xff] # invalid
87
87
  )/nx) { |c|
88
88
  c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
89
- s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
89
+ s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
90
90
  s.gsub!(/.{4}/n, '\\\\u\&')
91
91
  }
92
92
  string
93
- rescue Iconv::Failure => e
93
+ rescue => e
94
94
  raise GeneratorError, "Caught #{e.class}: #{e}"
95
95
  end
96
96
  end
@@ -106,11 +106,13 @@ module JSON
106
106
  # an unconfigured instance. If _opts_ is a State object, it is just
107
107
  # returned.
108
108
  def self.from_state(opts)
109
- case opts
110
- when self
109
+ case
110
+ when self === opts
111
111
  opts
112
- when Hash
113
- new(opts)
112
+ when opts.respond_to?(:to_hash)
113
+ new(opts.to_hash)
114
+ when opts.respond_to?(:to_h)
115
+ new(opts.to_h)
114
116
  else
115
117
  SAFE_STATE_PROTOTYPE.dup
116
118
  end
@@ -113,13 +113,13 @@ module JSON
113
113
  else
114
114
  @max_nesting = 0
115
115
  end
116
- @allow_nan = !!opts[:allow_nan]
117
- @symbolize_names = !!opts[:symbolize_names]
118
- ca = true
119
- ca = opts[:create_additions] if opts.key?(:create_additions)
120
- @create_id = ca ? JSON.create_id : nil
121
- @object_class = opts[:object_class] || Hash
122
- @array_class = opts[:array_class] || Array
116
+ @allow_nan = !!opts[:allow_nan]
117
+ @symbolize_names = !!opts[:symbolize_names]
118
+ @create_additions = opts.key?(:create_additions) ? !!opts[:create_additions] : true
119
+ @create_id = opts[:create_id] || JSON.create_id
120
+ @object_class = opts[:object_class] || Hash
121
+ @array_class = opts[:array_class] || Array
122
+ @match_string = opts[:match_string]
123
123
  end
124
124
 
125
125
  alias source string
@@ -165,6 +165,11 @@ module JSON
165
165
  ?u => nil,
166
166
  })
167
167
 
168
+ EMPTY_8BIT_STRING = ''
169
+ if ::String.method_defined?(:encode)
170
+ EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
171
+ end
172
+
168
173
  def parse_string
169
174
  if scan(STRING)
170
175
  return '' if self[1].empty?
@@ -172,24 +177,30 @@ module JSON
172
177
  if u = UNESCAPE_MAP[$&[1]]
173
178
  u
174
179
  else # \uXXXX
175
- bytes = ''
180
+ bytes = EMPTY_8BIT_STRING.dup
176
181
  i = 0
177
182
  while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
178
183
  bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
179
184
  i += 1
180
185
  end
181
- JSON::UTF16toUTF8.iconv(bytes)
186
+ JSON.iconv('utf-8', 'utf-16be', bytes)
182
187
  end
183
188
  end
184
189
  if string.respond_to?(:force_encoding)
185
190
  string.force_encoding(::Encoding::UTF_8)
186
191
  end
192
+ if @create_additions and @match_string
193
+ for (regexp, klass) in @match_string
194
+ klass.json_creatable? or next
195
+ string =~ regexp and return klass.json_create(string)
196
+ end
197
+ end
187
198
  string
188
199
  else
189
200
  UNPARSED
190
201
  end
191
- rescue Iconv::Failure => e
192
- raise GeneratorError, "Caught #{e.class}: #{e}"
202
+ rescue => e
203
+ raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
193
204
  end
194
205
 
195
206
  def parse_value
@@ -290,7 +301,7 @@ module JSON
290
301
  if delim
291
302
  raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
292
303
  end
293
- if @create_id and klassname = result[@create_id]
304
+ if @create_additions and klassname = result[@create_id]
294
305
  klass = JSON.deep_const_get klassname
295
306
  break unless klass and klass.json_creatable?
296
307
  result = klass.json_create(result)
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.4.6'
3
+ VERSION = '1.5.0'
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:
@@ -0,0 +1,11 @@
1
+ case ENV['JSON']
2
+ when 'pure'
3
+ $:.unshift 'lib'
4
+ require 'json/pure'
5
+ when 'ext'
6
+ $:.unshift 'ext', 'lib'
7
+ require 'json/ext'
8
+ else
9
+ $:.unshift 'ext', 'lib'
10
+ require 'json'
11
+ end
@@ -2,11 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
10
6
  require 'stringio'
11
7
 
12
8
  unless Array.method_defined?(:permutation)
@@ -2,12 +2,8 @@
2
2
  # -*- coding:utf-8 -*-
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
10
- require 'json/add/core'
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
6
+ load 'json/add/core.rb'
11
7
  require 'date'
12
8
 
13
9
  class TC_JSONAddition < Test::Unit::TestCase
@@ -36,6 +32,15 @@ class TC_JSONAddition < Test::Unit::TestCase
36
32
  end
37
33
  end
38
34
 
35
+ class A2 < A
36
+ def to_json(*args)
37
+ {
38
+ 'json_class' => self.class.name,
39
+ 'args' => [ @a ],
40
+ }.to_json(*args)
41
+ end
42
+ end
43
+
39
44
  class B
40
45
  def self.json_creatable?
41
46
  false
@@ -110,9 +115,9 @@ class TC_JSONAddition < Test::Unit::TestCase
110
115
  json_raw_object = raw.to_json_raw_object
111
116
  hash = { 'json_class' => 'String', 'raw'=> raw_array }
112
117
  assert_equal hash, json_raw_object
113
- assert_match /\A\{.*\}\Z/, json
114
- assert_match /"json_class":"String"/, json
115
- 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
118
+ assert_match(/\A\{.*\}\Z/, json)
119
+ assert_match(/"json_class":"String"/, json)
120
+ 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)
116
121
  raw_again = JSON.parse(json)
117
122
  assert_equal raw, raw_again
118
123
  end
@@ -2,12 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
10
- require 'iconv'
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
11
6
 
12
7
  class TC_JSONEncoding < Test::Unit::TestCase
13
8
  include JSON
@@ -15,19 +10,21 @@ class TC_JSONEncoding < Test::Unit::TestCase
15
10
  def setup
16
11
  @utf_8 = '["© ≠ €!"]'
17
12
  @parsed = [ "© ≠ €!" ]
18
- @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
19
13
  @generated = '["\u00a9 \u2260 \u20ac!"]'
20
- if defined?(::Encoding)
14
+ if String.method_defined?(:encode)
15
+ @utf_16_data = [@parsed.first.encode('utf-16be', 'utf-8')]
21
16
  @utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
22
- @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
17
+ @utf_16be = @utf_8.encode('utf-16be', 'utf-8')
23
18
  @utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
24
- @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
19
+ @utf_16le = @utf_8.encode('utf-16le', 'utf-8')
25
20
  @utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
26
- @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
21
+ @utf_32be = @utf_8.encode('utf-32be', 'utf-8')
27
22
  @utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
28
- @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
23
+ @utf_32le = @utf_8.encode('utf-32le', 'utf-8')
29
24
  @utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
30
25
  else
26
+ require 'iconv'
27
+ @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
31
28
  @utf_8_ascii_8bit = @utf_8.dup
32
29
  @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
33
30
  @utf_16be_ascii_8bit = @utf_16be.dup
@@ -2,11 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
10
6
 
11
7
  class TC_JSONFixtures < Test::Unit::TestCase
12
8
  def setup
@@ -18,15 +14,20 @@ class TC_JSONFixtures < Test::Unit::TestCase
18
14
 
19
15
  def test_passing
20
16
  for name, source in @passed
21
- assert JSON.parse(source),
22
- "Did not pass for fixture '#{name}'"
17
+ begin
18
+ assert JSON.parse(source),
19
+ "Did not pass for fixture '#{name}': #{source.inspect}"
20
+ rescue => e
21
+ warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
22
+ raise e
23
+ end
23
24
  end
24
25
  end
25
26
 
26
27
  def test_failing
27
28
  for name, source in @failed
28
29
  assert_raises(JSON::ParserError, JSON::NestingError,
29
- "Did not fail for fixture '#{name}'") do
30
+ "Did not fail for fixture '#{name}': #{source.inspect}") do
30
31
  JSON.parse(source)
31
32
  end
32
33
  end
@@ -2,11 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
10
6
 
11
7
  class TC_JSONGenerate < Test::Unit::TestCase
12
8
  include JSON
@@ -84,6 +80,8 @@ EOT
84
80
  assert_raise(GeneratorError) { fast_generate(666) }
85
81
  end
86
82
 
83
+
84
+
87
85
  def test_states
88
86
  json = generate({1=>2}, nil)
89
87
  assert_equal('{"1":2}', json)
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'test/unit'
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
6
+ require 'stringio'
7
+ require 'time'
8
+
9
+ class TestJsonStringMatching < Test::Unit::TestCase
10
+ include JSON
11
+
12
+ class TestTime < ::Time
13
+ def self.json_create(string)
14
+ Time.parse(string)
15
+ end
16
+
17
+ def to_json(*)
18
+ %{"#{strftime('%FT%T%z')}"}
19
+ end
20
+
21
+ def ==(other)
22
+ to_i == other.to_i
23
+ end
24
+ end
25
+
26
+ def test_match_date
27
+ t = TestTime.new
28
+ t_json = [ t ].to_json
29
+ assert_equal [ t ],
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 })
32
+ assert_equal [ t.strftime('%FT%T%z') ],
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 })
35
+ assert_equal [ t.strftime('%FT%T%z') ],
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 },
38
+ :create_additions => false)
39
+ end
40
+ end
@@ -2,11 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
5
+ require File.join(File.dirname(__FILE__), 'setup_variant')
10
6
 
11
7
  class TC_JSONUnicode < Test::Unit::TestCase
12
8
  include JSON
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 4
8
- - 6
9
- version: 1.4.6
7
+ - 5
8
+ - 0
9
+ version: 1.5.0
10
10
  platform: java
11
11
  authors:
12
12
  - Daniel Luz
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-12 00:00:00 -05:00
17
+ date: 2011-01-23 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -28,64 +28,65 @@ extra_rdoc_files: []
28
28
 
29
29
  files:
30
30
  - lib/json.rb
31
- - lib/json/Array.xpm
32
- - lib/json/common.rb
33
- - lib/json/editor.rb
34
- - lib/json/ext.rb
35
- - lib/json/FalseClass.xpm
36
- - lib/json/Hash.xpm
37
31
  - lib/json/json.xpm
38
32
  - lib/json/Key.xpm
39
- - lib/json/NilClass.xpm
40
- - lib/json/Numeric.xpm
41
- - lib/json/pure.rb
42
33
  - lib/json/String.xpm
34
+ - lib/json/Numeric.xpm
35
+ - lib/json/Hash.xpm
36
+ - lib/json/common.rb
37
+ - lib/json/Array.xpm
38
+ - lib/json/FalseClass.xpm
43
39
  - lib/json/TrueClass.xpm
40
+ - lib/json/pure.rb
44
41
  - lib/json/version.rb
45
- - lib/json/add/core.rb
42
+ - lib/json/ext.rb
43
+ - lib/json/editor.rb
44
+ - lib/json/NilClass.xpm
46
45
  - lib/json/add/rails.rb
47
- - lib/json/ext/generator.jar
46
+ - lib/json/add/core.rb
48
47
  - lib/json/ext/parser.jar
48
+ - lib/json/ext/generator.jar
49
49
  - lib/json/pure/generator.rb
50
50
  - lib/json/pure/parser.rb
51
- - tests/test_json.rb
52
- - tests/test_json_addition.rb
53
51
  - tests/test_json_encoding.rb
54
- - tests/test_json_fixtures.rb
52
+ - tests/test_json_addition.rb
53
+ - tests/test_json.rb
54
+ - tests/test_json_string_matching.rb
55
55
  - tests/test_json_generate.rb
56
- - tests/test_json_rails.rb
57
56
  - tests/test_json_unicode.rb
57
+ - tests/setup_variant.rb
58
+ - tests/test_json_fixtures.rb
59
+ - tests/fixtures/pass16.json
60
+ - tests/fixtures/fail4.json
58
61
  - tests/fixtures/fail1.json
59
- - tests/fixtures/fail10.json
60
- - tests/fixtures/fail11.json
61
- - tests/fixtures/fail12.json
62
- - tests/fixtures/fail13.json
63
- - tests/fixtures/fail14.json
64
- - tests/fixtures/fail18.json
62
+ - tests/fixtures/fail28.json
63
+ - tests/fixtures/fail8.json
65
64
  - tests/fixtures/fail19.json
66
- - tests/fixtures/fail2.json
65
+ - tests/fixtures/pass2.json
66
+ - tests/fixtures/pass26.json
67
+ - tests/fixtures/pass1.json
68
+ - tests/fixtures/fail3.json
67
69
  - tests/fixtures/fail20.json
68
- - tests/fixtures/fail21.json
70
+ - tests/fixtures/pass3.json
71
+ - tests/fixtures/pass15.json
72
+ - tests/fixtures/fail12.json
73
+ - tests/fixtures/fail13.json
69
74
  - tests/fixtures/fail22.json
70
- - tests/fixtures/fail23.json
71
75
  - tests/fixtures/fail24.json
72
- - tests/fixtures/fail25.json
73
- - tests/fixtures/fail27.json
74
- - tests/fixtures/fail28.json
75
- - tests/fixtures/fail3.json
76
- - tests/fixtures/fail4.json
77
- - tests/fixtures/fail5.json
76
+ - tests/fixtures/fail9.json
77
+ - tests/fixtures/fail2.json
78
+ - tests/fixtures/fail14.json
78
79
  - tests/fixtures/fail6.json
80
+ - tests/fixtures/fail21.json
79
81
  - tests/fixtures/fail7.json
80
- - tests/fixtures/fail8.json
81
- - tests/fixtures/fail9.json
82
- - tests/fixtures/pass1.json
83
- - tests/fixtures/pass15.json
84
- - tests/fixtures/pass16.json
85
82
  - tests/fixtures/pass17.json
86
- - tests/fixtures/pass2.json
87
- - tests/fixtures/pass26.json
88
- - tests/fixtures/pass3.json
83
+ - tests/fixtures/fail11.json
84
+ - tests/fixtures/fail25.json
85
+ - tests/fixtures/fail5.json
86
+ - tests/fixtures/fail18.json
87
+ - tests/fixtures/fail27.json
88
+ - tests/fixtures/fail10.json
89
+ - tests/fixtures/fail23.json
89
90
  has_rdoc: true
90
91
  homepage: http://json-jruby.rubyforge.org/
91
92
  licenses: []
@@ -96,7 +97,6 @@ rdoc_options: []
96
97
  require_paths:
97
98
  - lib
98
99
  required_ruby_version: !ruby/object:Gem::Requirement
99
- none: false
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
@@ -104,7 +104,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
104
  - 0
105
105
  version: "0"
106
106
  required_rubygems_version: !ruby/object:Gem::Requirement
107
- none: false
108
107
  requirements:
109
108
  - - ">="
110
109
  - !ruby/object:Gem::Version
@@ -114,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
113
  requirements: []
115
114
 
116
115
  rubyforge_project: json-jruby
117
- rubygems_version: 1.3.7
116
+ rubygems_version: 1.3.6
118
117
  signing_key:
119
118
  specification_version: 3
120
119
  summary: JSON implementation for JRuby
@@ -1,144 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # -*- coding: utf-8 -*-
3
-
4
- require 'test/unit'
5
- case ENV['JSON']
6
- when 'pure' then require 'json/pure'
7
- when 'ext' then require 'json/ext'
8
- else require 'json'
9
- end
10
- require 'json/add/rails'
11
- require 'date'
12
-
13
- class TC_JSONRails < Test::Unit::TestCase
14
- include JSON
15
-
16
- class A
17
- def initialize(a)
18
- @a = a
19
- end
20
-
21
- attr_reader :a
22
-
23
- def ==(other)
24
- a == other.a
25
- end
26
-
27
- def self.json_create(object)
28
- new(*object['args'])
29
- end
30
-
31
- def to_json(*args)
32
- {
33
- 'json_class' => self.class.name,
34
- 'args' => [ @a ],
35
- }.to_json(*args)
36
- end
37
- end
38
-
39
- class B
40
- def self.json_creatable?
41
- false
42
- end
43
-
44
- def to_json(*args)
45
- {
46
- 'json_class' => self.class.name,
47
- }.to_json(*args)
48
- end
49
- end
50
-
51
- class C
52
- def to_json(*args)
53
- {
54
- 'json_class' => 'TC_JSONRails::Nix',
55
- }.to_json(*args)
56
- end
57
- end
58
-
59
- class D
60
- def initialize
61
- @foo = 666
62
- end
63
-
64
- attr_reader :foo
65
-
66
- def ==(other)
67
- foo == other.foo
68
- end
69
- end
70
-
71
- def test_extended_json
72
- a = A.new(666)
73
- assert A.json_creatable?
74
- assert_equal 666, a.a
75
- json = generate(a)
76
- a_again = JSON.parse(json)
77
- assert_kind_of a.class, a_again
78
- assert_equal a, a_again
79
- assert_equal 666, a_again.a
80
- end
81
-
82
- def test_extended_json_generic_object
83
- d = D.new
84
- assert D.json_creatable?
85
- assert_equal 666, d.foo
86
- json = generate(d)
87
- d_again = JSON.parse(json)
88
- assert_kind_of d.class, d_again
89
- assert_equal d, d_again
90
- assert_equal 666, d_again.foo
91
- end
92
-
93
- def test_extended_json_disabled
94
- a = A.new(666)
95
- assert A.json_creatable?
96
- json = generate(a)
97
- a_again = JSON.parse(json, :create_additions => true)
98
- assert_kind_of a.class, a_again
99
- assert_equal a, a_again
100
- a_hash = JSON.parse(json, :create_additions => false)
101
- assert_kind_of Hash, a_hash
102
- assert_equal(
103
- {"args"=>[666], "json_class"=>"TC_JSONRails::A"}.sort_by { |k,| k },
104
- a_hash.sort_by { |k,| k }
105
- )
106
- end
107
-
108
- def test_extended_json_fail1
109
- b = B.new
110
- assert !B.json_creatable?
111
- json = generate(b)
112
- assert_equal({ 'json_class' => B.name }, JSON.parse(json))
113
- end
114
-
115
- def test_extended_json_fail2
116
- c = C.new # with rails addition all objects are theoretically creatable
117
- assert C.json_creatable?
118
- json = generate(c)
119
- assert_raises(ArgumentError, NameError) { JSON.parse(json) }
120
- end
121
-
122
- def test_raw_strings
123
- raw = ''
124
- raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
125
- raw_array = []
126
- for i in 0..255
127
- raw << i
128
- raw_array << i
129
- end
130
- json = raw.to_json_raw
131
- json_raw_object = raw.to_json_raw_object
132
- hash = { 'json_class' => 'String', 'raw'=> raw_array }
133
- assert_equal hash, json_raw_object
134
- assert_match /\A\{.*\}\Z/, json
135
- assert_match /"json_class":"String"/, json
136
- 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
137
- raw_again = JSON.parse(json)
138
- assert_equal raw, raw_again
139
- end
140
-
141
- def test_symbol
142
- assert_equal '"foo"', :foo.to_json # we don't want an object here
143
- end
144
- end