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
metadata CHANGED
@@ -1,49 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.6
4
+ version: 2.11.1
5
5
  platform: java
6
6
  authors:
7
7
  - Daniel Luz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-13 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - ">="
17
- - !ruby/object:Gem::Version
18
- version: '0'
19
- name: rake
20
- prerelease: false
21
- type: :development
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- requirement: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - "~>"
31
- - !ruby/object:Gem::Version
32
- version: '2.0'
33
- name: test-unit
34
- prerelease: false
35
- type: :development
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '2.0'
11
+ date: 2025-04-24 00:00:00.000000000 Z
12
+ dependencies: []
41
13
  description: A JSON implementation as a JRuby extension.
42
14
  email: dev+ruby@mernen.com
43
15
  executables: []
44
16
  extensions: []
45
- extra_rdoc_files: []
17
+ extra_rdoc_files:
18
+ - README.md
46
19
  files:
20
+ - BSDL
21
+ - CHANGES.md
22
+ - COPYING
23
+ - LEGAL
24
+ - README.md
25
+ - json.gemspec
47
26
  - lib/json.rb
48
27
  - lib/json/add/bigdecimal.rb
49
28
  - lib/json/add/complex.rb
@@ -55,80 +34,48 @@ files:
55
34
  - lib/json/add/range.rb
56
35
  - lib/json/add/rational.rb
57
36
  - lib/json/add/regexp.rb
37
+ - lib/json/add/set.rb
58
38
  - lib/json/add/struct.rb
59
39
  - lib/json/add/symbol.rb
60
40
  - lib/json/add/time.rb
61
41
  - lib/json/common.rb
62
42
  - lib/json/ext.rb
63
43
  - lib/json/ext/generator.jar
44
+ - lib/json/ext/generator/state.rb
64
45
  - lib/json/ext/parser.jar
65
46
  - lib/json/generic_object.rb
66
- - lib/json/pure.rb
67
- - lib/json/pure/generator.rb
68
- - lib/json/pure/parser.rb
47
+ - lib/json/truffle_ruby/generator.rb
69
48
  - lib/json/version.rb
70
- - tests/fixtures/fail1.json
71
- - tests/fixtures/fail10.json
72
- - tests/fixtures/fail11.json
73
- - tests/fixtures/fail12.json
74
- - tests/fixtures/fail13.json
75
- - tests/fixtures/fail14.json
76
- - tests/fixtures/fail18.json
77
- - tests/fixtures/fail19.json
78
- - tests/fixtures/fail2.json
79
- - tests/fixtures/fail20.json
80
- - tests/fixtures/fail21.json
81
- - tests/fixtures/fail22.json
82
- - tests/fixtures/fail23.json
83
- - tests/fixtures/fail24.json
84
- - tests/fixtures/fail25.json
85
- - tests/fixtures/fail27.json
86
- - tests/fixtures/fail28.json
87
- - tests/fixtures/fail3.json
88
- - tests/fixtures/fail4.json
89
- - tests/fixtures/fail5.json
90
- - tests/fixtures/fail6.json
91
- - tests/fixtures/fail7.json
92
- - tests/fixtures/fail8.json
93
- - tests/fixtures/fail9.json
94
- - tests/fixtures/pass1.json
95
- - tests/fixtures/pass15.json
96
- - tests/fixtures/pass16.json
97
- - tests/fixtures/pass17.json
98
- - tests/fixtures/pass2.json
99
- - tests/fixtures/pass26.json
100
- - tests/fixtures/pass3.json
101
- - tests/setup_variant.rb
102
- - tests/test_json.rb
103
- - tests/test_json_addition.rb
104
- - tests/test_json_encoding.rb
105
- - tests/test_json_fixtures.rb
106
- - tests/test_json_generate.rb
107
- - tests/test_json_generic_object.rb
108
- - tests/test_json_string_matching.rb
109
- - tests/test_json_unicode.rb
110
- homepage: http://json-jruby.rubyforge.org/
49
+ homepage: https://github.com/ruby/json
111
50
  licenses:
