json 1.8.6 → 2.7.2

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 (107) hide show
  1. checksums.yaml +5 -5
  2. data/{CHANGES → CHANGES.md} +292 -96
  3. data/LICENSE +56 -0
  4. data/README.md +185 -114
  5. data/ext/json/ext/fbuffer/fbuffer.h +0 -3
  6. data/ext/json/ext/generator/generator.c +328 -117
  7. data/ext/json/ext/generator/generator.h +8 -8
  8. data/ext/json/ext/parser/extconf.rb +29 -0
  9. data/ext/json/ext/parser/parser.c +540 -569
  10. data/ext/json/ext/parser/parser.h +10 -6
  11. data/ext/json/ext/parser/parser.rl +269 -261
  12. data/ext/json/extconf.rb +1 -1
  13. data/json.gemspec +0 -0
  14. data/lib/json/add/bigdecimal.rb +40 -10
  15. data/lib/json/add/complex.rb +32 -9
  16. data/lib/json/add/core.rb +1 -0
  17. data/lib/json/add/date.rb +27 -7
  18. data/lib/json/add/date_time.rb +26 -9
  19. data/lib/json/add/exception.rb +25 -7
  20. data/lib/json/add/ostruct.rb +32 -9
  21. data/lib/json/add/range.rb +33 -8
  22. data/lib/json/add/rational.rb +30 -8
  23. data/lib/json/add/regexp.rb +28 -10
  24. data/lib/json/add/set.rb +48 -0
  25. data/lib/json/add/struct.rb +29 -7
  26. data/lib/json/add/symbol.rb +28 -5
  27. data/lib/json/add/time.rb +27 -6
  28. data/lib/json/common.rb +402 -188
  29. data/lib/json/ext.rb +0 -6
  30. data/lib/json/generic_object.rb +11 -6
  31. data/lib/json/pure/generator.rb +120 -137
  32. data/lib/json/pure/parser.rb +64 -86
  33. data/lib/json/pure.rb +2 -8
  34. data/lib/json/version.rb +2 -1
  35. data/lib/json.rb +559 -29
  36. metadata +18 -129
  37. data/.gitignore +0 -17
  38. data/.travis.yml +0 -18
  39. data/Gemfile +0 -7
  40. data/README-json-jruby.markdown +0 -33
  41. data/Rakefile +0 -402
  42. data/TODO +0 -1
  43. data/VERSION +0 -1
  44. data/data/example.json +0 -1
  45. data/data/index.html +0 -38
  46. data/data/prototype.js +0 -4184
  47. data/diagrams/.keep +0 -0
  48. data/install.rb +0 -23
  49. data/java/src/json/ext/ByteListTranscoder.java +0 -166
  50. data/java/src/json/ext/Generator.java +0 -446
  51. data/java/src/json/ext/GeneratorMethods.java +0 -231
  52. data/java/src/json/ext/GeneratorService.java +0 -42
  53. data/java/src/json/ext/GeneratorState.java +0 -542
  54. data/java/src/json/ext/OptionsReader.java +0 -113
  55. data/java/src/json/ext/Parser.java +0 -2644
  56. data/java/src/json/ext/Parser.rl +0 -968
  57. data/java/src/json/ext/ParserService.java +0 -34
  58. data/java/src/json/ext/RuntimeInfo.java +0 -120
  59. data/java/src/json/ext/StringDecoder.java +0 -166
  60. data/java/src/json/ext/StringEncoder.java +0 -111
  61. data/java/src/json/ext/Utils.java +0 -88
  62. data/json-java.gemspec +0 -38
  63. data/json_pure.gemspec +0 -37
  64. data/lib/json/ext/.keep +0 -0
  65. data/tests/fixtures/fail1.json +0 -1
  66. data/tests/fixtures/fail10.json +0 -1
  67. data/tests/fixtures/fail11.json +0 -1
  68. data/tests/fixtures/fail12.json +0 -1
  69. data/tests/fixtures/fail13.json +0 -1
  70. data/tests/fixtures/fail14.json +0 -1
  71. data/tests/fixtures/fail18.json +0 -1
  72. data/tests/fixtures/fail19.json +0 -1
  73. data/tests/fixtures/fail2.json +0 -1
  74. data/tests/fixtures/fail20.json +0 -1
  75. data/tests/fixtures/fail21.json +0 -1
  76. data/tests/fixtures/fail22.json +0 -1
  77. data/tests/fixtures/fail23.json +0 -1
  78. data/tests/fixtures/fail24.json +0 -1
  79. data/tests/fixtures/fail25.json +0 -1
  80. data/tests/fixtures/fail27.json +0 -2
  81. data/tests/fixtures/fail28.json +0 -2
  82. data/tests/fixtures/fail3.json +0 -1
  83. data/tests/fixtures/fail4.json +0 -1
  84. data/tests/fixtures/fail5.json +0 -1
  85. data/tests/fixtures/fail6.json +0 -1
  86. data/tests/fixtures/fail7.json +0 -1
  87. data/tests/fixtures/fail8.json +0 -1
  88. data/tests/fixtures/fail9.json +0 -1
  89. data/tests/fixtures/pass1.json +0 -56
  90. data/tests/fixtures/pass15.json +0 -1
  91. data/tests/fixtures/pass16.json +0 -1
  92. data/tests/fixtures/pass17.json +0 -1
  93. data/tests/fixtures/pass2.json +0 -1
  94. data/tests/fixtures/pass26.json +0 -1
  95. data/tests/fixtures/pass3.json +0 -6
  96. data/tests/setup_variant.rb +0 -11
  97. data/tests/test_json.rb +0 -519
  98. data/tests/test_json_addition.rb +0 -196
  99. data/tests/test_json_encoding.rb +0 -65
  100. data/tests/test_json_fixtures.rb +0 -35
  101. data/tests/test_json_generate.rb +0 -348
  102. data/tests/test_json_generic_object.rb +0 -75
  103. data/tests/test_json_string_matching.rb +0 -39
  104. data/tests/test_json_unicode.rb +0 -72
  105. data/tools/diff.sh +0 -18
  106. data/tools/fuzz.rb +0 -139
  107. data/tools/server.rb +0 -62
