json-jruby 1.1.7-universal-java-1.6 → 1.2.0-universal-java-1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/json.rb CHANGED
@@ -1,233 +1,10 @@
1
1
  require 'json/common'
2
- # = json - JSON for Ruby
3
- #
4
- # == Description
5
- #
6
- # This is a implementation of the JSON specification according to RFC 4627
7
- # (http://www.ietf.org/rfc/rfc4627.txt). Starting from version 1.0.0 on there
8
- # will be two variants available:
9
- #
10
- # * A pure ruby variant, that relies on the iconv and the stringscan
11
- # extensions, which are both part of the ruby standard library.
12
- # * The quite a bit faster C extension variant, which is in parts implemented
13
- # in C and comes with its own unicode conversion functions and a parser
14
- # generated by the ragel state machine compiler
15
- # (http://www.cs.queensu.ca/~thurston/ragel).
16
- #
17
- # Both variants of the JSON generator escape all non-ASCII an control
18
- # characters with \uXXXX escape sequences, and support UTF-16 surrogate pairs
19
- # in order to be able to generate the whole range of unicode code points. This
20
- # means that generated JSON text is encoded as UTF-8 (because ASCII is a subset
21
- # of UTF-8) and at the same time avoids decoding problems for receiving
22
- # endpoints, that don't expect UTF-8 encoded texts. On the negative side this
23
- # may lead to a bit longer strings than necessarry.
24
- #
25
- # All strings, that are to be encoded as JSON strings, should be UTF-8 byte
26
- # sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
27
- # encoded, please use the to_json_raw_object method of String (which produces
28
- # an object, that contains a byte array) and decode the result on the receiving
29
- # endpoint.
30
- #
31
- # == Author
32
- #
33
- # Florian Frank <mailto:flori@ping.de>
34
- #
35
- # == License
36
- #
37
- # This software is distributed under the same license as Ruby itself, see
38
- # http://www.ruby-lang.org/en/LICENSE.txt.
39
- #
40
- # == Download
41
- #
42
- # The latest version of this library can be downloaded at
43
- #
44
- # * http://rubyforge.org/frs?group_id=953
45
- #
46
- # Online Documentation should be located at
47
- #
48
- # * http://json.rubyforge.org
49
- #
50
- # == Usage
51
- #
52
- # To use JSON you can
53
- # require 'json'
54
- # to load the installed variant (either the extension 'json' or the pure
55
- # variant 'json_pure'). If you have installed the extension variant, you can
56
- # pick either the extension variant or the pure variant by typing
57
- # require 'json/ext'
58
- # or
59
- # require 'json/pure'
60
- #
61
- # You can choose to load a set of common additions to ruby core's objects if
62
- # you
63
- # require 'json/add/core'
64
- #
65
- # After requiring this you can, e. g., serialise/deserialise Ruby ranges:
66
- #
67
- # JSON JSON(1..10) # => 1..10
68
- #
69
- # To find out how to add JSON support to other or your own classes, read the
70
- # Examples section below.
71
- #
72
- # To get the best compatibility to rails' JSON implementation, you can
73
- # require 'json/add/rails'
74
- #
75
- # Both of the additions attempt to require 'json' (like above) first, if it has
76
- # not been required yet.
77
- #
78
- # == Speed Comparisons
79
- #
80
- # I have created some benchmark results (see the benchmarks subdir of the
81
- # package) for the JSON-Parser to estimate the speed up in the C extension:
82
- #
83
- # JSON::Pure::Parser:: 28.90 calls/second
84
- # JSON::Ext::Parser:: 505.50 calls/second
85
- #
86
- # This is ca. <b>17.5</b> times the speed of the pure Ruby implementation.
87
- #
88
- # I have benchmarked the JSON-Generator as well. This generates a few more
89
- # values, because there are different modes, that also influence the achieved
90
- # speed:
91
- #
92
- # * JSON::Pure::Generator:
93
- # generate:: 35.06 calls/second
94
- # pretty_generate:: 34.00 calls/second
95
- # fast_generate:: 41.06 calls/second
96
- #
97
- # * JSON::Ext::Generator:
98
- # generate:: 492.11 calls/second
99
- # pretty_generate:: 348.85 calls/second
100
- # fast_generate:: 541.60 calls/second
101
- #
102
- # * Speedup Ext/Pure:
103
- # generate safe:: 14.0 times
104
- # generate pretty:: 10.3 times
105
- # generate fast:: 13.2 times
106
- #
107
- # The rails framework includes a generator as well, also it seems to be rather
108
- # slow: I measured only 23.87 calls/second which is slower than any of my pure
109
- # generator results. Here a comparison of the different speedups with the Rails
110
- # measurement as the divisor:
111
- #
112
- # * Speedup Pure/Rails:
113
- # generate safe:: 1.5 times
114
- # generate pretty:: 1.4 times
115
- # generate fast:: 1.7 times
116
- #
117
- # * Speedup Ext/Rails:
118
- # generate safe:: 20.6 times
119
- # generate pretty:: 14.6 times
120
- # generate fast:: 22.7 times
121
- #
122
- # To achieve the fastest JSON text output, you can use the
123
- # fast_generate/fast_unparse methods. Beware, that this will disable the
124
- # checking for circular Ruby data structures, which may cause JSON to go into
125
- # an infinite loop.
126
- #
127
- # == Examples
128
- #
129
- # To create a JSON text from a ruby data structure, you
130
- # can call JSON.generate (or JSON.unparse) like that:
131
- #
132
- # json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
133
- # # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
134
- #
135
- # To create a valid JSON text you have to make sure, that the output is
136
- # embedded in either a JSON array [] or a JSON object {}. The easiest way to do
137
- # this, is by putting your values in a Ruby Array or Hash instance.
138
- #
139
- # To get back a ruby data structure from a JSON text, you have to call
140
- # JSON.parse on it:
141
- #
142
- # JSON.parse json
143
- # # => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
144
- #
145
- # Note, that the range from the original data structure is a simple
146
- # string now. The reason for this is, that JSON doesn't support ranges
147
- # or arbitrary classes. In this case the json library falls back to call
148
- # Object#to_json, which is the same as #to_s.to_json.
149
- #
150
- # It's possible to add JSON support serialization to arbitrary classes by
151
- # simply implementing a more specialized version of the #to_json method, that
152
- # should return a JSON object (a hash converted to JSON with #to_json) like
153
- # this (don't forget the *a for all the arguments):
154
- #
155
- # class Range
156
- # def to_json(*a)
157
- # {
158
- # 'json_class' => self.class.name, # = 'Range'
159
- # 'data' => [ first, last, exclude_end? ]
160
- # }.to_json(*a)
161
- # end
162
- # end
163
- #
164
- # The hash key 'json_class' is the class, that will be asked to deserialise the
165
- # JSON representation later. In this case it's 'Range', but any namespace of
166
- # the form 'A::B' or '::A::B' will do. All other keys are arbitrary and can be
167
- # used to store the necessary data to configure the object to be deserialised.
168
- #
169
- # If a the key 'json_class' is found in a JSON object, the JSON parser checks
170
- # if the given class responds to the json_create class method. If so, it is
171
- # called with the JSON object converted to a Ruby hash. So a range can
172
- # be deserialised by implementing Range.json_create like this:
173
- #
174
- # class Range
175
- # def self.json_create(o)
176
- # new(*o['data'])
177
- # end
178
- # end
179
- #
180
- # Now it possible to serialise/deserialise ranges as well:
181
- #
182
- # json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
183
- # # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
184
- # JSON.parse json
185
- # # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
186
- #
187
- # JSON.generate always creates the shortest possible string representation of a
188
- # ruby data structure in one line. This good for data storage or network
189
- # protocols, but not so good for humans to read. Fortunately there's also
190
- # JSON.pretty_generate (or JSON.pretty_generate) that creates a more
191
- # readable output:
192
- #
193
- # puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
194
- # [
195
- # 1,
196
- # 2,
197
- # {
198
- # "a": 3.141
199
- # },
200
- # false,
201
- # true,
202
- # null,
203
- # {
204
- # "json_class": "Range",
205
- # "data": [
206
- # 4,
207
- # 10,
208
- # false
209
- # ]
210
- # }
211
- # ]
212
- #
213
- # There are also the methods Kernel#j for unparse, and Kernel#jj for
214
- # pretty_unparse output to the console, that work analogous to Core Ruby's p
215
- # and the pp library's pp methods.
216
- #
217
- # The script tools/server.rb contains a small example if you want to test, how
218
- # receiving a JSON object from a webrick server in your browser with the
219
- # javasript prototype library (http://www.prototypejs.org) works.
220
- #
221
2
  module JSON
