json 1.8.6-java → 2.11.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +5 -5
  2. data/BSDL +22 -0
  3. data/CHANGES.md +619 -0
  4. data/COPYING +56 -0
  5. data/LEGAL +8 -0
  6. data/README.md +268 -0
  7. data/json.gemspec +63 -0
  8. data/lib/json/add/bigdecimal.rb +41 -11
  9. data/lib/json/add/complex.rb +32 -9
  10. data/lib/json/add/core.rb +1 -0
  11. data/lib/json/add/date.rb +27 -7
  12. data/lib/json/add/date_time.rb +26 -9
  13. data/lib/json/add/exception.rb +25 -7
  14. data/lib/json/add/ostruct.rb +32 -9
  15. data/lib/json/add/range.rb +33 -8
  16. data/lib/json/add/rational.rb +30 -8
  17. data/lib/json/add/regexp.rb +28 -10
  18. data/lib/json/add/set.rb +48 -0
  19. data/lib/json/add/struct.rb +29 -7
  20. data/lib/json/add/symbol.rb +34 -7
  21. data/lib/json/add/time.rb +29 -15
  22. data/lib/json/common.rb +916 -316
  23. data/lib/json/ext/generator/state.rb +106 -0
  24. data/lib/json/ext/generator.jar +0 -0
  25. data/lib/json/ext/parser.jar +0 -0
  26. data/lib/json/ext.rb +34 -10
  27. data/lib/json/generic_object.rb +11 -6
  28. data/lib/json/truffle_ruby/generator.rb +690 -0
  29. data/lib/json/version.rb +3 -6
  30. data/lib/json.rb +560 -35
  31. metadata +29 -82
  32. data/lib/json/pure/generator.rb +0 -530
  33. data/lib/json/pure/parser.rb +0 -359
  34. data/lib/json/pure.rb +0 -21
  35. data/tests/fixtures/fail1.json +0 -1
  36. data/tests/fixtures/fail10.json +0 -1
  37. data/tests/fixtures/fail11.json +0 -1
  38. data/tests/fixtures/fail12.json +0 -1
  39. data/tests/fixtures/fail13.json +0 -1
  40. data/tests/fixtures/fail14.json +0 -1
  41. data/tests/fixtures/fail18.json +0 -1
  42. data/tests/fixtures/fail19.json +0 -1
  43. data/tests/fixtures/fail2.json +0 -1
  44. data/tests/fixtures/fail20.json +0 -1
  45. data/tests/fixtures/fail21.json +0 -1
  46. data/tests/fixtures/fail22.json +0 -1
  47. data/tests/fixtures/fail23.json +0 -1
  48. data/tests/fixtures/fail24.json +0 -1
  49. data/tests/fixtures/fail25.json +0 -1
  50. data/tests/fixtures/fail27.json +0 -2
  51. data/tests/fixtures/fail28.json +0 -2
  52. data/tests/fixtures/fail3.json +0 -1
  53. data/tests/fixtures/fail4.json +0 -1
  54. data/tests/fixtures/fail5.json +0 -1
  55. data/tests/fixtures/fail6.json +0 -1
  56. data/tests/fixtures/fail7.json +0 -1
  57. data/tests/fixtures/fail8.json +0 -1
  58. data/tests/fixtures/fail9.json +0 -1
  59. data/tests/fixtures/pass1.json +0 -56
  60. data/tests/fixtures/pass15.json +0 -1
  61. data/tests/fixtures/pass16.json +0 -1
  62. data/tests/fixtures/pass17.json +0 -1
  63. data/tests/fixtures/pass2.json +0 -1
  64. data/tests/fixtures/pass26.json +0 -1
  65. data/tests/fixtures/pass3.json +0 -6
  66. data/tests/setup_variant.rb +0 -11
  67. data/tests/test_json.rb +0 -519
  68. data/tests/test_json_addition.rb +0 -196
  69. data/tests/test_json_encoding.rb +0 -65
  70. data/tests/test_json_fixtures.rb +0 -35
  71. data/tests/test_json_generate.rb +0 -348
  72. data/tests/test_json_generic_object.rb +0 -75
  73. data/tests/test_json_string_matching.rb +0 -39
  74. data/tests/test_json_unicode.rb +0 -72
