es-scout 5.3.0.es1

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