222
3
  require 'json/version'
223
4
 
224
- if VARIANT_BINARY
5
+ begin
225
6
  require 'json/ext'
226
- else
227
- begin
228
- require 'json/ext'
229
- rescue LoadError
230
- require 'json/pure'
231
- end
7
+ rescue LoadError
8
+ require 'json/pure'
232
9
  end
233
10
  end
data/lib/json/common.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'json/version'
2
+ require 'iconv'
2
3
 
3
4
  module JSON
4
5
  class << self
@@ -74,7 +75,7 @@ module JSON
74
75
  end
75
76
  self.create_id = 'json_class'
76
77
 
77
- NaN = (-1.0) ** 0.5
78
+ NaN = 0.0/0
78
79
 
79
80
  Infinity = 1.0/0
80
81
 
@@ -105,7 +106,7 @@ module JSON
105
106
 
106
107
  module_function
107
108
 
108
- # Parse the JSON string _source_ into a Ruby data structure and return it.
109
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
109
110
  #
110
111
  # _opts_ can have the following
111
112
  # keys:
@@ -122,9 +123,9 @@ module JSON
122
123
  JSON.parser.new(source, opts).parse
123
124
  end
124
125
 
125
- # Parse the JSON string _source_ into a Ruby data structure and return it.
126
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
126
127
  # The bang version of the parse method, defaults to the more dangerous values
