json 1.4.3-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.

Files changed (60) hide show
  1. data/lib/json.rb +10 -0
  2. data/lib/json/Array.xpm +21 -0
  3. data/lib/json/FalseClass.xpm +21 -0
  4. data/lib/json/Hash.xpm +21 -0
  5. data/lib/json/Key.xpm +73 -0
  6. data/lib/json/NilClass.xpm +21 -0
  7. data/lib/json/Numeric.xpm +28 -0
  8. data/lib/json/String.xpm +96 -0
  9. data/lib/json/TrueClass.xpm +21 -0
  10. data/lib/json/add/core.rb +148 -0
  11. data/lib/json/add/rails.rb +58 -0
  12. data/lib/json/common.rb +397 -0
  13. data/lib/json/editor.rb +1371 -0
  14. data/lib/json/ext.rb +15 -0
  15. data/lib/json/ext/generator.jar +0 -0
  16. data/lib/json/ext/parser.jar +0 -0
  17. data/lib/json/json.xpm +1499 -0
  18. data/lib/json/pure.rb +77 -0
  19. data/lib/json/pure/generator.rb +452 -0
  20. data/lib/json/pure/parser.rb +307 -0
  21. data/lib/json/version.rb +8 -0
  22. data/tests/fixtures/fail1.json +1 -0
  23. data/tests/fixtures/fail10.json +1 -0
  24. data/tests/fixtures/fail11.json +1 -0
  25. data/tests/fixtures/fail12.json +1 -0
  26. data/tests/fixtures/fail13.json +1 -0
  27. data/tests/fixtures/fail14.json +1 -0
  28. data/tests/fixtures/fail18.json +1 -0
  29. data/tests/fixtures/fail19.json +1 -0
  30. data/tests/fixtures/fail2.json +1 -0
  31. data/tests/fixtures/fail20.json +1 -0
  32. data/tests/fixtures/fail21.json +1 -0
  33. data/tests/fixtures/fail22.json +1 -0
  34. data/tests/fixtures/fail23.json +1 -0
  35. data/tests/fixtures/fail24.json +1 -0
  36. data/tests/fixtures/fail25.json +1 -0
  37. data/tests/fixtures/fail27.json +2 -0
  38. data/tests/fixtures/fail28.json +2 -0
  39. data/tests/fixtures/fail3.json +1 -0
  40. data/tests/fixtures/fail4.json +1 -0
  41. data/tests/fixtures/fail5.json +1 -0
  42. data/tests/fixtures/fail6.json +1 -0
  43. data/tests/fixtures/fail7.json +1 -0
  44. data/tests/fixtures/fail8.json +1 -0
  45. data/tests/fixtures/fail9.json +1 -0
  46. data/tests/fixtures/pass1.json +56 -0
  47. data/tests/fixtures/pass15.json +1 -0
  48. data/tests/fixtures/pass16.json +1 -0
  49. data/tests/fixtures/pass17.json +1 -0
  50. data/tests/fixtures/pass2.json +1 -0
  51. data/tests/fixtures/pass26.json +1 -0
  52. data/tests/fixtures/pass3.json +6 -0
  53. data/tests/test_json.rb +361 -0
  54. data/tests/test_json_addition.rb +162 -0
  55. data/tests/test_json_encoding.rb +68 -0
  56. data/tests/test_json_fixtures.rb +34 -0
  57. data/tests/test_json_generate.rb +122 -0
  58. data/tests/test_json_rails.rb +144 -0
  59. data/tests/test_json_unicode.rb +76 -0
  60. metadata +122 -0