@@ -1,3 +1,4 @@
1
+ #frozen_string_literal: false
1
2
  require 'strscan'
2
3
 
3
4
  module JSON
@@ -44,11 +45,11 @@ module JSON
44
45
  /(?=\*/) # single slash before this comment's end
45
46
  )*
46
47
  \*/ # the End of this comment
47
- |[ \t\r\n]+ # whitespaces: space, horicontal tab, lf, cr
48
+ |[ \t\r\n]+ # whitespaces: space, horizontal tab, lf, cr
48
49
  )+
49
50
  )mx
50
51
 
51
- UNPARSED = Object.new
52
+ UNPARSED = Object.new.freeze
52
53
 
53
54
  # Creates a new JSON::Pure::Parser instance for the string _source_.
54
55
  #
@@ -58,23 +59,25 @@ module JSON
58
59
  # structures. Disable depth checking with :max_nesting => false|nil|0,
59
60
  # it defaults to 100.
60
61
  # * *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
+ # defiance of RFC 7159 to be parsed by the Parser. This option defaults
62
63
  # to false.
64
+ # * *freeze*: If set to true, all parsed objects will be frozen. Parsed
65
+ # string will be deduplicated if possible.
63
66
  # * *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.
67
+ # (keys) in a JSON object. Otherwise strings are returned, which is
68
+ # also the default. It's not possible to use this option in
69
+ # conjunction with the *create_additions* option.
66
70
  # * *create_additions*: If set to true, the Parser creates
67
- # additions when if a matching class and create_id was found. This
71
+ # additions when a matching class and create_id are found. This
68
72
  # option defaults to false.
69
73
  # * *object_class*: Defaults to Hash
70
74
  # * *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.
75
+ # * *decimal_class*: Specifies which class to use instead of the default
76
+ # (Float) when parsing decimal numbers. This class must accept a single
77
+ # string argument in its constructor.
73
78
  def initialize(source, opts = {})
74
79
  opts ||= {}
75
- unless @quirks_mode = opts[:quirks_mode]
76
- source = convert_encoding source
77
- end
80
+ source = convert_encoding source
78
81
  super source
79
82
  if !opts.key?(:max_nesting) # defaults to 100
80
83
  @max_nesting = 100
@@ -85,61 +88,45 @@ module JSON
85
88
  end
86
89
  @allow_nan = !!opts[:allow_nan]
87
90
  @symbolize_names = !!opts[:symbolize_names]
91
+ @freeze = !!opts[:freeze]
88
92
  if opts.key?(:create_additions)
89
93
  @create_additions = !!opts[:create_additions]
90
94
  else
91
95
  @create_additions = false
92
96
  end
97
+ @symbolize_names && @create_additions and raise ArgumentError,
98
+ 'options :symbolize_names and :create_additions cannot be used '\
99
+ 'in conjunction'
93
100
  @create_id = @create_additions ? JSON.create_id : nil
94
101
  @object_class = opts[:object_class] || Hash
95
102
  @array_class = opts[:array_class] || Array
103
+ @decimal_class = opts[:decimal_class]
96
104
  @match_string = opts[:match_string]
97
105
  end
98
106
 
99
107
  alias source string
100
108
 
101
- def quirks_mode?
102
- !!@quirks_mode
103
- end
104
-
105
109
  def reset
106
110
  super
107
111
  @current_nesting = 0
108
112
  end
109
113
 
110
- # Parses the current JSON string _source_ and returns the complete data
111
- # structure as a result.
114
+ # Parses the current JSON string _source_ and returns the
115
+ # complete data structure as a result.
112
116
  def parse
113
117
  reset
114
118
  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
119
+ while !eos? && skip(IGNORE) do end
120
+ if eos?
121
+ raise ParserError, "source is not valid JSON!"
124
122
  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!"
123
+ obj = parse_value
124
+ UNPARSED.equal?(obj) and raise ParserError,
125
+ "source is not valid JSON!"
126
+ obj.freeze if @freeze
142
127
  end