127
- # for the _opts_ hash, so be sure only to parse trusted _source_ strings.
128
+ # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
128
129
  #
129
130
  # _opts_ can have the following keys:
130
131
  # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
@@ -145,9 +146,8 @@ module JSON
145
146
  JSON.parser.new(source, opts).parse
146
147
  end
147
148
 
148
- # Unparse the Ruby data structure _obj_ into a single line JSON string and
149
- # return it. _state_ is
150
- # * a JSON::State object,
149
+ # Generate a JSON document from the Ruby data structure _obj_ and return
150
+ # it. _state_ is * a JSON::State object,
151
151
  # * or a Hash like object (responding to to_hash),
152
152
  # * an object convertible into a hash by a to_h method,
153
153
  # that is used as or to configure a State object.
@@ -180,7 +180,11 @@ module JSON
180
180
  else
181
181
  state = State.new
182
182
  end
183
- obj.to_json(state)
183
+ result = obj.to_json(state)
184
+ if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
185
+ raise GeneratorError, "only generation of JSON objects or arrays allowed"
186
+ end
187
+ result
184
188
  end
185
189
 
186
190
  # :stopdoc:
@@ -190,14 +194,17 @@ module JSON
190
194
  module_function :unparse
191
195
  # :startdoc:
192
196
 
193
- # Unparse the Ruby data structure _obj_ into a single line JSON string and
194
- # return it. This method disables the checks for circles in Ruby objects, and
195
- # also generates NaN, Infinity, and, -Infinity float values.
197
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
198
+ # This method disables the checks for circles in Ruby objects.
196
199
  #
197
200
  # *WARNING*: Be careful not to pass any Ruby data structures with circles as
198
201
  # _obj_ argument, because this will cause JSON to go into an infinite loop.
199
202
  def fast_generate(obj)
200
- obj.to_json(nil)
203
+ result = obj.to_json(nil)
204
+ if result !~ /\A(?:\[.*\]|\{.*\})\Z/
205
+ raise GeneratorError, "only generation of JSON objects or arrays allowed"
206
+ end
207
+ result
201
208
  end
202
209
 
203
210
  # :stopdoc:
@@ -206,8 +213,9 @@ module JSON
206
213
  module_function :fast_unparse
207
214
  # :startdoc:
208
215
 
209
- # Unparse the Ruby data structure _obj_ into a JSON string and return it. The
210
- # returned string is a prettier form of the string returned by #unparse.
216
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
217
+ # The returned document is a prettier form of the document returned by
218
+ # #unparse.
211
219
  #
212
220
  # The _opts_ argument can be used to configure the generator, see the
213
221
  # generate method for a more detailed explanation.
@@ -229,7 +237,11 @@ module JSON
229
237
  end
230
238
  state.configure(opts)
231
239
  end
232
- obj.to_json(state)
240
+ result = obj.to_json(state)
241
+ if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
242
+ raise GeneratorError, "only generation of JSON objects or arrays allowed"
243
+ end
244
+ result
233
245
  end
234
246
 
235
247
  # :stopdoc:
@@ -270,8 +282,6 @@ module JSON
270
282
  proc.call result
271
283
  end
272
284
  end
273
- private :recurse_proc
274
- module_function :recurse_proc
275
285
 
276
286
  alias restore load
277
287
  module_function :restore
@@ -307,9 +317,16 @@ module JSON
307
317
  rescue JSON::NestingError
308
318
  raise ArgumentError, "exceed depth limit"
309
319
  end
320
+
321
+ # Shortuct for iconv.
322
+ def self.iconv(to, from, string)
323
+ Iconv.iconv(to, from, string).first
324
+ end
310
325
  end
311
326
 
312
327
  module ::Kernel
328
+ private
329
+
313
330
  # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
314
331
  # one line.
315
332
  def j(*objs)
Binary file
Binary file
data/lib/json/pure.rb CHANGED
@@ -10,6 +10,9 @@ module JSON
10
10
  # An iconv instance to convert from UTF16 Big Endian to UTF8.
11
11
  UTF8toUTF16 = Iconv.new('utf-16be', 'utf-8') # :nodoc:
12
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"
13
16
  rescue Errno::EINVAL, Iconv::InvalidEncoding