@@ -1,359 +0,0 @@
1
- require 'strscan'
2
-
3
- module JSON
4
- module Pure
5
- # This class implements the JSON parser that is used to parse a JSON string
6
- # into a Ruby data structure.
7
- class Parser < StringScanner
8
- STRING = /" ((?:[^\x0-\x1f"\\] |
9
- # escaped special characters:
10
- \\["\\\/bfnrt] |
11
- \\u[0-9a-fA-F]{4} |
12
- # match all but escaped special characters:
13
- \\[\x20-\x21\x23-\x2e\x30-\x5b\x5d-\x61\x63-\x65\x67-\x6d\x6f-\x71\x73\x75-\xff])*)
14
- "/nx
15
- INTEGER = /(-?0|-?[1-9]\d*)/
16
- FLOAT = /(-?
17
- (?:0|[1-9]\d*)
18
- (?:
19
- \.\d+(?i:e[+-]?\d+) |
20
- \.\d+ |
21
- (?i:e[+-]?\d+)
22
- )
23
- )/x
24
- NAN = /NaN/
25
- INFINITY = /Infinity/
26
- MINUS_INFINITY = /-Infinity/
27
- OBJECT_OPEN = /\{/
28
- OBJECT_CLOSE = /\}/
29
- ARRAY_OPEN = /\[/
30
- ARRAY_CLOSE = /\]/
31
- PAIR_DELIMITER = /:/
32
- COLLECTION_DELIMITER = /,/
33
- TRUE = /true/
34
- FALSE = /false/
35
- NULL = /null/
36
- IGNORE = %r(
37
- (?:
38
- //[^\n\r]*[\n\r]| # line comments
39
- /\* # c-style comments
40
- (?:
41
- [^*/]| # normal chars
42
- /[^*]| # slashes that do not start a nested comment
43
- \*[^/]| # asterisks that do not end this comment
44
- /(?=\*/) # single slash before this comment's end
45
- )*
46
- \*/ # the End of this comment
47
- |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr
48
- )+
49
- )mx
50
-
51
- UNPARSED = Object.new
52
-
53
- # Creates a new JSON::Pure::Parser instance for the string _source_.
54
- #
55
- # It will be configured by the _opts_ hash. _opts_ can have the following
56
- # keys:
57
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
58
- # structures. Disable depth checking with :max_nesting => false|nil|0,
59
- # it defaults to 100.
60
- # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
61
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
62
- # to false.
63
- # * *symbolize_names*: If set to true, returns symbols for the names
64
- # (keys) in a JSON object. Otherwise strings are returned, which is also
65
- # the default.
66
- # * *create_additions*: If set to true, the Parser creates
67
- # additions when if a matching class and create_id was found. This
68
- # option defaults to false.
69
- # * *object_class*: Defaults to Hash
70
- # * *array_class*: Defaults to Array
71
- # * *quirks_mode*: Enables quirks_mode for parser, that is for example
72
- # parsing single JSON values instead of documents is possible.
73
- def initialize(source, opts = {})
74
- opts ||= {}
75
- unless @quirks_mode = opts[:quirks_mode]
76
- source = convert_encoding source
77
- end
78
- super source
79
- if !opts.key?(:max_nesting) # defaults to 100
80
- @max_nesting = 100
81
- elsif opts[:max_nesting]
82
- @max_nesting = opts[:max_nesting]
83
- else
84
- @max_nesting = 0
85
- end
86
- @allow_nan = !!opts[:allow_nan]
87
- @symbolize_names = !!opts[:symbolize_names]
88
- if opts.key?(:create_additions)
89
- @create_additions = !!opts[:create_additions]
90
- else
91
- @create_additions = false
92
- end
93
- @create_id = @create_additions ? JSON.create_id : nil
94
- @object_class = opts[:object_class] || Hash
95
- @array_class = opts[:array_class] || Array
96
- @match_string = opts[:match_string]
97
- end
98
-
99
- alias source string
100
-
101
- def quirks_mode?
102
- !!@quirks_mode
103
- end
104
-
105
- def reset
106
- super
107
- @current_nesting = 0
108
- end
109
-
110
- # Parses the current JSON string _source_ and returns the complete data
111
- # structure as a result.
112
- def parse
113
- reset
114
- obj = nil
115
- if @quirks_mode
116
- while !eos? && skip(IGNORE)
117
- end
118
- if eos?
119
- raise ParserError, "source did not contain any JSON!"
120
- else
121
- obj = parse_value
122
- obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
123
- end
124
- else
125
- until eos?
126
- case
127
- when scan(OBJECT_OPEN)
128
- obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
129
- @current_nesting = 1
130
- obj = parse_object
131
- when scan(ARRAY_OPEN)
132
- obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
133
- @current_nesting = 1
134
- obj = parse_array
135
- when skip(IGNORE)
136
- ;
137
- else
138
- raise ParserError, "source '#{peek(20)}' not in JSON!"
139
- end
140
- end
141
- obj or raise ParserError, "source did not contain any JSON!"
142
- end
143
- obj
144
- end
145
-
146
- private
147
-
148
- def convert_encoding(source)
149
- if source.respond_to?(:to_str)
150
- source = source.to_str
151
- else
152
- raise TypeError, "#{source.inspect} is not like a string"
153
- end
154
- if defined?(::Encoding)
155
- if source.encoding == ::Encoding::ASCII_8BIT
156
- b = source[0, 4].bytes.to_a
157
- source =
158
- case
159
- when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
160
- source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
161
- when b.size >= 4 && b[0] == 0 && b[2] == 0
162
- source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
163
- when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
164
- source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
165
- when b.size >= 4 && b[1] == 0 && b[3] == 0
166
- source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
167
- else
168
- source.dup
169
- end
170
- else
171
- source = source.encode(::Encoding::UTF_8)
172
- end
173
- source.force_encoding(::Encoding::ASCII_8BIT)
174
- else
175
- b = source
176
- source =
177
- case
178
- when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
179
- JSON.iconv('utf-8', 'utf-32be', b)
180
- when b.size >= 4 && b[0] == 0 && b[2] == 0
181
- JSON.iconv('utf-8', 'utf-16be', b)
182
- when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
183
- JSON.iconv('utf-8', 'utf-32le', b)
184
- when b.size >= 4 && b[1] == 0 && b[3] == 0
185
- JSON.iconv('utf-8', 'utf-16le', b)
186
- else
187
- b
188
- end
189
- end
190
- source
191
- end
192
-
193
- # Unescape characters in strings.
194
- UNESCAPE_MAP = Hash.new { |h, k| h[k] = k.chr }
195
- UNESCAPE_MAP.update({
196
- ?" => '"',
197
- ?\\ => '\\',
198
- ?/ => '/',
199
- ?b => "\b",
200
- ?f => "\f",
201
- ?n => "\n",
202
- ?r => "\r",
203
- ?t => "\t",
204
- ?u => nil,
205
- })
206
-
207
- EMPTY_8BIT_STRING = ''
208
- if ::String.method_defined?(:encode)
209
- EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
210
- end
211
-
212
- def parse_string
213
- if scan(STRING)
214
- return '' if self[1].empty?
215
- string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
216
- if u = UNESCAPE_MAP[$&[1]]
217
- u
218
- else # \uXXXX
219
- bytes = EMPTY_8BIT_STRING.dup
220
- i = 0
221
- while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
222
- bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
223
- i += 1
224
- end
225
- JSON.iconv('utf-8', 'utf-16be', bytes)
226
- end
227
- end
228
- if string.respond_to?(:force_encoding)
229
- string.force_encoding(::Encoding::UTF_8)
230
- end
231
- if @create_additions and @match_string
232
- for (regexp, klass) in @match_string
233
- klass.json_creatable? or next
234
- string =~ regexp and return klass.json_create(string)
235
- end
236
- end
237
- string
238
- else
239
- UNPARSED
240
- end
241
- rescue => e
242
- raise ParserError, "Caught #{e.class} at '#{peek(20)}': #{e}"
243
- end
244
-
245
- def parse_value
246
- case
247
- when scan(FLOAT)
248
- Float(self[1])
249
- when scan(INTEGER)
250
- Integer(self[1])
251
- when scan(TRUE)
252
- true
253
- when scan(FALSE)
254
- false
255
- when scan(NULL)
256
- nil
257
- when (string = parse_string) != UNPARSED
258
- string
259
- when scan(ARRAY_OPEN)
260
- @current_nesting += 1
261
- ary = parse_array
262
- @current_nesting -= 1
263
- ary
264
- when scan(OBJECT_OPEN)
265
- @current_nesting += 1
266
- obj = parse_object
267
- @current_nesting -= 1
268
- obj
269
- when @allow_nan && scan(NAN)
270
- NaN
271
- when @allow_nan && scan(INFINITY)
272
- Infinity
273
- when @allow_nan && scan(MINUS_INFINITY)
274
- MinusInfinity
275
- else
276
- UNPARSED
277
- end
278
- end
279
-
280
- def parse_array
281
- raise NestingError, "nesting of #@current_nesting is too deep" if
282
- @max_nesting.nonzero? && @current_nesting > @max_nesting
283
- result = @array_class.new
284
- delim = false
285
- until eos?
286
- case
287
- when (value = parse_value) != UNPARSED
288
- delim = false
289
- result << value
290
- skip(IGNORE)
291
- if scan(COLLECTION_DELIMITER)
292
- delim = true
293
- elsif match?(ARRAY_CLOSE)
294
- ;
295
- else
296
- raise ParserError, "expected ',' or ']' in array at '#{peek(20)}'!"
297
- end
298
- when scan(ARRAY_CLOSE)
299
- if delim
300
- raise ParserError, "expected next element in array at '#{peek(20)}'!"
301
- end
302
- break
303
- when skip(IGNORE)
304
- ;
305
- else
306
- raise ParserError, "unexpected token in array at '#{peek(20)}'!"
307
- end
308
- end
309
- result
310
- end
311
-
312
- def parse_object
313
- raise NestingError, "nesting of #@current_nesting is too deep" if
314
- @max_nesting.nonzero? && @current_nesting > @max_nesting
315
- result = @object_class.new
316
- delim = false
317
- until eos?
318
- case
319
- when (string = parse_string) != UNPARSED
320
- skip(IGNORE)
321
- unless scan(PAIR_DELIMITER)
322
- raise ParserError, "expected ':' in object at '#{peek(20)}'!"
323
- end
324
- skip(IGNORE)
325
- unless (value = parse_value).equal? UNPARSED
326
- result[@symbolize_names ? string.to_sym : string] = value
327
- delim = false
328
- skip(IGNORE)
329
- if scan(COLLECTION_DELIMITER)
330
- delim = true
331
- elsif match?(OBJECT_CLOSE)
332
- ;
333
- else
334
- raise ParserError, "expected ',' or '}' in object at '#{peek(20)}'!"
335
- end
336
- else
337
- raise ParserError, "expected value in object at '#{peek(20)}'!"
338
- end
339
- when scan(OBJECT_CLOSE)
340
- if delim
341
- raise ParserError, "expected next name, value pair in object at '#{peek(20)}'!"
342
- end
343
- if @create_additions and klassname = result[@create_id]
344
- klass = JSON.deep_const_get klassname
345
- break unless klass and klass.json_creatable?
346
- result = klass.json_create(result)
347
- end
348
- break
349
- when skip(IGNORE)
350
- ;
351
- else
352
- raise ParserError, "unexpected token in object at '#{peek(20)}'!"
353
- end
354
- end
355
- result
356
- end
357
- end
358
- end
359
- end
data/lib/json/pure.rb DELETED
@@ -1,21 +0,0 @@
1
- if ENV['SIMPLECOV_COVERAGE'].to_i == 1
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter "/tests/"
5
- end
6
- end
7
- require 'json/common'
8
- require 'json/pure/parser'
9
- require 'json/pure/generator'
10
-
11
- module JSON
12
- # This module holds all the modules/classes that implement JSON's
13
- # functionality in pure ruby.
14
- module Pure
15
- $DEBUG and warn "Using Pure library for JSON."
16
- JSON.parser = Parser
17
- JSON.generator = Generator
18
- end
19
-
20
- JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)
21
- end
@@ -1 +0,0 @@
1
- "A JSON payload should be an object or array, not a string."
@@ -1 +0,0 @@
1
- {"Extra value after close": true} "misplaced quoted value"
@@ -1 +0,0 @@
1
- {"Illegal expression": 1 + 2}
@@ -1 +0,0 @@
1
- {"Illegal invocation": alert()}
@@ -1 +0,0 @@
1
- {"Numbers cannot have leading zeroes": 013}
@@ -1 +0,0 @@
1
- {"Numbers cannot be hex": 0x14}
@@ -1 +0,0 @@
1
- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
@@ -1 +0,0 @@
1
- {"Missing colon" null}
@@ -1 +0,0 @@
1
- ["Unclosed array"
@@ -1 +0,0 @@
1
- {"Double colon":: null}
@@ -1 +0,0 @@
1
- {"Comma instead of colon", null}
@@ -1 +0,0 @@
1
- ["Colon instead of comma": false]
@@ -1 +0,0 @@
1
- ["Bad value", truth]
@@ -1 +0,0 @@
1
- ['single quote']
@@ -1 +0,0 @@
1
- ["tab character in string "]
@@ -1,2 +0,0 @@
1
- ["line
2
- break"]
@@ -1,2 +0,0 @@
1
- ["line\
2
- break"]
@@ -1 +0,0 @@
1
- {unquoted_key: "keys must be quoted"}
@@ -1 +0,0 @@
1
- ["extra comma",]
@@ -1 +0,0 @@
1
- ["double extra comma",,]
@@ -1 +0,0 @@
1
- [ , "<-- missing value"]
@@ -1 +0,0 @@
1
- ["Comma after the close"],
@@ -1 +0,0 @@
1
- ["Extra close"]]
@@ -1 +0,0 @@
1
- {"Extra comma": true,}
@@ -1,56 +0,0 @@
1
- [
2
- "JSON Test Pattern pass1",
3
- {"object with 1 member":["array with 1 element"]},
4
- {},
5
- [],
6
- -42,
7
- true,
8
- false,
9
- null,
10
- {
11
- "integer": 1234567890,
12
- "real": -9876.543210,
13
- "e": 0.123456789e-12,
14
- "E": 1.234567890E+34,
15
- "": 23456789012E666,
16
- "zero": 0,
17
- "one": 1,
18
- "space": " ",
19
- "quote": "\"",
20
- "backslash": "\\",
21
- "controls": "\b\f\n\r\t",
22
- "slash": "/ & \/",
23
- "alpha": "abcdefghijklmnopqrstuvwyz",
24
- "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
25
- "digit": "0123456789",
26
- "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
27
- "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
28
- "true": true,
29
- "false": false,
30
- "null": null,
31
- "array":[ ],
32
- "object":{ },
33
- "address": "50 St. James Street",
34
- "url": "http://www.JSON.org/",
35
- "comment": "// /* <!-- --",
36
- "# -- --> */": " ",
37
- " s p a c e d " :[1,2 , 3
38
-
39
- ,
40
-
41
- 4 , 5 , 6 ,7 ],
42
- "compact": [1,2,3,4,5,6,7],
43
- "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
44
- "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
45
- "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
46
- : "A key can be any string"
47
- },
48
- 0.5 ,98.6
49
- ,
50
- 99.44
51
- ,
52
-
53
- 1066
54
-
55
-
56
- ,"rosebud"]
@@ -1 +0,0 @@
1
- ["Illegal backslash escape: \x15"]
@@ -1 +0,0 @@
1
- ["Illegal backslash escape: \'"]
@@ -1 +0,0 @@
1
- ["Illegal backslash escape: \017"]
@@ -1 +0,0 @@
1
- [[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
@@ -1 +0,0 @@
1
- ["tab\ character\ in\ string\ "]
@@ -1,6 +0,0 @@
1
- {
2
- "JSON Test Pattern pass3": {
3
- "The outermost value": "must be an object or array.",
4
- "In this test": "It is an object."
5
- }
6
- }
@@ -1,11 +0,0 @@
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