es-scout 5.3.0.es1
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +4 -0
- data/CHANGELOG +225 -0
- data/COPYING +340 -0
- data/INSTALL +18 -0
- data/LICENSE +6 -0
- data/README +66 -0
- data/Rakefile +74 -0
- data/TODO +6 -0
- data/bin/es-scout +10 -0
- data/data/cacert.pem +3154 -0
- data/data/code_key.pub +13 -0
- data/data/gpl-2.0.txt +339 -0
- data/data/lgpl-2.1.txt +504 -0
- data/lib/es-scout/command/install.rb +68 -0
- data/lib/es-scout/command/run.rb +56 -0
- data/lib/es-scout/command/test.rb +62 -0
- data/lib/es-scout/command/troubleshoot.rb +142 -0
- data/lib/es-scout/command.rb +258 -0
- data/lib/es-scout/plugin.rb +237 -0
- data/lib/es-scout/plugin_options.rb +80 -0
- data/lib/es-scout/scout_logger.rb +19 -0
- data/lib/es-scout/server.rb +578 -0
- data/lib/es-scout.rb +11 -0
- data/vendor/json_pure/CHANGES +162 -0
- data/vendor/json_pure/COPYING +58 -0
- data/vendor/json_pure/GPL +340 -0
- data/vendor/json_pure/README +358 -0
- data/vendor/json_pure/Rakefile +292 -0
- data/vendor/json_pure/TODO +1 -0
- data/vendor/json_pure/VERSION +1 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
- data/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
- data/vendor/json_pure/benchmarks/generator2_benchmark.rb +222 -0
- data/vendor/json_pure/benchmarks/generator_benchmark.rb +224 -0
- data/vendor/json_pure/benchmarks/ohai.json +1216 -0
- data/vendor/json_pure/benchmarks/ohai.ruby +1 -0
- data/vendor/json_pure/benchmarks/parser2_benchmark.rb +251 -0
- data/vendor/json_pure/benchmarks/parser_benchmark.rb +259 -0
- data/vendor/json_pure/bin/edit_json.rb +9 -0
- data/vendor/json_pure/bin/prettify_json.rb +75 -0
- data/vendor/json_pure/data/example.json +1 -0
- data/vendor/json_pure/data/index.html +38 -0
- data/vendor/json_pure/data/prototype.js +4184 -0
- data/vendor/json_pure/ext/json/ext/generator/extconf.rb +16 -0
- data/vendor/json_pure/ext/json/ext/generator/generator.c +1341 -0
- data/vendor/json_pure/ext/json/ext/generator/generator.h +170 -0
- data/vendor/json_pure/ext/json/ext/parser/extconf.rb +15 -0
- data/vendor/json_pure/ext/json/ext/parser/parser.c +1935 -0
- data/vendor/json_pure/ext/json/ext/parser/parser.h +71 -0
- data/vendor/json_pure/ext/json/ext/parser/parser.rl +792 -0
- data/vendor/json_pure/install.rb +26 -0
- data/vendor/json_pure/lib/json/Array.xpm +21 -0
- data/vendor/json_pure/lib/json/FalseClass.xpm +21 -0
- data/vendor/json_pure/lib/json/Hash.xpm +21 -0
- data/vendor/json_pure/lib/json/Key.xpm +73 -0
- data/vendor/json_pure/lib/json/NilClass.xpm +21 -0
- data/vendor/json_pure/lib/json/Numeric.xpm +28 -0
- data/vendor/json_pure/lib/json/String.xpm +96 -0
- data/vendor/json_pure/lib/json/TrueClass.xpm +21 -0
- data/vendor/json_pure/lib/json/add/core.rb +148 -0
- data/vendor/json_pure/lib/json/add/rails.rb +58 -0
- data/vendor/json_pure/lib/json/common.rb +397 -0
- data/vendor/json_pure/lib/json/editor.rb +1371 -0
- data/vendor/json_pure/lib/json/ext.rb +15 -0
- data/vendor/json_pure/lib/json/json.xpm +1499 -0
- data/vendor/json_pure/lib/json/pure/generator.rb +452 -0
- data/vendor/json_pure/lib/json/pure/parser.rb +307 -0
- data/vendor/json_pure/lib/json/pure.rb +77 -0
- data/vendor/json_pure/lib/json/version.rb +8 -0
- data/vendor/json_pure/lib/json.rb +10 -0
- data/vendor/json_pure/tests/fixtures/fail1.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail10.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail11.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail12.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail13.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail14.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail18.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail19.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail2.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail20.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail21.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail22.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail23.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail24.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail25.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail27.json +2 -0
- data/vendor/json_pure/tests/fixtures/fail28.json +2 -0
- data/vendor/json_pure/tests/fixtures/fail3.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail4.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail5.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail6.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail7.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail8.json +1 -0
- data/vendor/json_pure/tests/fixtures/fail9.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass1.json +56 -0
- data/vendor/json_pure/tests/fixtures/pass15.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass16.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass17.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass2.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass26.json +1 -0
- data/vendor/json_pure/tests/fixtures/pass3.json +6 -0
- data/vendor/json_pure/tests/test_json.rb +340 -0
- data/vendor/json_pure/tests/test_json_addition.rb +162 -0
- data/vendor/json_pure/tests/test_json_encoding.rb +68 -0
- data/vendor/json_pure/tests/test_json_fixtures.rb +34 -0
- data/vendor/json_pure/tests/test_json_generate.rb +122 -0
- data/vendor/json_pure/tests/test_json_rails.rb +144 -0
- data/vendor/json_pure/tests/test_json_unicode.rb +76 -0
- data/vendor/json_pure/tools/fuzz.rb +139 -0
- data/vendor/json_pure/tools/server.rb +61 -0
- 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
|