@@ -0,0 +1,58 @@
1
+ # This file contains implementations of rails custom objects for
2
+ # serialisation/deserialisation.
3
+
4
+ unless Object.const_defined?(:JSON) and ::JSON.const_defined?(:JSON_LOADED) and
5
+ ::JSON::JSON_LOADED
6
+ require 'json'
7
+ end
8
+
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
+
@@ -0,0 +1,397 @@
1
+ require 'json/version'
2
+ require 'iconv'
3
+
4
+ module JSON
5
+ class << self
6
+ # If _object_ is string-like parse the string and return the parsed result
7
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
8
+ # data structure object and return it.
9
+ #
10
+ # The _opts_ argument is passed through to generate/parse respectively, see
11
+ # generate and parse for their documentation.
12
+ def [](object, opts = {})
13
+ if object.respond_to? :to_str
14
+ JSON.parse(object.to_str, opts => {})
15
+ else
16
+ JSON.generate(object, opts => {})
17
+ end
18
+ end
19
+
20
+ # Returns the JSON parser class, that is used by JSON. This might be either
21
+ # JSON::Ext::Parser or JSON::Pure::Parser.
22
+ attr_reader :parser
23
+
24
+ # Set the JSON parser class _parser_ to be used by JSON.
25
+ def parser=(parser) # :nodoc:
26
+ @parser = parser
27
+ remove_const :Parser if const_defined? :Parser
28
+ const_set :Parser, parser
29
+ end
30
+
31
+ # Return the constant located at _path_. The format of _path_ has to be
32
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
33
+ # level (absolute namespace path?). If there doesn't exist a constant at
34
+ # the given path, an ArgumentError is raised.
35
+ def deep_const_get(path) # :nodoc:
36
+ path.to_s.split(/::/).inject(Object) do |p, c|
37
+ case
38
+ when c.empty? then p
39
+ when p.const_defined?(c) then p.const_get(c)
40
+ else
41
+ begin
42
+ p.const_missing(c)
43
+ rescue NameError
44
+ raise ArgumentError, "can't find const #{path}"
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ # Set the module _generator_ to be used by JSON.
51
+ def generator=(generator) # :nodoc:
52
+ @generator = generator
53
+ generator_methods = generator::GeneratorMethods
54
+ for const in generator_methods.constants
55
+ klass = deep_const_get(const)
56
+ modul = generator_methods.const_get(const)
57
+ klass.class_eval do
58
+ instance_methods(false).each do |m|
59
+ m.to_s == 'to_json' and remove_method m
60
+ end
61
+ include modul
62
+ end
63
+ end
64
+ self.state = generator::State
65
+ const_set :State, self.state
66
+ const_set :SAFE_STATE_PROTOTYPE, State.new.freeze
67
+ const_set :FAST_STATE_PROTOTYPE, State.new(
68
+ :indent => '',
69
+ :space => '',
70
+ :object_nl => "",
71
+ :array_nl => "",
72
+ :max_nesting => false
73
+ ).freeze
74
+ const_set :PRETTY_STATE_PROTOTYPE, State.new(
75
+ :indent => ' ',
76
+ :space => ' ',
77
+ :object_nl => "\n",
78
+ :array_nl => "\n"
79
+ ).freeze
80
+ end
81
+
82
+ # Returns the JSON generator modul, that is used by JSON. This might be
83
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
84
+ attr_reader :generator
85
+
86
+ # Returns the JSON generator state class, that is used by JSON. This might
87
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
88
+ attr_accessor :state
89
+
90
+ # This is create identifier, that is used to decide, if the _json_create_
91
+ # hook of a class should be called. It defaults to 'json_class'.
92
+ attr_accessor :create_id
93
+ end
94
+ self.create_id = 'json_class'
95
+
96
+ NaN = 0.0/0
97
+
98
+ Infinity = 1.0/0
99
+
100
+ MinusInfinity = -Infinity
101
+
102
+ # The base exception for JSON errors.
103
+ class JSONError < StandardError; end
104
+
105
+ # This exception is raised, if a parser error occurs.
106
+ class ParserError < JSONError; end
107
+
108
+ # This exception is raised, if the nesting of parsed datastructures is too
109
+ # deep.
110
+ class NestingError < ParserError; end
111
+
112
+ # :stopdoc:
113
+ class CircularDatastructure < NestingError; end
114
+ # :startdoc:
115
+
116
+ # This exception is raised, if a generator or unparser error occurs.
117
+ class GeneratorError < JSONError; end
118
+ # For backwards compatibility
119
+ UnparserError = GeneratorError
120
+
121
+ # This exception is raised, if the required unicode support is missing on the
122
+ # system. Usually this means, that the iconv library is not installed.
123
+ class MissingUnicodeSupport < JSONError; end
124
+
125
+ module_function
126
+
127
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
128
+ #
129
+ # _opts_ can have the following
130
+ # keys:
131
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
132
+ # structures. Disable depth checking with :max_nesting => false, it defaults
133
+ # to 19.
134
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
135
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
136
+ # to false.
137
+ # * *symbolize_names*: If set to true, returns symbols for the names
138
+ # (keys) in a JSON object. Otherwise strings are returned, which is also
139
+ # the default.
140
+ # * *create_additions*: If set to false, the Parser doesn't create
141
+ # additions even if a matchin class and create_id was found. This option
142
+ # defaults to true.
143
+ # * *object_class*: Defaults to Hash
144
+ # * *array_class*: Defaults to Array
145
+ def parse(source, opts = {})
146
+ Parser.new(source, opts).parse
147
+ end
148
+
149
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
150
+ # The bang version of the parse method, defaults to the more dangerous values
151
+ # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
152
+ #
153
+ # _opts_ can have the following keys:
154
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
155
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
156
+ # methods defaults to not doing max depth checking: This can be dangerous,
157
+ # if someone wants to fill up your stack.
158
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
159
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
160
+ # to true.
161
+ # * *create_additions*: If set to false, the Parser doesn't create
162
+ # additions even if a matchin class and create_id was found. This option
163
+ # defaults to true.
164
+ def parse!(source, opts = {})
165
+ opts = {
166
+ :max_nesting => false,
167
+ :allow_nan => true
168
+ }.update(opts)
169
+ Parser.new(source, opts).parse
170
+ end
171
+
172
+ # Generate a JSON document from the Ruby data structure _obj_ and return
173
+ # it. _state_ is * a JSON::State object,
174
+ # * or a Hash like object (responding to to_hash),
175
+ # * an object convertible into a hash by a to_h method,
176
+ # that is used as or to configure a State object.
177
+ #
178
+ # It defaults to a state object, that creates the shortest possible JSON text
179
+ # in one line, checks for circular data structures and doesn't allow NaN,
180
+ # Infinity, and -Infinity.
181
+ #
182
+ # A _state_ hash can have the following keys:
183
+ # * *indent*: a string used to indent levels (default: ''),
184
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
185
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
186
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
187
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
188
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
189
+ # generated, otherwise an exception is thrown, if these values are
190
+ # encountered. This options defaults to false.
191
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
192
+ # structures from which JSON is to be generated. Disable depth checking
193
+ # with :max_nesting => false, it defaults to 19.
194
+ #
195
+ # See also the fast_generate for the fastest creation method with the least
196
+ # amount of sanity checks, and the pretty_generate method for some
197
+ # defaults for a pretty output.
198
+ def generate(obj, opts = nil)
199
+ if opts
200
+ if opts.respond_to? :to_hash
201
+ opts = opts.to_hash
202
+ elsif opts.respond_to? :to_h
203
+ opts = opts.to_h
204
+ else
205
+ raise TypeError, "can't convert #{opts.class} into Hash"
206
+ end
207
+ state = SAFE_STATE_PROTOTYPE.dup
208
+ state = state.configure(opts)
209
+ else
210
+ state = SAFE_STATE_PROTOTYPE
211
+ end
212
+ state.generate(obj)
213
+ end
214
+
215
+ # :stopdoc:
216
+ # I want to deprecate these later, so I'll first be silent about them, and
217
+ # later delete them.
218
+ alias unparse generate
219
+ module_function :unparse
220
+ # :startdoc:
221
+
222
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
223
+ # This method disables the checks for circles in Ruby objects.
224
+ #
225
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
226
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
227
+ def fast_generate(obj, opts = nil)
228
+ if opts
229
+ if opts.respond_to? :to_hash
230
+ opts = opts.to_hash
231
+ elsif opts.respond_to? :to_h
232
+ opts = opts.to_h
233
+ else
234
+ raise TypeError, "can't convert #{opts.class} into Hash"
235
+ end
236
+ state = FAST_STATE_PROTOTYPE.dup
237
+ state.configure(opts)
238
+ else
239
+ state = FAST_STATE_PROTOTYPE
240
+ end
241
+ state.generate(obj)
242
+ end
243
+
244
+ # :stopdoc:
245
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
246
+ alias fast_unparse fast_generate
247
+ module_function :fast_unparse
248
+ # :startdoc:
249
+
250
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
251
+ # The returned document is a prettier form of the document returned by
252
+ # #unparse.
253
+ #
254
+ # The _opts_ argument can be used to configure the generator, see the
255
+ # generate method for a more detailed explanation.
256
+ def pretty_generate(obj, opts = nil)
257
+ if opts
258
+ if opts.respond_to? :to_hash
259
+ opts = opts.to_hash
260
+ elsif opts.respond_to? :to_h
261
+ opts = opts.to_h
262
+ else
263
+ raise TypeError, "can't convert #{opts.class} into Hash"
264
+ end
265
+ state = PRETTY_STATE_PROTOTYPE.dup
266
+ state.configure(opts)
267
+ else
268
+ state = PRETTY_STATE_PROTOTYPE
269
+ end
270
+ state.generate(obj)
271
+ end
272
+
273
+ # :stopdoc:
274
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
275
+ alias pretty_unparse pretty_generate
276
+ module_function :pretty_unparse
277
+ # :startdoc:
278
+
279
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
280
+ # either be a string-like object, an IO like object, or an object responding
281
+ # to the read method. If _proc_ was given, it will be called with any nested
282
+ # Ruby object as an argument recursively in depth first order.
283
+ #
284
+ # This method is part of the implementation of the load/dump interface of
285
+ # Marshal and YAML.
286
+ def load(source, proc = nil)
287
+ if source.respond_to? :to_str
288
+ source = source.to_str
289
+ elsif source.respond_to? :to_io
290
+ source = source.to_io.read
291
+ else
292
+ source = source.read
293
+ end
294
+ result = parse(source, :max_nesting => false, :allow_nan => true)
295
+ recurse_proc(result, &proc) if proc
296
+ result
297
+ end
298
+
299
+ def recurse_proc(result, &proc)
300
+ case result
301
+ when Array
302
+ result.each { |x| recurse_proc x, &proc }
303
+ proc.call result
304
+ when Hash
305
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
306
+ proc.call result
307
+ else
308
+ proc.call result
309
+ end
310
+ end
311
+
312
+ alias restore load
313
+ module_function :restore
314
+
315
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
316
+ # the result.
317
+ #
318
+ # If anIO (an IO like object or an object that responds to the write method)
319
+ # was given, the resulting JSON is written to it.
320
+ #
321
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
322
+ # exception is raised. This argument is similar (but not exactly the
323
+ # same!) to the _limit_ argument in Marshal.dump.
324
+ #
325
+ # This method is part of the implementation of the load/dump interface of
326
+ # Marshal and YAML.
327
+ def dump(obj, anIO = nil, limit = nil)
328
+ if anIO and limit.nil?
329
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
330
+ unless anIO.respond_to?(:write)
331
+ limit = anIO
332
+ anIO = nil
333
+ end
334
+ end
335
+ limit ||= 0
336
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
337
+ if anIO
338
+ anIO.write result
339
+ anIO
340
+ else
341
+ result
342
+ end
343
+ rescue JSON::NestingError
344
+ raise ArgumentError, "exceed depth limit"
345
+ end
346
+
347
+ # Shortuct for iconv.
348
+ def self.iconv(to, from, string)
349
+ Iconv.iconv(to, from, string).first
350
+ end
351
+ end
352
+
353
+ module ::Kernel
354
+ private
355
+
356
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
357
+ # one line.
358
+ def j(*objs)
359
+ objs.each do |obj|
360
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
361
+ end
362
+ nil
363
+ end
364
+
365
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
366
+ # indentation and over many lines.
367
+ def jj(*objs)
368
+ objs.each do |obj|
369
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
370
+ end
371
+ nil
372
+ end
373
+
374
+ # If _object_ is string-like parse the string and return the parsed result as
375
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
376
+ # structure object and return it.
377
+ #
378
+ # The _opts_ argument is passed through to generate/parse respectively, see
379
+ # generate and parse for their documentation.
380
+ def JSON(object, opts = {})
381
+ if object.respond_to? :to_str
382
+ JSON.parse(object.to_str, opts)
383
+ else
384
+ JSON.generate(object, opts)
385
+ end
386
+ end
387
+ end
388
+
389
+ class ::Class
390
+ # Returns true, if this class can be used to create an instance
391
+ # from a serialised JSON string. The class has to implement a class
392
+ # method _json_create_ that expects a hash as first parameter, which includes
393
+ # the required data.
394
+ def json_creatable?
395
+ respond_to?(:json_create)
396
+ end
397
+ end