128
+ while !eos? && skip(IGNORE) do end
129
+ eos? or raise ParserError, "source is not valid JSON!"
143
130
  obj
144
131
  end
145
132
 
@@ -149,43 +136,12 @@ module JSON
149
136
  if source.respond_to?(:to_str)
150
137
  source = source.to_str
151
138
  else
152
- raise TypeError, "#{source.inspect} is not like a string"
139
+ raise TypeError,
140
+ "#{source.inspect} is not like a string"
153
141
  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
142
+ if source.encoding != ::Encoding::ASCII_8BIT
143
+ source = source.encode(::Encoding::UTF_8)
173
144
  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
145
  end
190
146
  source
191
147
  end
@@ -209,6 +165,7 @@ module JSON
209
165
  EMPTY_8BIT_STRING.force_encoding Encoding::ASCII_8BIT
210
166
  end
211
167
 
168
+ STR_UMINUS = ''.respond_to?(:-@)
212
169
  def parse_string
213
170
  if scan(STRING)
214
171
  return '' if self[1].empty?
@@ -222,12 +179,21 @@ module JSON
222
179
  bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
223
180
  i += 1
224
181
  end
225
- JSON.iconv('utf-8', 'utf-16be', bytes)
182
+ JSON.iconv('utf-8', 'utf-16be', bytes).force_encoding(::Encoding::ASCII_8BIT)
226
183
  end
227
184
  end
228
185
  if string.respond_to?(:force_encoding)
229
186
  string.force_encoding(::Encoding::UTF_8)
230
187
  end
188
+
189
+ if @freeze
190
+ if STR_UMINUS
191
+ string = -string
192
+ else
193
+ string.freeze
194
+ end
195
+ end
196
+
231
197
  if @create_additions and @match_string
232
198
  for (regexp, klass) in @match_string
233
199
  klass.json_creatable? or next
@@ -245,7 +211,15 @@ module JSON
245
211
  def parse_value
246
212
  case
247
213
  when scan(FLOAT)
248
- Float(self[1])
214
+ if @decimal_class then
215
+ if @decimal_class == BigDecimal then
216
+ BigDecimal(self[1])
217
+ else
218
+ @decimal_class.new(self[1]) || Float(self[1])
219
+ end
220
+ else
221
+ Float(self[1])
222
+ end
249
223
  when scan(INTEGER)
250
224
  Integer(self[1])
251
225
  when scan(TRUE)
@@ -254,7 +228,7 @@ module JSON
254
228
  false
255
229
  when scan(NULL)
256
230
  nil
257
- when (string = parse_string) != UNPARSED
231
+ when !UNPARSED.equal?(string = parse_string)
258
232
  string
259
233
  when scan(ARRAY_OPEN)
260
234
  @current_nesting += 1
@@ -282,9 +256,11 @@ module JSON
282
256
  @max_nesting.nonzero? && @current_nesting > @max_nesting
283
257
  result = @array_class.new
284
258
  delim = false
285
- until eos?
259
+ loop do
286
260
  case
287
- when (value = parse_value) != UNPARSED
261
+ when eos?
262
+ raise ParserError, "unexpected end of string while parsing array"
263
+ when !UNPARSED.equal?(value = parse_value)
288
264
  delim = false
289
265
  result << value
290
266
  skip(IGNORE)
@@ -314,15 +290,17 @@ module JSON
314
290
  @max_nesting.nonzero? && @current_nesting > @max_nesting
315
291
  result = @object_class.new
316
292
  delim = false
317
- until eos?
293
+ loop do
318
294
  case
319
- when (string = parse_string) != UNPARSED
295
+ when eos?
296
+ raise ParserError, "unexpected end of string while parsing object"
297
+ when !UNPARSED.equal?(string = parse_string)
320
298
  skip(IGNORE)
321
299
  unless scan(PAIR_DELIMITER)
322
300
  raise ParserError, "expected ':' in object at '#{peek(20)}'!"
323
301
  end
324
302
  skip(IGNORE)
325
- unless (value = parse_value).equal? UNPARSED
303
+ unless UNPARSED.equal?(value = parse_value)
326
304
  result[@symbolize_names ? string.to_sym : string] = value
327
305
  delim = false
328
306
  skip(IGNORE)
data/lib/json/pure.rb CHANGED
@@ -1,17 +1,11 @@
1
- if ENV['SIMPLECOV_COVERAGE'].to_i == 1
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter "/tests/"
5
- end
6
- end
7
1
  require 'json/common'
8
- require 'json/pure/parser'
9
- require 'json/pure/generator'
10
2
 
11
3
  module JSON
12
4
  # This module holds all the modules/classes that implement JSON's
13
5
  # functionality in pure ruby.
14
6
  module Pure
7
+ require 'json/pure/parser'
8
+ require 'json/pure/generator'
15
9
  $DEBUG and warn "Using Pure library for JSON."
16
10
  JSON.parser = Parser
17
11
  JSON.generator = Generator
data/lib/json/version.rb CHANGED
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: false
1
2
  module JSON
2
3
  # JSON version
3
- VERSION = '1.8.6'
4
+ VERSION = '2.7.2'
4
5
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
6
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
7
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: