json 1.1.5-x86-linux

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 (117) hide show
  1. data/CHANGES +106 -0
  2. data/GPL +340 -0
  3. data/README +78 -0
  4. data/RUBY +58 -0
  5. data/Rakefile +268 -0
  6. data/TODO +1 -0
  7. data/VERSION +1 -0
  8. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  9. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  10. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  11. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  12. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  13. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  14. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  15. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  16. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  17. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  18. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  19. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  20. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  21. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  22. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  23. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  24. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  25. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  26. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  27. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  28. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  29. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  30. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  31. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  32. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  33. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  34. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  35. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  36. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  37. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  38. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  39. data/benchmarks/generator_benchmark.rb +165 -0
  40. data/benchmarks/parser_benchmark.rb +197 -0
  41. data/bin/edit_json.rb +9 -0
  42. data/bin/prettify_json.rb +75 -0
  43. data/data/example.json +1 -0
  44. data/data/index.html +38 -0
  45. data/data/prototype.js +4184 -0
  46. data/doc-templates/main.txt +283 -0
  47. data/ext/json/ext/generator/extconf.rb +11 -0
  48. data/ext/json/ext/generator/generator.c +919 -0
  49. data/ext/json/ext/generator/unicode.c +182 -0
  50. data/ext/json/ext/generator/unicode.h +53 -0
  51. data/ext/json/ext/parser/extconf.rb +11 -0
  52. data/ext/json/ext/parser/parser.c +1829 -0
  53. data/ext/json/ext/parser/parser.rl +686 -0
  54. data/ext/json/ext/parser/unicode.c +154 -0
  55. data/ext/json/ext/parser/unicode.h +58 -0
  56. data/install.rb +26 -0
  57. data/lib/json.rb +10 -0
  58. data/lib/json/Array.xpm +21 -0
  59. data/lib/json/FalseClass.xpm +21 -0
  60. data/lib/json/Hash.xpm +21 -0
  61. data/lib/json/Key.xpm +73 -0
  62. data/lib/json/NilClass.xpm +21 -0
  63. data/lib/json/Numeric.xpm +28 -0
  64. data/lib/json/String.xpm +96 -0
  65. data/lib/json/TrueClass.xpm +21 -0
  66. data/lib/json/add/core.rb +135 -0
  67. data/lib/json/add/rails.rb +58 -0
  68. data/lib/json/common.rb +354 -0
  69. data/lib/json/editor.rb +1371 -0
  70. data/lib/json/ext.rb +15 -0
  71. data/lib/json/ext/generator.so +0 -0
  72. data/lib/json/ext/parser.so +0 -0
  73. data/lib/json/json.xpm +1499 -0
  74. data/lib/json/pure.rb +77 -0
  75. data/lib/json/pure/generator.rb +430 -0
  76. data/lib/json/pure/parser.rb +267 -0
  77. data/lib/json/version.rb +8 -0
  78. data/tests/fixtures/fail1.json +1 -0
  79. data/tests/fixtures/fail10.json +1 -0
  80. data/tests/fixtures/fail11.json +1 -0
  81. data/tests/fixtures/fail12.json +1 -0
  82. data/tests/fixtures/fail13.json +1 -0
  83. data/tests/fixtures/fail14.json +1 -0
  84. data/tests/fixtures/fail18.json +1 -0
  85. data/tests/fixtures/fail19.json +1 -0
  86. data/tests/fixtures/fail2.json +1 -0
  87. data/tests/fixtures/fail20.json +1 -0
  88. data/tests/fixtures/fail21.json +1 -0
  89. data/tests/fixtures/fail22.json +1 -0
  90. data/tests/fixtures/fail23.json +1 -0
  91. data/tests/fixtures/fail24.json +1 -0
  92. data/tests/fixtures/fail25.json +1 -0
  93. data/tests/fixtures/fail27.json +2 -0
  94. data/tests/fixtures/fail28.json +2 -0
  95. data/tests/fixtures/fail3.json +1 -0
  96. data/tests/fixtures/fail4.json +1 -0
  97. data/tests/fixtures/fail5.json +1 -0
  98. data/tests/fixtures/fail6.json +1 -0
  99. data/tests/fixtures/fail7.json +1 -0
  100. data/tests/fixtures/fail8.json +1 -0
  101. data/tests/fixtures/fail9.json +1 -0
  102. data/tests/fixtures/pass1.json +56 -0
  103. data/tests/fixtures/pass15.json +1 -0
  104. data/tests/fixtures/pass16.json +1 -0
  105. data/tests/fixtures/pass17.json +1 -0
  106. data/tests/fixtures/pass2.json +1 -0
  107. data/tests/fixtures/pass26.json +1 -0
  108. data/tests/fixtures/pass3.json +6 -0
  109. data/tests/test_json.rb +312 -0
  110. data/tests/test_json_addition.rb +164 -0
  111. data/tests/test_json_fixtures.rb +34 -0
  112. data/tests/test_json_generate.rb +106 -0
  113. data/tests/test_json_rails.rb +146 -0
  114. data/tests/test_json_unicode.rb +62 -0
  115. data/tools/fuzz.rb +139 -0
  116. data/tools/server.rb +61 -0
  117. metadata +200 -0
@@ -0,0 +1,77 @@
1
+ require 'json/common'
2
+ require 'json/pure/parser'
3
+ require 'json/pure/generator'
4
+
5
+ module JSON
6
+ begin
7
+ require 'iconv'
8
+ # An iconv instance to convert from UTF8 to UTF16 Big Endian.
9
+ UTF16toUTF8 = Iconv.new('utf-8', 'utf-16be') # :nodoc:
10
+ # An iconv instance to convert from UTF16 Big Endian to UTF8.
11
+ UTF8toUTF16 = Iconv.new('utf-16be', 'utf-8') # :nodoc:
12
+ UTF8toUTF16.iconv('no bom')
13
+ rescue Errno::EINVAL, Iconv::InvalidEncoding
14
+ # Iconv doesn't support big endian utf-16. Let's try to hack this manually
15
+ # into the converters.
16
+ begin
17
+ old_verbose, $VERBSOSE = $VERBOSE, nil
18
+ # An iconv instance to convert from UTF8 to UTF16 Big Endian.
19
+ UTF16toUTF8 = Iconv.new('utf-8', 'utf-16') # :nodoc:
20
+ # An iconv instance to convert from UTF16 Big Endian to UTF8.
21
+ UTF8toUTF16 = Iconv.new('utf-16', 'utf-8') # :nodoc:
22
+ UTF8toUTF16.iconv('no bom')
23
+ if UTF8toUTF16.iconv("\xe2\x82\xac") == "\xac\x20"
24
+ swapper = Class.new do
25
+ def initialize(iconv) # :nodoc:
26
+ @iconv = iconv
27
+ end
28
+
29
+ def iconv(string) # :nodoc:
30
+ result = @iconv.iconv(string)
31
+ JSON.swap!(result)
32
+ end
33
+ end
34
+ UTF8toUTF16 = swapper.new(UTF8toUTF16) # :nodoc:
35
+ end
36
+ if UTF16toUTF8.iconv("\xac\x20") == "\xe2\x82\xac"
37
+ swapper = Class.new do
38
+ def initialize(iconv) # :nodoc:
39
+ @iconv = iconv
40
+ end
41
+
42
+ def iconv(string) # :nodoc:
43
+ string = JSON.swap!(string.dup)
44
+ @iconv.iconv(string)
45
+ end
46
+ end
47
+ UTF16toUTF8 = swapper.new(UTF16toUTF8) # :nodoc:
48
+ end
49
+ rescue Errno::EINVAL, Iconv::InvalidEncoding
50
+ raise MissingUnicodeSupport, "iconv doesn't seem to support UTF-8/UTF-16 conversions"
51
+ ensure
52
+ $VERBOSE = old_verbose
53
+ end
54
+ rescue LoadError
55
+ raise MissingUnicodeSupport,
56
+ "iconv couldn't be loaded, which is required for UTF-8/UTF-16 conversions"
57
+ end
58
+
59
+ # Swap consecutive bytes of _string_ in place.
60
+ def self.swap!(string) # :nodoc:
61
+ 0.upto(string.size / 2) do |i|
62
+ break unless string[2 * i + 1]
63
+ string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
64
+ end
65
+ string
66
+ end
67
+
68
+ # This module holds all the modules/classes that implement JSON's
69
+ # functionality in pure ruby.
70
+ module Pure
71
+ $DEBUG and warn "Using pure library for JSON."
72
+ JSON.parser = Parser
73
+ JSON.generator = Generator
74
+ end
75
+
76
+ JSON_LOADED = true
77
+ end
@@ -0,0 +1,430 @@
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
+ '/' => '\/',
38
+ } # :nodoc:
39
+
40
+ # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
41
+ # UTF16 big endian characters as \u????, and return it.
42
+ if String.method_defined?(:force_encoding)
43
+ def utf8_to_json(string) # :nodoc:
44
+ string = string.dup
45
+ string << '' # XXX workaround: avoid buffer sharing
46
+ string.force_encoding(Encoding::ASCII_8BIT)
47
+ string.gsub!(/["\\\/\x0-\x1f]/) { MAP[$&] }
48
+ string.gsub!(/(
49
+ (?:
50
+ [\xc2-\xdf][\x80-\xbf] |
51
+ [\xe0-\xef][\x80-\xbf]{2} |
52
+ [\xf0-\xf4][\x80-\xbf]{3}
53
+ )+ |
54
+ [\x80-\xc1\xf5-\xff] # invalid
55
+ )/nx) { |c|
56
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
57
+ s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
58
+ s.gsub!(/.{4}/n, '\\\\u\&')
59
+ }
60
+ string.force_encoding(Encoding::UTF_8)
61
+ string
62
+ rescue Iconv::Failure => e
63
+ raise GeneratorError, "Caught #{e.class}: #{e}"
64
+ end
65
+ else
66
+ def utf8_to_json(string) # :nodoc:
67
+ string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
68
+ string.gsub!(/(
69
+ (?:
70
+ [\xc2-\xdf][\x80-\xbf] |
71
+ [\xe0-\xef][\x80-\xbf]{2} |
72
+ [\xf0-\xf4][\x80-\xbf]{3}
73
+ )+ |
74
+ [\x80-\xc1\xf5-\xff] # invalid
75
+ )/nx) { |c|
76
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
77
+ s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
78
+ s.gsub!(/.{4}/n, '\\\\u\&')
79
+ }
80
+ string
81
+ rescue Iconv::Failure => e
82
+ raise GeneratorError, "Caught #{e.class}: #{e}"
83
+ end
84
+ end
85
+ module_function :utf8_to_json
86
+
87
+ module Pure
88
+ module Generator
89
+ # This class is used to create State instances, that are use to hold data
90
+ # while generating a JSON text from a a Ruby data structure.
91
+ class State
92
+ # Creates a State object from _opts_, which ought to be Hash to create
93
+ # a new State instance configured by _opts_, something else to create
94
+ # an unconfigured instance. If _opts_ is a State object, it is just
95
+ # returned.
96
+ def self.from_state(opts)
97
+ case opts
98
+ when self
99
+ opts
100
+ when Hash
101
+ new(opts)
102
+ else
103
+ new
104
+ end
105
+ end
106
+
107
+ # Instantiates a new State object, configured by _opts_.
108
+ #
109
+ # _opts_ can have the following keys:
110
+ #
111
+ # * *indent*: a string used to indent levels (default: ''),
112
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
113
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
114
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
115
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
116
+ # * *check_circular*: true if checking for circular data structures
117
+ # should be done (the default), false otherwise.
118
+ # * *check_circular*: true if checking for circular data structures
119
+ # should be done, false (the default) otherwise.
120
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
121
+ # generated, otherwise an exception is thrown, if these values are
122
+ # encountered. This options defaults to false.
123
+ def initialize(opts = {})
124
+ @seen = {}
125
+ @indent = ''
126
+ @space = ''
127
+ @space_before = ''
128
+ @object_nl = ''
129
+ @array_nl = ''
130
+ @check_circular = true
131
+ @allow_nan = false
132
+ configure opts
133
+ end
134
+
135
+ # This string is used to indent levels in the JSON text.
136
+ attr_accessor :indent
137
+
138
+ # This string is used to insert a space between the tokens in a JSON
139
+ # string.
140
+ attr_accessor :space
141
+
142
+ # This string is used to insert a space before the ':' in JSON objects.
143
+ attr_accessor :space_before
144
+
145
+ # This string is put at the end of a line that holds a JSON object (or
146
+ # Hash).
147
+ attr_accessor :object_nl
148
+
149
+ # This string is put at the end of a line that holds a JSON array.
150
+ attr_accessor :array_nl
151
+
152
+ # This integer returns the maximum level of data structure nesting in
153
+ # the generated JSON, max_nesting = 0 if no maximum is checked.
154
+ attr_accessor :max_nesting
155
+
156
+ def check_max_nesting(depth) # :nodoc:
157
+ return if @max_nesting.zero?
158
+ current_nesting = depth + 1
159
+ current_nesting > @max_nesting and
160
+ raise NestingError, "nesting of #{current_nesting} is too deep"
161
+ end
162
+
163
+ # Returns true, if circular data structures should be checked,
164
+ # otherwise returns false.
165
+ def check_circular?
166
+ @check_circular
167
+ end
168
+
169
+ # Returns true if NaN, Infinity, and -Infinity should be considered as
170
+ # valid JSON and output.
171
+ def allow_nan?
172
+ @allow_nan
173
+ end
174
+
175
+ # Returns _true_, if _object_ was already seen during this generating
176
+ # run.
177
+ def seen?(object)
178
+ @seen.key?(object.__id__)
179
+ end
180
+
181
+ # Remember _object_, to find out if it was already encountered (if a
182
+ # cyclic data structure is if a cyclic data structure is rendered).
183
+ def remember(object)
184
+ @seen[object.__id__] = true
185
+ end
186
+
187
+ # Forget _object_ for this generating run.
188
+ def forget(object)
189
+ @seen.delete object.__id__
190
+ end
191
+
192
+ # Configure this State instance with the Hash _opts_, and return
193
+ # itself.
194
+ def configure(opts)
195
+ @indent = opts[:indent] if opts.key?(:indent)
196
+ @space = opts[:space] if opts.key?(:space)
197
+ @space_before = opts[:space_before] if opts.key?(:space_before)
198
+ @object_nl = opts[:object_nl] if opts.key?(:object_nl)
199
+ @array_nl = opts[:array_nl] if opts.key?(:array_nl)
200
+ @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
201
+ @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
202
+ if !opts.key?(:max_nesting) # defaults to 19
203
+ @max_nesting = 19
204
+ elsif opts[:max_nesting]
205
+ @max_nesting = opts[:max_nesting]
206
+ else
207
+ @max_nesting = 0
208
+ end
209
+ self
210
+ end
211
+
212
+ # Returns the configuration instance variables as a hash, that can be
213
+ # passed to the configure method.
214
+ def to_h
215
+ result = {}
216
+ for iv in %w[indent space space_before object_nl array_nl check_circular allow_nan max_nesting]
217
+ result[iv.intern] = instance_variable_get("@#{iv}")
218
+ end
219
+ result
220
+ end
221
+ end
222
+
223
+ module GeneratorMethods
224
+ module Object
225
+ # Converts this object to a string (calling #to_s), converts
226
+ # it to a JSON string, and returns the result. This is a fallback, if no
227
+ # special method #to_json was defined for some object.
228
+ def to_json(*) to_s.to_json end
229
+ end
230
+
231
+ module Hash
232
+ # Returns a JSON string containing a JSON object, that is unparsed from
233
+ # this Hash instance.
234
+ # _state_ is a JSON::State object, that can also be used to configure the
235
+ # produced JSON string output further.
236
+ # _depth_ is used to find out nesting depth, to indent accordingly.
237
+ def to_json(state = nil, depth = 0, *)
238
+ if state
239
+ state = JSON.state.from_state(state)
240
+ state.check_max_nesting(depth)
241
+ json_check_circular(state) { json_transform(state, depth) }
242
+ else
243
+ json_transform(state, depth)
244
+ end
245
+ end
246
+
247
+ private
248
+
249
+ def json_check_circular(state)
250
+ if state and state.check_circular?
251
+ state.seen?(self) and raise JSON::CircularDatastructure,
252
+ "circular data structures not supported!"
253
+ state.remember self
254
+ end
255
+ yield
256
+ ensure
257
+ state and state.forget self
258
+ end
259
+
260
+ def json_shift(state, depth)
261
+ state and not state.object_nl.empty? or return ''
262
+ state.indent * depth
263
+ end
264
+
265
+ def json_transform(state, depth)
266
+ delim = ','
267
+ if state
268
+ delim << state.object_nl
269
+ result = '{'
270
+ result << state.object_nl
271
+ result << map { |key,value|
272
+ s = json_shift(state, depth + 1)
273
+ s << key.to_s.to_json(state, depth + 1)
274
+ s << state.space_before
275
+ s << ':'
276
+ s << state.space
277
+ s << value.to_json(state, depth + 1)
278
+ }.join(delim)
279
+ result << state.object_nl
280
+ result << json_shift(state, depth)
281
+ result << '}'
282
+ else
283
+ result = '{'
284
+ result << map { |key,value|
285
+ key.to_s.to_json << ':' << value.to_json
286
+ }.join(delim)
287
+ result << '}'
288
+ end
289
+ result
290
+ end
291
+ end
292
+
293
+ module Array
294
+ # Returns a JSON string containing a JSON array, that is unparsed from
295
+ # this Array instance.
296
+ # _state_ is a JSON::State object, that can also be used to configure the
297
+ # produced JSON string output further.
298
+ # _depth_ is used to find out nesting depth, to indent accordingly.
299
+ def to_json(state = nil, depth = 0, *)
300
+ if state
301
+ state = JSON.state.from_state(state)
302
+ state.check_max_nesting(depth)
303
+ json_check_circular(state) { json_transform(state, depth) }
304
+ else
305
+ json_transform(state, depth)
306
+ end
307
+ end
308
+
309
+ private
310
+
311
+ def json_check_circular(state)
312
+ if state and state.check_circular?
313
+ state.seen?(self) and raise JSON::CircularDatastructure,
314
+ "circular data structures not supported!"
315
+ state.remember self
316
+ end
317
+ yield
318
+ ensure
319
+ state and state.forget self
320
+ end
321
+
322
+ def json_shift(state, depth)
323
+ state and not state.array_nl.empty? or return ''
324
+ state.indent * depth
325
+ end
326
+
327
+ def json_transform(state, depth)
328
+ delim = ','
329
+ if state
330
+ delim << state.array_nl
331
+ result = '['
332
+ result << state.array_nl
333
+ result << map { |value|
334
+ json_shift(state, depth + 1) << value.to_json(state, depth + 1)
335
+ }.join(delim)
336
+ result << state.array_nl
337
+ result << json_shift(state, depth)
338
+ result << ']'
339
+ else
340
+ '[' << map { |value| value.to_json }.join(delim) << ']'
341
+ end
342
+ end
343
+ end
344
+
345
+ module Integer
346
+ # Returns a JSON string representation for this Integer number.
347
+ def to_json(*) to_s end
348
+ end
349
+
350
+ module Float
351
+ # Returns a JSON string representation for this Float number.
352
+ def to_json(state = nil, *)
353
+ case
354
+ when infinite?
355
+ if !state || state.allow_nan?
356
+ to_s
357
+ else
358
+ raise GeneratorError, "#{self} not allowed in JSON"
359
+ end
360
+ when nan?
361
+ if !state || state.allow_nan?
362
+ to_s
363
+ else
364
+ raise GeneratorError, "#{self} not allowed in JSON"
365
+ end
366
+ else
367
+ to_s
368
+ end
369
+ end
370
+ end
371
+
372
+ 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) << '"'
378
+ end
379
+
380
+ # Module that holds the extinding methods if, the String module is
381
+ # included.
382
+ module Extend
383
+ # Raw Strings are JSON Objects (the raw bytes are stored in an array for the
384
+ # key "raw"). The Ruby String can be created by this module method.
385
+ def json_create(o)
386
+ o['raw'].pack('C*')
387
+ end
388
+ end
389
+
390
+ # Extends _modul_ with the String::Extend module.
391
+ def self.included(modul)
392
+ modul.extend Extend
393
+ end
394
+
395
+ # This method creates a raw object hash, that can be nested into
396
+ # other data structures and will be unparsed as a raw string. This
397
+ # method should be used, if you want to convert raw strings to JSON
398
+ # instead of UTF-8 strings, e. g. binary data.
399
+ def to_json_raw_object
400
+ {
401
+ JSON.create_id => self.class.name,
402
+ 'raw' => self.unpack('C*'),
403
+ }
404
+ end
405
+
406
+ # This method creates a JSON text from the result of
407
+ # a call to to_json_raw_object of this String.
408
+ def to_json_raw(*args)
409
+ to_json_raw_object.to_json(*args)
410
+ end
411
+ end
412
+
413
+ module TrueClass
414
+ # Returns a JSON string for true: 'true'.
415
+ def to_json(*) 'true' end
416
+ end
417
+
418
+ module FalseClass
419
+ # Returns a JSON string for false: 'false'.
420
+ def to_json(*) 'false' end
421
+ end
422
+
423
+ module NilClass
424
+ # Returns a JSON string for nil: 'null'.
425
+ def to_json(*) 'null' end
426
+ end
427
+ end
428
+ end
429
+ end
430
+ end