112
51
  - Ruby
113
- metadata: {}
52
+ metadata:
53
+ bug_tracker_uri: https://github.com/ruby/json/issues
54
+ changelog_uri: https://github.com/ruby/json/blob/master/CHANGES.md
55
+ documentation_uri: https://docs.ruby-lang.org/en/master/JSON.html
56
+ homepage_uri: https://github.com/ruby/json
57
+ source_code_uri: https://github.com/ruby/json
114
58
  post_install_message:
115
- rdoc_options: []
59
+ rdoc_options:
60
+ - "--title"
61
+ - JSON implementation for Ruby
62
+ - "--main"
63
+ - README.md
116
64
  require_paths:
117
65
  - lib
118
66
  required_ruby_version: !ruby/object:Gem::Requirement
119
67
  requirements:
120
68
  - - ">="
121
69
  - !ruby/object:Gem::Version
122
- version: '0'
70
+ version: '2.7'
123
71
  required_rubygems_version: !ruby/object:Gem::Requirement
124
72
  requirements:
125
73
  - - ">="
126
74
  - !ruby/object:Gem::Version
127
75
  version: '0'
128
76
  requirements: []
129
- rubyforge_project: json-jruby
130
- rubygems_version: 2.6.8
77
+ rubygems_version: 3.3.26
131
78
  signing_key:
132
79
  specification_version: 4
133
- summary: JSON implementation for JRuby
80
+ summary: JSON Implementation for Ruby
134
81
  test_files: []