14
17
  # Iconv doesn't support big endian utf-16. Let's try to hack this manually
15
18
  # into the converters.
@@ -51,9 +54,6 @@ module JSON
51
54
  ensure
52
55
  $VERBOSE = old_verbose
53
56
  end
54
- rescue LoadError
55
- raise MissingUnicodeSupport,
56
- "iconv couldn't be loaded, which is required for UTF-8/UTF-16 conversions"
57
57
  end
58
58
 
59
59
  # Swap consecutive bytes of _string_ in place.
@@ -34,17 +34,16 @@ module JSON
34
34
  "\x1f" => '\u001f',
35
35
  '"' => '\"',
36
36
  '\\' => '\\\\',
37
- '/' => '\/',
38
37
  } # :nodoc:
39
38
 
40
39
  # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
41
40
  # UTF16 big endian characters as \u????, and return it.
42
- if String.method_defined?(:force_encoding)
41
+ if defined?(::Encoding)
43
42
  def utf8_to_json(string) # :nodoc:
44
43
  string = string.dup
45
44
  string << '' # XXX workaround: avoid buffer sharing
46
- string.force_encoding(Encoding::ASCII_8BIT)
47
- string.gsub!(/["\\\/\x0-\x1f]/) { MAP[$&] }
45
+ string.force_encoding(::Encoding::ASCII_8BIT)
46
+ string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
48
47
  string.gsub!(/(
49
48
  (?:
50
49
  [\xc2-\xdf][\x80-\xbf] |
@@ -57,14 +56,14 @@ module JSON
57
56
  s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
58
57
  s.gsub!(/.{4}/n, '\\\\u\&')
59
58
  }
60
- string.force_encoding(Encoding::UTF_8)
59
+ string.force_encoding(::Encoding::UTF_8)
61
60
  string
62
61
  rescue Iconv::Failure => e
63
62
  raise GeneratorError, "Caught #{e.class}: #{e}"
64
63
  end
65
64
  else
66
65
  def utf8_to_json(string) # :nodoc:
67
- string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
66
+ string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
68
67
  string.gsub!(/(
69
68
  (?:
70
69
  [\xc2-\xdf][\x80-\xbf] |
@@ -352,13 +351,13 @@ module JSON
352
351
  def to_json(state = nil, *)
353
352
  case
354
353
  when infinite?
355
- if !state || state.allow_nan?
354
+ if state && state.allow_nan?
356
355
  to_s
357
356
  else
358
357
  raise GeneratorError, "#{self} not allowed in JSON"
359
358
  end
360
359
  when nan?
361
- if !state || state.allow_nan?
360
+ if state && state.allow_nan?
362
361
  to_s
363
362
  else
364
363
  raise GeneratorError, "#{self} not allowed in JSON"
@@ -370,11 +369,25 @@ module JSON
370
369
  end
371
370
 
372
371
  module String
373
- # This string should be encoded with UTF-8 A call to this method
374
- # returns a JSON string encoded with UTF16 big endian characters as
375
- # \u????.
376
- def to_json(*)
377
- '"' << JSON.utf8_to_json(self) << '"'
372
+ if defined?(::Encoding)
373
+ # This string should be encoded with UTF-8 A call to this method
374
+ # returns a JSON string encoded with UTF16 big endian characters as
375
+ # \u????.
376
+ def to_json(*)
377
+ if encoding == ::Encoding::UTF_8
378
+ '"' << JSON.utf8_to_json(self) << '"'
379
+ else
380
+ string = encode(::Encoding::UTF_8)
381
+ '"' << JSON.utf8_to_json(string) << '"'
382
+ end
383
+ end
384
+ else
385
+ # This string should be encoded with UTF-8 A call to this method
386
+ # returns a JSON string encoded with UTF16 big endian characters as
387
+ # \u????.
388
+ def to_json(*)
389
+ '"' << JSON.utf8_to_json(self) << '"'
390
+ end
378
391
  end
379
392
 
380
393
  # Module that holds the extinding methods if, the String module is
@@ -66,7 +66,41 @@ module JSON
66
66
  # * *object_class*: Defaults to Hash
67
67
  # * *array_class*: Defaults to Array
68
68
  def initialize(source, opts = {})
69
- super
69
+ if defined?(::Encoding)
70
+ if source.encoding == Encoding::ASCII_8BIT
71
+ b = source[0, 4].bytes.to_a
72
+ source = case
73
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
74
+ source.dup.force_encoding(Encoding::UTF_32BE).encode!(Encoding::UTF_8)
75
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
76
+ source.dup.force_encoding(Encoding::UTF_16BE).encode!(Encoding::UTF_8)
77
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
78
+ source.dup.force_encoding(Encoding::UTF_32LE).encode!(Encoding::UTF_8)
79
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
80
+ source.dup.force_encoding(Encoding::UTF_16LE).encode!(Encoding::UTF_8)
81
+ else
82
+ source.dup
83
+ end
84
+ else
85
+ source = source.encode(Encoding::UTF_8)
86
+ end
87
+ source.force_encoding(Encoding::ASCII_8BIT)
88
+ else
89
+ b = source
90
+ source = case
91
+ when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
92
+ JSON.iconv('utf-8', 'utf-32be', b)
93
+ when b.size >= 4 && b[0] == 0 && b[2] == 0
94
+ JSON.iconv('utf-8', 'utf-16be', b)
95
+ when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
96
+ JSON.iconv('utf-8', 'utf-32le', b)
97
+ when b.size >= 4 && b[1] == 0 && b[3] == 0
98
+ JSON.iconv('utf-8', 'utf-16le', b)
99
+ else
100
+ b
101
+ end
102
+ end
103
+ super source
70
104
  if !opts.key?(:max_nesting) # defaults to 19
71
105
  @max_nesting = 19
72
106
  elsif opts[:max_nesting]
@@ -188,7 +222,7 @@ module JSON
188
222
  end
189
223
 
190
224
  def parse_array
191
- raise NestingError, "nesting of #@current_nesting is to deep" if
225
+ raise NestingError, "nesting of #@current_nesting is too deep" if
192
226
  @max_nesting.nonzero? && @current_nesting > @max_nesting
193
227
  result = @array_class.new
194
228
  delim = false
@@ -220,7 +254,7 @@ module JSON
220
254
  end
221
255
 
222
256
  def parse_object
223
- raise NestingError, "nesting of #@current_nesting is to deep" if
257
+ raise NestingError, "nesting of #@current_nesting is too deep" if
224
258
  @max_nesting.nonzero? && @current_nesting > @max_nesting
225
259
  result = @object_class.new
226
260
  delim = false
data/lib/json/version.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.1.7'
3
+ VERSION = '1.2.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:
7
7
  VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
8
- VARIANT_BINARY = false
9
8
  end
@@ -226,11 +226,6 @@ EOT
226
226
  assert_equal json, JSON.unparse(data)
227
227
  assert_equal data, JSON.parse(json)
228
228
  #
229
- json = '["\/"]'
230
- data = JSON.parse(json)
231
- assert_equal ['/'], data
232
- assert_equal json, JSON.unparse(data)
233
- #
234
229
  json = '["\""]'
235
230
  data = JSON.parse(json)
236
231
  assert_equal ['"'], data
data/tests/test_json.rb CHANGED
@@ -9,6 +9,20 @@ else require 'json'
9
9
  end
10
10
  require 'stringio'
11
11
 
12
+ unless Array.method_defined?(:permutation)
13
+ begin
14
+ require 'enumerator'
15
+ require 'permutation'
16
+ class Array
17
+ def permutation
18
+ Permutation.for(self).to_enum.map { |x| x.project }
19
+ end
20
+ end
21
+ rescue LoadError
22
+ warn "Skipping permutation tests."
23
+ end
24
+ end
25
+
12
26
  class TC_JSON < Test::Unit::TestCase
13
27
  include JSON
14
28
 
@@ -94,30 +108,24 @@ class TC_JSON < Test::Unit::TestCase
94
108
  assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
95
109
  end
96
110
 
97
- begin
98
- require 'permutation'
111
+ if Array.method_defined?(:permutation)
99
112
  def test_parse_more_complex_arrays
100
113
  a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
101
- perms = Permutation.for a
102
- perms.each do |perm|
103
- orig_ary = perm.project
104
- json = pretty_generate(orig_ary)
105
- assert_equal orig_ary, parse(json)
114
+ a.permutation.each do |perm|
115
+ json = pretty_generate(perm)
116
+ assert_equal perm, parse(json)
106
117
  end
107
118
  end
108
119
 
109
120
  def test_parse_complex_objects
110
121
  a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
111
- perms = Permutation.for a
112
- perms.each do |perm|
122
+ a.permutation.each do |perm|
113
123
  s = "a"
114
- orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
124
+ orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
115
125
  json = pretty_generate(orig_obj)
116
126
  assert_equal orig_obj, parse(json)
117
127
  end
118
128
  end
119
- rescue LoadError
120
- warn "Skipping permutation tests."
121
129
  end
122
130
 
123
131
  def test_parse_arrays
@@ -222,27 +230,27 @@ EOT
222
230
  def test_backslash
223
231
  data = [ '\\.(?i:gif|jpe?g|png)$' ]
224
232
  json = '["\\\\.(?i:gif|jpe?g|png)$"]'
225
- assert_equal json, JSON.unparse(data)
233
+ assert_equal json, JSON.generate(data)
226
234
  assert_equal data, JSON.parse(json)
227
235
  #
228
236
  data = [ '\\"' ]
229
237
  json = '["\\\\\""]'
230
- assert_equal json, JSON.unparse(data)
238
+ assert_equal json, JSON.generate(data)
231
239
  assert_equal data, JSON.parse(json)
232
240
  #
233
- json = '["\/"]'
241
+ json = '["/"]'
234
242
  data = JSON.parse(json)
235
243
  assert_equal ['/'], data
236
- assert_equal json, JSON.unparse(data)
244
+ assert_equal json, JSON.generate(data)
237
245
  #
238
246
  json = '["\""]'
239
247
  data = JSON.parse(json)
240
248
  assert_equal ['"'], data
241
- assert_equal json, JSON.unparse(data)
249
+ assert_equal json, JSON.generate(data)
242
250
  json = '["\\\'"]'
243
251
  data = JSON.parse(json)
244
252
  assert_equal ["'"], data
245
- assert_equal '["\'"]', JSON.unparse(data)
253
+ assert_equal '["\'"]', JSON.generate(data)
246
254
  end
247
255
 
248
256
  def test_wrong_inputs
@@ -0,0 +1,67 @@
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 'iconv'
11
+
12
+ class TC_JSONEncoding < Test::Unit::TestCase
13
+ include JSON
14
+
15
+ def setup
16
+ @utf_8 = '["© ≠ €!"]'
17
+ @parsed = [ "© ≠ €!" ]
18
+ @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
19
+ @generated = '["\u00a9 \u2260 \u20ac!"]'
20
+ if defined?(::Encoding)
21
+ @utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
22
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
23
+ @utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
24
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
25
+ @utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
26
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
27
+ @utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
28
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
29
+ @utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
30
+ else
31
+ @utf_8_ascii_8bit = @utf_8.dup
32
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
33
+ @utf_16be_ascii_8bit = @utf_16be.dup
34
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
35
+ @utf_16le_ascii_8bit = @utf_16le.dup
36
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
37
+ @utf_32be_ascii_8bit = @utf_32be.dup
38
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
39
+ @utf_32le_ascii_8bit = @utf_32le.dup
40
+ end
41
+ end
42
+
43
+ def test_parse
44
+ assert_equal @parsed, JSON.parse(@utf_8)
45
+ assert_equal @parsed, JSON.parse(@utf_16be)
46
+ assert_equal @parsed, JSON.parse(@utf_16le)
47
+ assert_equal @parsed, JSON.parse(@utf_32be)
48
+ assert_equal @parsed, JSON.parse(@utf_32le)
49
+ end
50
+
51
+ def test_parse_ascii_8bit
52
+ assert_equal @parsed, JSON.parse(@utf_8_ascii_8bit)
53
+ assert_equal @parsed, JSON.parse(@utf_16be_ascii_8bit)
54
+ assert_equal @parsed, JSON.parse(@utf_16le_ascii_8bit)
55
+ assert_equal @parsed, JSON.parse(@utf_32be_ascii_8bit)
56
+ assert_equal @parsed, JSON.parse(@utf_32le_ascii_8bit)
57
+ end
58
+
59
+ def test_generate
60
+ assert_equal @generated, JSON.generate(@parsed)
61
+ if defined?(::Encoding)
62
+ assert_equal @generated, JSON.generate(@utf_16_data)
63
+ else
64
+ assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data) }
65
+ end
66
+ end
67
+ end
@@ -44,8 +44,8 @@ class TC_JSONGenerate < Test::Unit::TestCase
44
44
  EOT
45
45
  end
46
46
 
47
- def test_unparse
48
- json = unparse(@hash)
47
+ def test_generate
48
+ json = generate(@hash)
49
49
  assert_equal(JSON.parse(@json2), JSON.parse(json))
50
50
  parsed_json = parse(json)
51
51
  assert_equal(@hash, parsed_json)
@@ -53,10 +53,11 @@ EOT
53
53
  assert_equal('{"1":2}', json)
54
54
  parsed_json = parse(json)
55
55
  assert_equal({"1"=>2}, parsed_json)
56
+ assert_raise(GeneratorError) { generate(666) }
56
57
  end
57
58
 
58
- def test_unparse_pretty
59
- json = pretty_unparse(@hash)
59
+ def test_generate_pretty
60
+ json = pretty_generate(@hash)
60
61
  assert_equal(JSON.parse(@json3), JSON.parse(json))
61
62
  parsed_json = parse(json)
62
63
  assert_equal(@hash, parsed_json)
@@ -68,6 +69,19 @@ EOT
68
69
  EOT
69
70
  parsed_json = parse(json)
70
71
  assert_equal({"1"=>2}, parsed_json)
72
+ assert_raise(GeneratorError) { pretty_generate(666) }
73
+ end
74
+
75
+ def test_fast_generate
76
+ json = fast_generate(@hash)
77
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
78
+ parsed_json = parse(json)
79
+ assert_equal(@hash, parsed_json)
80
+ json = fast_generate({1=>2})
81
+ assert_equal('{"1":2}', json)
82
+ parsed_json = parse(json)
83
+ assert_equal({"1"=>2}, parsed_json)
84
+ assert_raise(GeneratorError) { fast_generate(666) }
71
85
  end
72
86
 
73
87
  def test_states
@@ -89,17 +103,17 @@ EOT
89
103
  def test_allow_nan
90
104
  assert_raises(GeneratorError) { generate([JSON::NaN]) }
91
105
  assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
92
- assert_equal '[NaN]', fast_generate([JSON::NaN])
106
+ assert_raises(GeneratorError) { fast_generate([JSON::NaN]) }
93
107
  assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
94
108
  assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
95
109
  assert_raises(GeneratorError) { generate([JSON::Infinity]) }
96
110
  assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
97
- assert_equal '[Infinity]', fast_generate([JSON::Infinity])
111
+ assert_raises(GeneratorError) { fast_generate([JSON::Infinity]) }
98
112
  assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
99
113
  assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
100
114
  assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
101
115
  assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
102
- assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity])
116
+ assert_raises(GeneratorError) { fast_generate([JSON::MinusInfinity]) }
103
117
  assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
104
118
  assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
105
119
  end
@@ -141,6 +141,6 @@ EOT
141
141
  end
142
142
 
143
143
  def test_symbol
144
- assert_equal '"foo"', JSON(:foo) # we don't want an object here
144
+ assert_equal '"foo"', :foo.to_json # we don't want an object here
145
145
  end
146
146
  end
metadata CHANGED
@@ -1,104 +1,104 @@
1
1
  --- !ruby/object:Gem::Specification
2
- required_ruby_version: !ruby/object:Gem::Requirement
3
- requirements:
4
- - - '>='
5
- - !ruby/object:Gem::Version
6
- version: "0"
7
- version:
8
- email: mernen+rubyforge@gmail.com
2
+ name: json-jruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: universal-java-1.6
6
+ authors:
7
+ - Daniel Luz
8
+ autorequire:
9
+ bindir: bin
9
10
  cert_chain: []
10
11
 
11
- summary: A JSON implementation as a JRuby extension
12
- post_install_message:
12
+ date: 2009-11-28 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mernen+rubyforge@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
13
22
  extra_rdoc_files: []
14
23
 
24
+ files:
25
+ - lib/json.rb
26
+ - lib/json/version.rb
27
+ - lib/json/ext.rb
28
+ - lib/json/pure.rb
29
+ - lib/json/common.rb
30
+ - lib/json/add/rails.rb
31
+ - lib/json/add/core.rb
32
+ - lib/json/ext/generator.jar
33
+ - lib/json/ext/parser.jar
34
+ - lib/json/pure/generator.rb
35
+ - lib/json/pure/parser.rb
36
+ - tests/test_json_generate.rb
37
+ - tests/test_json_unicode.rb
38
+ - tests/test_jjrb_offsets.rb
39
+ - tests/test_json_addition.rb
40
+ - tests/test_json_fixtures.rb
41
+ - tests/test_json_encoding.rb
42
+ - tests/test_json_rails.rb
43
+ - tests/test_json.rb
44
+ - tests/fixtures/fail20.json
45
+ - tests/fixtures/fail6.json
46
+ - tests/fixtures/fail1.json
47
+ - tests/fixtures/fail14.json
48
+ - tests/fixtures/fail9.json
49
+ - tests/fixtures/pass26.json
50
+ - tests/fixtures/fail27.json
51
+ - tests/fixtures/fail19.json
52
+ - tests/fixtures/fail5.json
53
+ - tests/fixtures/fail10.json
54
+ - tests/fixtures/fail12.json
55
+ - tests/fixtures/fail3.json
56
+ - tests/fixtures/fail13.json
57
+ - tests/fixtures/fail24.json
58
+ - tests/fixtures/pass3.json
59
+ - tests/fixtures/fail23.json
60
+ - tests/fixtures/fail7.json
61
+ - tests/fixtures/pass1.json
62
+ - tests/fixtures/fail18.json
63
+ - tests/fixtures/pass17.json
64
+ - tests/fixtures/fail11.json
65
+ - tests/fixtures/fail21.json
66
+ - tests/fixtures/fail25.json
67
+ - tests/fixtures/fail4.json
68
+ - tests/fixtures/fail8.json
69
+ - tests/fixtures/fail2.json
70
+ - tests/fixtures/pass15.json
71
+ - tests/fixtures/fail22.json
72
+ - tests/fixtures/fail28.json
73
+ - tests/fixtures/pass2.json
74
+ - tests/fixtures/pass16.json
75
+ has_rdoc: true
15
76
  homepage: http://rubyforge.org/projects/json-jruby/
16
- signing_key:
17
- name: json-jruby
18
- rdoc_options: []
19
-
20
- rubyforge_project: json-jruby
21
- autorequire:
22
77
  licenses: []
23
78
 
24
- executables: []
79
+ post_install_message:
80
+ rdoc_options: []
25
81
 
26
- description:
27
- specification_version: 3
28
- default_executable:
29
- files:
30
- - lib/json.rb
31
- - lib/json/version.rb
32
- - lib/json/ext.rb
33
- - lib/json/pure.rb
34
- - lib/json/common.rb
35
- - lib/json/add/rails.rb
36
- - lib/json/add/core.rb
37
- - lib/json/ext/generator.jar
38
- - lib/json/ext/parser.jar
39
- - lib/json/pure/generator.rb
40
- - lib/json/pure/parser.rb
41
- - tests/test_json_generate.rb
42
- - tests/test_json_unicode.rb
43
- - tests/test_jjrb_offsets.rb
44
- - tests/test_json_addition.rb
45
- - tests/test_json_fixtures.rb
46
- - tests/test_json_rails.rb
47
- - tests/test_json.rb
48
- - tests/runner.rb
49
- - tests/fixtures/fail20.json
50
- - tests/fixtures/fail6.json
51
- - tests/fixtures/fail1.json
52
- - tests/fixtures/fail14.json
53
- - tests/fixtures/fail9.json
54
- - tests/fixtures/pass26.json
55
- - tests/fixtures/fail27.json
56
- - tests/fixtures/fail19.json
57
- - tests/fixtures/fail5.json
58
- - tests/fixtures/fail10.json
59
- - tests/fixtures/fail12.json
60
- - tests/fixtures/fail3.json
61
- - tests/fixtures/fail13.json
62
- - tests/fixtures/fail24.json
63
- - tests/fixtures/pass3.json
64
- - tests/fixtures/fail23.json
65
- - tests/fixtures/fail7.json
66
- - tests/fixtures/pass1.json
67
- - tests/fixtures/fail18.json
68
- - tests/fixtures/pass17.json
69
- - tests/fixtures/fail11.json
70
- - tests/fixtures/fail21.json
71
- - tests/fixtures/fail25.json
72
- - tests/fixtures/fail4.json
73
- - tests/fixtures/fail8.json
74
- - tests/fixtures/fail2.json
75
- - tests/fixtures/pass15.json
76
- - tests/fixtures/fail22.json
77
- - tests/fixtures/fail28.json
78
- - tests/fixtures/pass2.json
79
- - tests/fixtures/pass16.json
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
80
90
  required_rubygems_version: !ruby/object:Gem::Requirement
81
91
  requirements:
82
- - - '>='
83
- - !ruby/object:Gem::Version
84
- version: "0"
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
85
95
  version:
86
- extensions: []
87
-
88
- rubygems_version: 1.3.3
89
96
  requirements: []
90
97
 
91
- authors:
92
- - Daniel Luz
93
- date: 2009-07-03 03:00:00 +00:00
94
- platform: universal-java-1.6
95
- test_files:
96
- - tests/runner.rb
97
- version: !ruby/object:Gem::Version
98
- version: 1.1.7
99
- require_paths:
100
- - lib
101
- dependencies: []
98
+ rubyforge_project: json-jruby
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A JSON implementation as a JRuby extension
103
+ test_files: []
102
104
 
103
- bindir: bin
104
- has_rdoc: true
data/tests/runner.rb DELETED
@@ -1,27 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/unit/ui/console/testrunner'
4
- require 'test/unit/testsuite'
5
- $:.unshift File.expand_path(File.dirname($0))
6
- $:.unshift 'tests'
7
- require 'test_json'
8
- require 'test_json_generate'
9
- require 'test_json_unicode'
10
- require 'test_json_addition'
11
- require 'test_json_rails'
12
- require 'test_json_fixtures'
13
- require 'test_jjrb_offsets'
14
-
15
- class TS_AllTests
16
- def self.suite
17
- suite = Test::Unit::TestSuite.new name
18
- suite << TC_JSONGenerate.suite
19
- suite << TC_JSON.suite
20
- suite << TC_JSONUnicode.suite
21
- suite << TC_JSONAddition.suite
22
- suite << TC_JSONRails.suite
23
- suite << TC_JSONFixtures.suite
24
- suite << TestJjrbOffsets.suite
25
- end
26
- end
27
- Test::Unit::UI::Console::TestRunner.run(TS_AllTests)