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,397 @@
1
+ require 'json/version'
2
+ require 'iconv'
3
+
4
+ module JSON
5
+ class << self
6
+ # If _object_ is string-like parse the string and return the parsed result
7
+ # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
8
+ # data structure object and return it.
9
+ #
10
+ # The _opts_ argument is passed through to generate/parse respectively, see
11
+ # generate and parse for their documentation.
12
+ def [](object, opts = {})
13
+ if object.respond_to? :to_str
14
+ JSON.parse(object.to_str, opts => {})
15
+ else
16
+ JSON.generate(object, opts => {})
17
+ end
18
+ end
19
+
20
+ # Returns the JSON parser class, that is used by JSON. This might be either
21
+ # JSON::Ext::Parser or JSON::Pure::Parser.
22
+ attr_reader :parser
23
+
24
+ # Set the JSON parser class _parser_ to be used by JSON.
25
+ def parser=(parser) # :nodoc:
26
+ @parser = parser
27
+ remove_const :Parser if const_defined? :Parser
28
+ const_set :Parser, parser
29
+ end
30
+
31
+ # Return the constant located at _path_. The format of _path_ has to be
32
+ # either ::A::B::C or A::B::C. In any case A has to be located at the top
33
+ # level (absolute namespace path?). If there doesn't exist a constant at
34
+ # the given path, an ArgumentError is raised.
35
+ def deep_const_get(path) # :nodoc:
36
+ path.to_s.split(/::/).inject(Object) do |p, c|
37
+ case
38
+ when c.empty? then p
39
+ when p.const_defined?(c) then p.const_get(c)
40
+ else
41
+ begin
42
+ p.const_missing(c)
43
+ rescue NameError
44
+ raise ArgumentError, "can't find const #{path}"
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ # Set the module _generator_ to be used by JSON.
51
+ def generator=(generator) # :nodoc:
52
+ @generator = generator
53
+ generator_methods = generator::GeneratorMethods
54
+ for const in generator_methods.constants
55
+ klass = deep_const_get(const)
56
+ modul = generator_methods.const_get(const)
57
+ klass.class_eval do
58
+ instance_methods(false).each do |m|
59
+ m.to_s == 'to_json' and remove_method m
60
+ end
61
+ include modul
62
+ end
63
+ end
64
+ self.state = generator::State
65
+ const_set :State, self.state
66
+ const_set :SAFE_STATE_PROTOTYPE, State.new.freeze
67
+ const_set :FAST_STATE_PROTOTYPE, State.new(
68
+ :indent => '',
69
+ :space => '',
70
+ :object_nl => "",
71
+ :array_nl => "",
72
+ :max_nesting => false
73
+ ).freeze
74
+ const_set :PRETTY_STATE_PROTOTYPE, State.new(
75
+ :indent => ' ',
76
+ :space => ' ',
77
+ :object_nl => "\n",
78
+ :array_nl => "\n"
79
+ ).freeze
80
+ end
81
+
82
+ # Returns the JSON generator modul, that is used by JSON. This might be
83
+ # either JSON::Ext::Generator or JSON::Pure::Generator.
84
+ attr_reader :generator
85
+
86
+ # Returns the JSON generator state class, that is used by JSON. This might
87
+ # be either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
88
+ attr_accessor :state
89
+
90
+ # This is create identifier, that is used to decide, if the _json_create_
91
+ # hook of a class should be called. It defaults to 'json_class'.
92
+ attr_accessor :create_id
93
+ end
94
+ self.create_id = 'json_class'
95
+
96
+ NaN = 0.0/0
97
+
98
+ Infinity = 1.0/0
99
+
100
+ MinusInfinity = -Infinity
101
+
102
+ # The base exception for JSON errors.
103
+ class JSONError < StandardError; end
104
+
105
+ # This exception is raised, if a parser error occurs.
106
+ class ParserError < JSONError; end
107
+
108
+ # This exception is raised, if the nesting of parsed datastructures is too
109
+ # deep.
110
+ class NestingError < ParserError; end
111
+
112
+ # :stopdoc:
113
+ class CircularDatastructure < NestingError; end
114
+ # :startdoc:
115
+
116
+ # This exception is raised, if a generator or unparser error occurs.
117
+ class GeneratorError < JSONError; end
118
+ # For backwards compatibility
119
+ UnparserError = GeneratorError
120
+
121
+ # This exception is raised, if the required unicode support is missing on the
122
+ # system. Usually this means, that the iconv library is not installed.
123
+ class MissingUnicodeSupport < JSONError; end
124
+
125
+ module_function
126
+
127
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
128
+ #
129
+ # _opts_ can have the following
130
+ # keys:
131
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
132
+ # structures. Disable depth checking with :max_nesting => false, it defaults
133
+ # to 19.
134
+ # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
135
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
136
+ # to false.
137
+ # * *symbolize_names*: If set to true, returns symbols for the names
138
+ # (keys) in a JSON object. Otherwise strings are returned, which is also
139
+ # the default.
140
+ # * *create_additions*: If set to false, the Parser doesn't create
141
+ # additions even if a matchin class and create_id was found. This option
142
+ # defaults to true.
143
+ # * *object_class*: Defaults to Hash
144
+ # * *array_class*: Defaults to Array
145
+ def parse(source, opts = {})
146
+ Parser.new(source, opts).parse
147
+ end
148
+
149
+ # Parse the JSON document _source_ into a Ruby data structure and return it.
150
+ # The bang version of the parse method, defaults to the more dangerous values
151
+ # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
152
+ #
153
+ # _opts_ can have the following keys:
154
+ # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
155
+ # structures. Enable depth checking with :max_nesting => anInteger. The parse!
156
+ # methods defaults to not doing max depth checking: This can be dangerous,
157
+ # if someone wants to fill up your stack.
158
+ # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
159
+ # defiance of RFC 4627 to be parsed by the Parser. This option defaults
160
+ # to true.
161
+ # * *create_additions*: If set to false, the Parser doesn't create
162
+ # additions even if a matchin class and create_id was found. This option
163
+ # defaults to true.
164
+ def parse!(source, opts = {})
165
+ opts = {
166
+ :max_nesting => false,
167
+ :allow_nan => true
168
+ }.update(opts)
169
+ Parser.new(source, opts).parse
170
+ end
171
+
172
+ # Generate a JSON document from the Ruby data structure _obj_ and return
173
+ # it. _state_ is * a JSON::State object,
174
+ # * or a Hash like object (responding to to_hash),
175
+ # * an object convertible into a hash by a to_h method,
176
+ # that is used as or to configure a State object.
177
+ #
178
+ # It defaults to a state object, that creates the shortest possible JSON text
179
+ # in one line, checks for circular data structures and doesn't allow NaN,
180
+ # Infinity, and -Infinity.
181
+ #
182
+ # A _state_ hash can have the following keys:
183
+ # * *indent*: a string used to indent levels (default: ''),
184
+ # * *space*: a string that is put after, a : or , delimiter (default: ''),
185
+ # * *space_before*: a string that is put before a : pair delimiter (default: ''),
186
+ # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
187
+ # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
188
+ # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
189
+ # generated, otherwise an exception is thrown, if these values are
190
+ # encountered. This options defaults to false.
191
+ # * *max_nesting*: The maximum depth of nesting allowed in the data
192
+ # structures from which JSON is to be generated. Disable depth checking
193
+ # with :max_nesting => false, it defaults to 19.
194
+ #
195
+ # See also the fast_generate for the fastest creation method with the least
196
+ # amount of sanity checks, and the pretty_generate method for some
197
+ # defaults for a pretty output.
198
+ def generate(obj, opts = nil)
199
+ if opts
200
+ if opts.respond_to? :to_hash
201
+ opts = opts.to_hash
202
+ elsif opts.respond_to? :to_h
203
+ opts = opts.to_h
204
+ else
205
+ raise TypeError, "can't convert #{opts.class} into Hash"
206
+ end
207
+ state = SAFE_STATE_PROTOTYPE.dup
208
+ state = state.configure(opts)
209
+ else
210
+ state = SAFE_STATE_PROTOTYPE
211
+ end
212
+ state.generate(obj)
213
+ end
214
+
215
+ # :stopdoc:
216
+ # I want to deprecate these later, so I'll first be silent about them, and
217
+ # later delete them.
218
+ alias unparse generate
219
+ module_function :unparse
220
+ # :startdoc:
221
+
222
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
223
+ # This method disables the checks for circles in Ruby objects.
224
+ #
225
+ # *WARNING*: Be careful not to pass any Ruby data structures with circles as
226
+ # _obj_ argument, because this will cause JSON to go into an infinite loop.
227
+ def fast_generate(obj, opts = nil)
228
+ if opts
229
+ if opts.respond_to? :to_hash
230
+ opts = opts.to_hash
231
+ elsif opts.respond_to? :to_h
232
+ opts = opts.to_h
233
+ else
234
+ raise TypeError, "can't convert #{opts.class} into Hash"
235
+ end
236
+ state = FAST_STATE_PROTOTYPE.dup
237
+ state.configure(opts)
238
+ else
239
+ state = FAST_STATE_PROTOTYPE
240
+ end
241
+ state.generate(obj)
242
+ end
243
+
244
+ # :stopdoc:
245
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
246
+ alias fast_unparse fast_generate
247
+ module_function :fast_unparse
248
+ # :startdoc:
249
+
250
+ # Generate a JSON document from the Ruby data structure _obj_ and return it.
251
+ # The returned document is a prettier form of the document returned by
252
+ # #unparse.
253
+ #
254
+ # The _opts_ argument can be used to configure the generator, see the
255
+ # generate method for a more detailed explanation.
256
+ def pretty_generate(obj, opts = nil)
257
+ if opts
258
+ if opts.respond_to? :to_hash
259
+ opts = opts.to_hash
260
+ elsif opts.respond_to? :to_h
261
+ opts = opts.to_h
262
+ else
263
+ raise TypeError, "can't convert #{opts.class} into Hash"
264
+ end
265
+ state = PRETTY_STATE_PROTOTYPE.dup
266
+ state.configure(opts)
267
+ else
268
+ state = PRETTY_STATE_PROTOTYPE
269
+ end
270
+ state.generate(obj)
271
+ end
272
+
273
+ # :stopdoc:
274
+ # I want to deprecate these later, so I'll first be silent about them, and later delete them.
275
+ alias pretty_unparse pretty_generate
276
+ module_function :pretty_unparse
277
+ # :startdoc:
278
+
279
+ # Load a ruby data structure from a JSON _source_ and return it. A source can
280
+ # either be a string-like object, an IO like object, or an object responding
281
+ # to the read method. If _proc_ was given, it will be called with any nested
282
+ # Ruby object as an argument recursively in depth first order.
283
+ #
284
+ # This method is part of the implementation of the load/dump interface of
285
+ # Marshal and YAML.
286
+ def load(source, proc = nil)
287
+ if source.respond_to? :to_str
288
+ source = source.to_str
289
+ elsif source.respond_to? :to_io
290
+ source = source.to_io.read
291
+ else
292
+ source = source.read
293
+ end
294
+ result = parse(source, :max_nesting => false, :allow_nan => true)
295
+ recurse_proc(result, &proc) if proc
296
+ result
297
+ end
298
+
299
+ def recurse_proc(result, &proc)
300
+ case result
301
+ when Array
302
+ result.each { |x| recurse_proc x, &proc }
303
+ proc.call result
304
+ when Hash
305
+ result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
306
+ proc.call result
307
+ else
308
+ proc.call result
309
+ end
310
+ end
311
+
312
+ alias restore load
313
+ module_function :restore
314
+
315
+ # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
316
+ # the result.
317
+ #
318
+ # If anIO (an IO like object or an object that responds to the write method)
319
+ # was given, the resulting JSON is written to it.
320
+ #
321
+ # If the number of nested arrays or objects exceeds _limit_ an ArgumentError
322
+ # exception is raised. This argument is similar (but not exactly the
323
+ # same!) to the _limit_ argument in Marshal.dump.
324
+ #
325
+ # This method is part of the implementation of the load/dump interface of
326
+ # Marshal and YAML.
327
+ def dump(obj, anIO = nil, limit = nil)
328
+ if anIO and limit.nil?
329
+ anIO = anIO.to_io if anIO.respond_to?(:to_io)
330
+ unless anIO.respond_to?(:write)
331
+ limit = anIO
332
+ anIO = nil
333
+ end
334
+ end
335
+ limit ||= 0
336
+ result = generate(obj, :allow_nan => true, :max_nesting => limit)
337
+ if anIO
338
+ anIO.write result
339
+ anIO
340
+ else
341
+ result
342
+ end
343
+ rescue JSON::NestingError
344
+ raise ArgumentError, "exceed depth limit"
345
+ end
346
+
347
+ # Shortuct for iconv.
348
+ def self.iconv(to, from, string)
349
+ Iconv.iconv(to, from, string).first
350
+ end
351
+ end
352
+
353
+ module ::Kernel
354
+ private
355
+
356
+ # Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
357
+ # one line.
358
+ def j(*objs)
359
+ objs.each do |obj|
360
+ puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
361
+ end
362
+ nil
363
+ end
364
+
365
+ # Ouputs _objs_ to STDOUT as JSON strings in a pretty format, with
366
+ # indentation and over many lines.
367
+ def jj(*objs)
368
+ objs.each do |obj|
369
+ puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
370
+ end
371
+ nil
372
+ end
373
+
374
+ # If _object_ is string-like parse the string and return the parsed result as
375
+ # a Ruby data structure. Otherwise generate a JSON text from the Ruby data
376
+ # structure object and return it.
377
+ #
378
+ # The _opts_ argument is passed through to generate/parse respectively, see
379
+ # generate and parse for their documentation.
380
+ def JSON(object, opts = {})
381
+ if object.respond_to? :to_str
382
+ JSON.parse(object.to_str, opts)
383
+ else
384
+ JSON.generate(object, opts)
385
+ end
386
+ end
387
+ end
388
+
389
+ class ::Class
390
+ # Returns true, if this class can be used to create an instance
391
+ # from a serialised JSON string. The class has to implement a class
392
+ # method _json_create_ that expects a hash as first parameter, which includes
393
+ # the required data.
394
+ def json_creatable?
395
+ respond_to?(:json_create)
396
+ end
397
+ end