@@ -1,530 +0,0 @@
1
- module JSON
2
- MAP = {
3
- "\x0" => '\u0000',
4
- "\x1" => '\u0001',
5
- "\x2" => '\u0002',
6
- "\x3" => '\u0003',
7
- "\x4" => '\u0004',
8
- "\x5" => '\u0005',
9
- "\x6" => '\u0006',
10
- "\x7" => '\u0007',
11
- "\b" => '\b',
12
- "\t" => '\t',
13
- "\n" => '\n',
14
- "\xb" => '\u000b',
15
- "\f" => '\f',
16
- "\r" => '\r',
17
- "\xe" => '\u000e',
18
- "\xf" => '\u000f',
19
- "\x10" => '\u0010',
20
- "\x11" => '\u0011',
21
- "\x12" => '\u0012',
22
- "\x13" => '\u0013',
23
- "\x14" => '\u0014',
24
- "\x15" => '\u0015',
25
- "\x16" => '\u0016',
26
- "\x17" => '\u0017',
27
- "\x18" => '\u0018',
28
- "\x19" => '\u0019',
29
- "\x1a" => '\u001a',
30
- "\x1b" => '\u001b',
31
- "\x1c" => '\u001c',
32
- "\x1d" => '\u001d',
33
- "\x1e" => '\u001e',
34
- "\x1f" => '\u001f',
35
- '"' => '\"',
36
- '\\' => '\\\\',
37
- } # :nodoc:
38
-
39
- # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
40
- # UTF16 big endian characters as \u????, and return it.
41
- if defined?(::Encoding)
42
- def utf8_to_json(string) # :nodoc:
43
- string = string.dup
44
- string.force_encoding(::Encoding::ASCII_8BIT)
45
- string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
46
- string.force_encoding(::Encoding::UTF_8)
47
- string
48
- end
49
-
50
- def utf8_to_json_ascii(string) # :nodoc:
51
- string = string.dup
52
- string.force_encoding(::Encoding::ASCII_8BIT)
53
- string.gsub!(/["\\\x0-\x1f]/n) { MAP[$&] }
54
- string.gsub!(/(
55
- (?:
56
- [\xc2-\xdf][\x80-\xbf] |
57
- [\xe0-\xef][\x80-\xbf]{2} |
58
- [\xf0-\xf4][\x80-\xbf]{3}
59
- )+ |
60
- [\x80-\xc1\xf5-\xff] # invalid
61
- )/nx) { |c|
62
- c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
63
- s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
64
- s.force_encoding(::Encoding::ASCII_8BIT)
65
- s.gsub!(/.{4}/n, '\\\\u\&')
66
- s.force_encoding(::Encoding::UTF_8)
67
- }
68
- string.force_encoding(::Encoding::UTF_8)
69
- string
70
- rescue => e
71
- raise GeneratorError.wrap(e)
72
- end
73
-
74
- def valid_utf8?(string)
75
- encoding = string.encoding
76
- (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) &&
77
- string.valid_encoding?
78
- end
79
- module_function :valid_utf8?
80
- else
81
- def utf8_to_json(string) # :nodoc:
82
- string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
83
- end
84
-
85
- def utf8_to_json_ascii(string) # :nodoc:
86
- string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
87
- string.gsub!(/(
88
- (?:
89
- [\xc2-\xdf][\x80-\xbf] |
90
- [\xe0-\xef][\x80-\xbf]{2} |
91
- [\xf0-\xf4][\x80-\xbf]{3}
92
- )+ |
93
- [\x80-\xc1\xf5-\xff] # invalid
94
- )/nx) { |c|
95
- c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
96
- s = JSON.iconv('utf-16be', 'utf-8', c).unpack('H*')[0]
97
- s.gsub!(/.{4}/n, '\\\\u\&')
98
- }
99
- string
100
- rescue => e
101
- raise GeneratorError.wrap(e)
102
- end
103
-
104
- def valid_utf8?(string)
105
- string =~
106
- /\A( [\x09\x0a\x0d\x20-\x7e] # ASCII
107
- | [\xc2-\xdf][\x80-\xbf] # non-overlong 2-byte
108
- | \xe0[\xa0-\xbf][\x80-\xbf] # excluding overlongs
109
- | [\xe1-\xec\xee\xef][\x80-\xbf]{2} # straight 3-byte
110
- | \xed[\x80-\x9f][\x80-\xbf] # excluding surrogates
111
- | \xf0[\x90-\xbf][\x80-\xbf]{2} # planes 1-3
112
- | [\xf1-\xf3][\x80-\xbf]{3} # planes 4-15
113
- | \xf4[\x80-\x8f][\x80-\xbf]{2} # plane 16
114
- )*\z/nx
115
- end
116
- end
117
- module_function :utf8_to_json, :utf8_to_json_ascii, :valid_utf8?
118
-
119
-
120
- module Pure
121
- module Generator
122
- # This class is used to create State instances, that are use to hold data
123
- # while generating a JSON text from a Ruby data structure.
124
- class State
125
- # Creates a State object from _opts_, which ought to be Hash to create
126
- # a new State instance configured by _opts_, something else to create
127
- # an unconfigured instance. If _opts_ is a State object, it is just
128
- # returned.
129
- def self.from_state(opts)
130
- case
131
- when self === opts
132
- opts
133
- when opts.respond_to?(:to_hash)
134
- new(opts.to_hash)
135
- when opts.respond_to?(:to_h)
136
- new(opts.to_h)
137
- else
138
- SAFE_STATE_PROTOTYPE.dup
139
- end
140
- end
141
-
142
- # Instantiates a new State object, configured by _opts_.
143
- #
144
- # _opts_ can have the following keys:
145
- #
146
- # * *indent*: a string used to indent levels (default: ''),
147
- # * *space*: a string that is put after, a : or , delimiter (default: ''),
148
- # * *space_before*: a string that is put before a : pair delimiter (default: ''),
149
- # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
150
- # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
151
- # * *check_circular*: is deprecated now, use the :max_nesting option instead,
152
- # * *max_nesting*: sets the maximum level of data structure nesting in
153
- # the generated JSON, max_nesting = 0 if no maximum should be checked.
154
- # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
155
- # generated, otherwise an exception is thrown, if these values are
156
- # encountered. This options defaults to false.
157
- # * *quirks_mode*: Enables quirks_mode for parser, that is for example
158
- # generating single JSON values instead of documents is possible.
159
- def initialize(opts = {})
160
- @indent = ''
161
- @space = ''
162
- @space_before = ''
163
- @object_nl = ''
164
- @array_nl = ''
165
- @allow_nan = false
166
- @ascii_only = false
167
- @quirks_mode = false
168
- @buffer_initial_length = 1024
169
- configure opts
170
- end
171
-
172
- # This string is used to indent levels in the JSON text.
173
- attr_accessor :indent
174
-
175
- # This string is used to insert a space between the tokens in a JSON
176
- # string.
177
- attr_accessor :space
178
-
179
- # This string is used to insert a space before the ':' in JSON objects.
180
- attr_accessor :space_before
181
-
182
- # This string is put at the end of a line that holds a JSON object (or
183
- # Hash).
184
- attr_accessor :object_nl
185
-
186
- # This string is put at the end of a line that holds a JSON array.
187
- attr_accessor :array_nl
188
-
189
- # This integer returns the maximum level of data structure nesting in
190
- # the generated JSON, max_nesting = 0 if no maximum is checked.
191
- attr_accessor :max_nesting
192
-
193
- # If this attribute is set to true, quirks mode is enabled, otherwise
194
- # it's disabled.
195
- attr_accessor :quirks_mode
196
-
197
- # :stopdoc:
198
- attr_reader :buffer_initial_length
199
-
200
- def buffer_initial_length=(length)
201
- if length > 0
202
- @buffer_initial_length = length
203
- end
204
- end
205
- # :startdoc:
206
-
207
- # This integer returns the current depth data structure nesting in the
208
- # generated JSON.
209
- attr_accessor :depth
210
-
211
- def check_max_nesting # :nodoc:
212
- return if @max_nesting.zero?
213
- current_nesting = depth + 1
214
- current_nesting > @max_nesting and
215
- raise NestingError, "nesting of #{current_nesting} is too deep"
216
- end
217
-
218
- # Returns true, if circular data structures are checked,
219
- # otherwise returns false.
220
- def check_circular?
221
- !@max_nesting.zero?
222
- end
223
-
224
- # Returns true if NaN, Infinity, and -Infinity should be considered as
225
- # valid JSON and output.
226
- def allow_nan?
227
- @allow_nan
228
- end
229
-
230
- # Returns true, if only ASCII characters should be generated. Otherwise
231
- # returns false.
232
- def ascii_only?
233
- @ascii_only
234
- end
235
-
236
- # Returns true, if quirks mode is enabled. Otherwise returns false.
237
- def quirks_mode?
238
- @quirks_mode
239
- end
240
-
241
- # Configure this State instance with the Hash _opts_, and return
242
- # itself.
243
- def configure(opts)
244
- if opts.respond_to?(:to_hash)
245
- opts = opts.to_hash
246
- elsif opts.respond_to?(:to_h)
247
- opts = opts.to_h
248
- else
249
- raise TypeError, "can't convert #{opts.class} into Hash"
250
- end
251
- for key, value in opts
252
- instance_variable_set "@#{key}", value
253
- end
254
- @indent = opts[:indent] if opts.key?(:indent)
255
- @space = opts[:space] if opts.key?(:space)
256
- @space_before = opts[:space_before] if opts.key?(:space_before)
257
- @object_nl = opts[:object_nl] if opts.key?(:object_nl)
258
- @array_nl = opts[:array_nl] if opts.key?(:array_nl)
259
- @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
260
- @ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
261
- @depth = opts[:depth] || 0
262
- @quirks_mode = opts[:quirks_mode] if opts.key?(:quirks_mode)
263
- @buffer_initial_length ||= opts[:buffer_initial_length]
264
-
265
- if !opts.key?(:max_nesting) # defaults to 100
266
- @max_nesting = 100
267
- elsif opts[:max_nesting]
268
- @max_nesting = opts[:max_nesting]
269
- else
270
- @max_nesting = 0
271
- end
272
- self
273
- end
274
- alias merge configure
275
-
276
- # Returns the configuration instance variables as a hash, that can be
277
- # passed to the configure method.
278
- def to_h
279
- result = {}
280
- for iv in instance_variables
281
- iv = iv.to_s[1..-1]
282
- result[iv.to_sym] = self[iv]
283
- end
284
- result
285
- end
286
-
287
- alias to_hash to_h
288
-
289
- # Generates a valid JSON document from object +obj+ and returns the
290
- # result. If no valid JSON document can be created this method raises a
291
- # GeneratorError exception.
292
- def generate(obj)
293
- result = obj.to_json(self)
294
- JSON.valid_utf8?(result) or raise GeneratorError,
295
- "source sequence #{result.inspect} is illegal/malformed utf-8"
296
- unless @quirks_mode
297
- unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
298
- result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
299
- then
300
- raise GeneratorError, "only generation of JSON objects or arrays allowed"
301
- end
302
- end
303
- result
304
- end
305
-
306
- # Return the value returned by method +name+.
307
- def [](name)
308
- if respond_to?(name)
309
- __send__(name)
310
- else
311
- instance_variable_get("@#{name}")
312
- end
313
- end
314
-
315
- def []=(name, value)
316
- if respond_to?(name_writer = "#{name}=")
317
- __send__ name_writer, value
318
- else
319
- instance_variable_set "@#{name}", value
320
- end
321
- end
322
- end
323
-
324
- module GeneratorMethods
325
- module Object
326
- # Converts this object to a string (calling #to_s), converts
327
- # it to a JSON string, and returns the result. This is a fallback, if no
328
- # special method #to_json was defined for some object.
329
- def to_json(*) to_s.to_json end
330
- end
331
-
332
- module Hash
333
- # Returns a JSON string containing a JSON object, that is unparsed from
334
- # this Hash instance.
335
- # _state_ is a JSON::State object, that can also be used to configure the
336
- # produced JSON string output further.
337
- # _depth_ is used to find out nesting depth, to indent accordingly.
338
- def to_json(state = nil, *)
339
- state = State.from_state(state)
340
- state.check_max_nesting
341
- json_transform(state)
342
- end
343
-
344
- private
345
-
346
- def json_shift(state)
347
- state.object_nl.empty? or return ''
348
- state.indent * state.depth
349
- end
350
-
351
- def json_transform(state)
352
- delim = ','
353
- delim << state.object_nl
354
- result = '{'
355
- result << state.object_nl
356
- depth = state.depth += 1
357
- first = true
358
- indent = !state.object_nl.empty?
359
- each { |key,value|
360
- result << delim unless first
361
- result << state.indent * depth if indent
362
- result << key.to_s.to_json(state)
363
- result << state.space_before
364
- result << ':'
365
- result << state.space
366
- if value.respond_to?(:to_json)
367
- result << value.to_json(state)
368
- else
369
- result << %{"#{String(value)}"}
370
- end
371
- first = false
372
- }
373
- depth = state.depth -= 1
374
- result << state.object_nl
375
- result << state.indent * depth if indent
376
- result << '}'
377
- result
378
- end
379
- end
380
-
381
- module Array
382
- # Returns a JSON string containing a JSON array, that is unparsed from
383
- # this Array instance.
384
- # _state_ is a JSON::State object, that can also be used to configure the
385
- # produced JSON string output further.
386
- def to_json(state = nil, *)
387
- state = State.from_state(state)
388
- state.check_max_nesting
389
- json_transform(state)
390
- end
391
-
392
- private
393
-
394
- def json_transform(state)
395
- delim = ','
396
- delim << state.array_nl
397
- result = '['
398
- result << state.array_nl
399
- depth = state.depth += 1
400
- first = true
401
- indent = !state.array_nl.empty?
402
- each { |value|
403
- result << delim unless first
404
- result << state.indent * depth if indent
405
- if value.respond_to?(:to_json)
406
- result << value.to_json(state)
407
- else
408
- result << %{"#{String(value)}"}
409
- end
410
- first = false
411
- }
412
- depth = state.depth -= 1
413
- result << state.array_nl
414
- result << state.indent * depth if indent
415
- result << ']'
416
- end
417
- end
418
-
419
- module Integer
420
- # Returns a JSON string representation for this Integer number.
421
- def to_json(*) to_s end
422
- end
423
-
424
- module Float
425
- # Returns a JSON string representation for this Float number.
426
- def to_json(state = nil, *)
427
- state = State.from_state(state)
428
- case
429
- when infinite?
430
- if state.allow_nan?
431
- to_s
432
- else
433
- raise GeneratorError, "#{self} not allowed in JSON"
434
- end
435
- when nan?
436
- if state.allow_nan?
437
- to_s
438
- else
439
- raise GeneratorError, "#{self} not allowed in JSON"
440
- end
441
- else
442
- to_s
443
- end
444
- end
445
- end
446
-
447
- module String
448
- if defined?(::Encoding)
449
- # This string should be encoded with UTF-8 A call to this method
450
- # returns a JSON string encoded with UTF16 big endian characters as
451
- # \u????.
452
- def to_json(state = nil, *args)
453
- state = State.from_state(state)
454
- if encoding == ::Encoding::UTF_8
455
- string = self
456
- else
457
- string = encode(::Encoding::UTF_8)
458
- end
459
- if state.ascii_only?
460
- '"' << JSON.utf8_to_json_ascii(string) << '"'
461
- else
462
- '"' << JSON.utf8_to_json(string) << '"'
463
- end
464
- end
465
- else
466
- # This string should be encoded with UTF-8 A call to this method
467
- # returns a JSON string encoded with UTF16 big endian characters as
468
- # \u????.
469
- def to_json(state = nil, *args)
470
- state = State.from_state(state)
471
- if state.ascii_only?
472
- '"' << JSON.utf8_to_json_ascii(self) << '"'
473
- else
474
- '"' << JSON.utf8_to_json(self) << '"'
475
- end
476
- end
477
- end
478
-
479
- # Module that holds the extinding methods if, the String module is
480
- # included.
481
- module Extend
482
- # Raw Strings are JSON Objects (the raw bytes are stored in an
483
- # array for the key "raw"). The Ruby String can be created by this
484
- # module method.
485
- def json_create(o)
486
- o['raw'].pack('C*')
487
- end
488
- end
489
-
490
- # Extends _modul_ with the String::Extend module.
491
- def self.included(modul)
492
- modul.extend Extend
493
- end
494
-
495
- # This method creates a raw object hash, that can be nested into
496
- # other data structures and will be unparsed as a raw string. This
497
- # method should be used, if you want to convert raw strings to JSON
498
- # instead of UTF-8 strings, e. g. binary data.
499
- def to_json_raw_object
500
- {
501
- JSON.create_id => self.class.name,
502
- 'raw' => self.unpack('C*'),
503
- }
504
- end
505
-
506
- # This method creates a JSON text from the result of
507
- # a call to to_json_raw_object of this String.
508
- def to_json_raw(*args)
509
- to_json_raw_object.to_json(*args)
510
- end
511
- end
512
-
513
- module TrueClass
514
- # Returns a JSON string for true: 'true'.
515
- def to_json(*) 'true' end
516
- end
517
-
518
- module FalseClass
519
- # Returns a JSON string for false: 'false'.
520
- def to_json(*) 'false' end
521
- end
522
-
523
- module NilClass
524
- # Returns a JSON string for nil: 'null'.
525
- def to_json(*) 'null' end
526
- end
527
- end
528
- end
529
- end
530
- end