json 2.2.0
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +23 -0
- data/CHANGES.md +391 -0
- data/Gemfile +14 -0
- data/README-json-jruby.md +33 -0
- data/README.md +409 -0
- data/Rakefile +408 -0
- data/VERSION +1 -0
- data/diagrams/.keep +0 -0
- data/ext/json/ext/fbuffer/fbuffer.h +187 -0
- data/ext/json/ext/generator/depend +1 -0
- data/ext/json/ext/generator/extconf.rb +4 -0
- data/ext/json/ext/generator/generator.c +1444 -0
- data/ext/json/ext/generator/generator.h +171 -0
- data/ext/json/ext/parser/depend +1 -0
- data/ext/json/ext/parser/extconf.rb +6 -0
- data/ext/json/ext/parser/parser.c +2131 -0
- data/ext/json/ext/parser/parser.h +91 -0
- data/ext/json/ext/parser/parser.rl +891 -0
- data/ext/json/extconf.rb +2 -0
- data/install.rb +23 -0
- data/java/src/json/ext/ByteListTranscoder.java +166 -0
- data/java/src/json/ext/Generator.java +443 -0
- data/java/src/json/ext/GeneratorMethods.java +231 -0
- data/java/src/json/ext/GeneratorService.java +42 -0
- data/java/src/json/ext/GeneratorState.java +490 -0
- data/java/src/json/ext/OptionsReader.java +113 -0
- data/java/src/json/ext/Parser.java +2362 -0
- data/java/src/json/ext/Parser.rl +893 -0
- data/java/src/json/ext/ParserService.java +34 -0
- data/java/src/json/ext/RuntimeInfo.java +116 -0
- data/java/src/json/ext/StringDecoder.java +166 -0
- data/java/src/json/ext/StringEncoder.java +111 -0
- data/java/src/json/ext/Utils.java +88 -0
- data/json-java.gemspec +38 -0
- data/json.gemspec +0 -0
- data/json_pure.gemspec +38 -0
- data/lib/json.rb +63 -0
- data/lib/json/add/bigdecimal.rb +29 -0
- data/lib/json/add/complex.rb +29 -0
- data/lib/json/add/core.rb +12 -0
- data/lib/json/add/date.rb +34 -0
- data/lib/json/add/date_time.rb +50 -0
- data/lib/json/add/exception.rb +31 -0
- data/lib/json/add/ostruct.rb +31 -0
- data/lib/json/add/range.rb +29 -0
- data/lib/json/add/rational.rb +28 -0
- data/lib/json/add/regexp.rb +30 -0
- data/lib/json/add/set.rb +29 -0
- data/lib/json/add/struct.rb +30 -0
- data/lib/json/add/symbol.rb +25 -0
- data/lib/json/add/time.rb +38 -0
- data/lib/json/common.rb +456 -0
- data/lib/json/ext.rb +15 -0
- data/lib/json/ext/.keep +0 -0
- data/lib/json/generic_object.rb +71 -0
- data/lib/json/pure.rb +15 -0
- data/lib/json/pure/generator.rb +458 -0
- data/lib/json/pure/parser.rb +311 -0
- data/lib/json/version.rb +9 -0
- data/references/rfc7159.txt +899 -0
- data/tests/fixtures/fail10.json +1 -0
- data/tests/fixtures/fail11.json +1 -0
- data/tests/fixtures/fail12.json +1 -0
- data/tests/fixtures/fail13.json +1 -0
- data/tests/fixtures/fail14.json +1 -0
- data/tests/fixtures/fail18.json +1 -0
- data/tests/fixtures/fail19.json +1 -0
- data/tests/fixtures/fail2.json +1 -0
- data/tests/fixtures/fail20.json +1 -0
- data/tests/fixtures/fail21.json +1 -0
- data/tests/fixtures/fail22.json +1 -0
- data/tests/fixtures/fail23.json +1 -0
- data/tests/fixtures/fail24.json +1 -0
- data/tests/fixtures/fail25.json +1 -0
- data/tests/fixtures/fail27.json +2 -0
- data/tests/fixtures/fail28.json +2 -0
- data/tests/fixtures/fail3.json +1 -0
- data/tests/fixtures/fail4.json +1 -0
- data/tests/fixtures/fail5.json +1 -0
- data/tests/fixtures/fail6.json +1 -0
- data/tests/fixtures/fail7.json +1 -0
- data/tests/fixtures/fail8.json +1 -0
- data/tests/fixtures/fail9.json +1 -0
- data/tests/fixtures/obsolete_fail1.json +1 -0
- data/tests/fixtures/pass1.json +56 -0
- data/tests/fixtures/pass15.json +1 -0
- data/tests/fixtures/pass16.json +1 -0
- data/tests/fixtures/pass17.json +1 -0
- data/tests/fixtures/pass2.json +1 -0
- data/tests/fixtures/pass26.json +1 -0
- data/tests/fixtures/pass3.json +6 -0
- data/tests/json_addition_test.rb +203 -0
- data/tests/json_common_interface_test.rb +126 -0
- data/tests/json_encoding_test.rb +107 -0
- data/tests/json_ext_parser_test.rb +15 -0
- data/tests/json_fixtures_test.rb +32 -0
- data/tests/json_generator_test.rb +377 -0
- data/tests/json_generic_object_test.rb +82 -0
- data/tests/json_parser_test.rb +472 -0
- data/tests/json_string_matching_test.rb +38 -0
- data/tests/test_helper.rb +17 -0
- data/tools/diff.sh +18 -0
- data/tools/fuzz.rb +131 -0
- data/tools/server.rb +62 -0
- metadata +185 -0
data/lib/json/add/set.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
defined?(::Set) or require 'set'
|
5
|
+
|
6
|
+
class Set
|
7
|
+
# Import a JSON Marshalled object.
|
8
|
+
#
|
9
|
+
# method used for JSON marshalling support.
|
10
|
+
def self.json_create(object)
|
11
|
+
new object['a']
|
12
|
+
end
|
13
|
+
|
14
|
+
# Marshal the object to JSON.
|
15
|
+
#
|
16
|
+
# method used for JSON marshalling support.
|
17
|
+
def as_json(*)
|
18
|
+
{
|
19
|
+
JSON.create_id => self.class.name,
|
20
|
+
'a' => to_a,
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
# return the JSON value
|
25
|
+
def to_json(*args)
|
26
|
+
as_json.to_json(*args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#frozen_string_literal: false
|
2
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
3
|
+
require 'json'
|
4
|
+
end
|
5
|
+
|
6
|
+
class Struct
|
7
|
+
|
8
|
+
# Deserializes JSON string by constructing new Struct object with values
|
9
|
+
# <tt>v</tt> serialized by <tt>to_json</tt>.
|
10
|
+
def self.json_create(object)
|
11
|
+
new(*object['v'])
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
15
|
+
# object.
|
16
|
+
def as_json(*)
|
17
|
+
klass = self.class.name
|
18
|
+
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
19
|
+
{
|
20
|
+
JSON.create_id => klass,
|
21
|
+
'v' => values,
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
# Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string.
|
26
|
+
# Only named structs are supported.
|
27
|
+
def to_json(*args)
|
28
|
+
as_json.to_json(*args)
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#frozen_string_literal: false
|
2
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
3
|
+
require 'json'
|
4
|
+
end
|
5
|
+
|
6
|
+
class Symbol
|
7
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
8
|
+
# object.
|
9
|
+
def as_json(*)
|
10
|
+
{
|
11
|
+
JSON.create_id => self.class.name,
|
12
|
+
's' => to_s,
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
# Stores class name (Symbol) with String representation of Symbol as a JSON string.
|
17
|
+
def to_json(*a)
|
18
|
+
as_json.to_json(*a)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
|
22
|
+
def self.json_create(o)
|
23
|
+
o['s'].to_sym
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#frozen_string_literal: false
|
2
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
3
|
+
require 'json'
|
4
|
+
end
|
5
|
+
|
6
|
+
class Time
|
7
|
+
|
8
|
+
# Deserializes JSON string by converting time since epoch to Time
|
9
|
+
def self.json_create(object)
|
10
|
+
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
|
11
|
+
object['n'] = usec * 1000
|
12
|
+
end
|
13
|
+
if method_defined?(:tv_nsec)
|
14
|
+
at(object['s'], Rational(object['n'], 1000))
|
15
|
+
else
|
16
|
+
at(object['s'], object['n'] / 1000)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
21
|
+
# object.
|
22
|
+
def as_json(*)
|
23
|
+
nanoseconds = [ tv_usec * 1000 ]
|
24
|
+
respond_to?(:tv_nsec) and nanoseconds << tv_nsec
|
25
|
+
nanoseconds = nanoseconds.max
|
26
|
+
{
|
27
|
+
JSON.create_id => self.class.name,
|
28
|
+
's' => tv_sec,
|
29
|
+
'n' => nanoseconds,
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
# Stores class name (Time) with number of seconds since epoch and number of
|
34
|
+
# microseconds for Time as JSON string
|
35
|
+
def to_json(*args)
|
36
|
+
as_json.to_json(*args)
|
37
|
+
end
|
38
|
+
end
|
data/lib/json/common.rb
ADDED
@@ -0,0 +1,456 @@
|
|
1
|
+
#frozen_string_literal: false
|
2
|
+
require 'json/version'
|
3
|
+
require 'json/generic_object'
|
4
|
+
|
5
|
+
module JSON
|
6
|
+
class << self
|
7
|
+
# If _object_ is string-like, parse the string and return the parsed
|
8
|
+
# result as a Ruby data structure. Otherwise generate a JSON text from the
|
9
|
+
# Ruby data structure object and return it.
|
10
|
+
#
|
11
|
+
# The _opts_ argument is passed through to generate/parse respectively.
|
12
|
+
# See generate and parse for their documentation.
|
13
|
+
def [](object, opts = {})
|
14
|
+
if object.respond_to? :to_str
|
15
|
+
JSON.parse(object.to_str, opts)
|
16
|
+
else
|
17
|
+
JSON.generate(object, opts)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the JSON parser class that is used by JSON. This is either
|
22
|
+
# JSON::Ext::Parser or JSON::Pure::Parser.
|
23
|
+
attr_reader :parser
|
24
|
+
|
25
|
+
# Set the JSON parser class _parser_ to be used by JSON.
|
26
|
+
def parser=(parser) # :nodoc:
|
27
|
+
@parser = parser
|
28
|
+
remove_const :Parser if const_defined?(:Parser, false)
|
29
|
+
const_set :Parser, parser
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return the constant located at _path_. The format of _path_ has to be
|
33
|
+
# either ::A::B::C or A::B::C. In any case, A has to be located at the top
|
34
|
+
# level (absolute namespace path?). If there doesn't exist a constant at
|
35
|
+
# the given path, an ArgumentError is raised.
|
36
|
+
def deep_const_get(path) # :nodoc:
|
37
|
+
path.to_s.split(/::/).inject(Object) do |p, c|
|
38
|
+
case
|
39
|
+
when c.empty? then p
|
40
|
+
when p.const_defined?(c, true) then p.const_get(c)
|
41
|
+
else
|
42
|
+
begin
|
43
|
+
p.const_missing(c)
|
44
|
+
rescue NameError => e
|
45
|
+
raise ArgumentError, "can't get const #{path}: #{e}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Set the module _generator_ to be used by JSON.
|
52
|
+
def generator=(generator) # :nodoc:
|
53
|
+
old, $VERBOSE = $VERBOSE, nil
|
54
|
+
@generator = generator
|
55
|
+
generator_methods = generator::GeneratorMethods
|
56
|
+
for const in generator_methods.constants
|
57
|
+
klass = deep_const_get(const)
|
58
|
+
modul = generator_methods.const_get(const)
|
59
|
+
klass.class_eval do
|
60
|
+
instance_methods(false).each do |m|
|
61
|
+
m.to_s == 'to_json' and remove_method m
|
62
|
+
end
|
63
|
+
include modul
|
64
|
+
end
|
65
|
+
end
|
66
|
+
self.state = generator::State
|
67
|
+
const_set :State, self.state
|
68
|
+
const_set :SAFE_STATE_PROTOTYPE, State.new
|
69
|
+
const_set :FAST_STATE_PROTOTYPE, State.new(
|
70
|
+
:indent => '',
|
71
|
+
:space => '',
|
72
|
+
:object_nl => "",
|
73
|
+
:array_nl => "",
|
74
|
+
:max_nesting => false
|
75
|
+
)
|
76
|
+
const_set :PRETTY_STATE_PROTOTYPE, State.new(
|
77
|
+
:indent => ' ',
|
78
|
+
:space => ' ',
|
79
|
+
:object_nl => "\n",
|
80
|
+
:array_nl => "\n"
|
81
|
+
)
|
82
|
+
ensure
|
83
|
+
$VERBOSE = old
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns the JSON generator module that is used by JSON. This is
|
87
|
+
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
88
|
+
attr_reader :generator
|
89
|
+
|
90
|
+
# Returns the JSON generator state class that is used by JSON. This is
|
91
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
92
|
+
attr_accessor :state
|
93
|
+
|
94
|
+
# This is create identifier, which is used to decide if the _json_create_
|
95
|
+
# hook of a class should be called. It defaults to 'json_class'.
|
96
|
+
attr_accessor :create_id
|
97
|
+
end
|
98
|
+
self.create_id = 'json_class'
|
99
|
+
|
100
|
+
NaN = 0.0/0
|
101
|
+
|
102
|
+
Infinity = 1.0/0
|
103
|
+
|
104
|
+
MinusInfinity = -Infinity
|
105
|
+
|
106
|
+
# The base exception for JSON errors.
|
107
|
+
class JSONError < StandardError
|
108
|
+
def self.wrap(exception)
|
109
|
+
obj = new("Wrapped(#{exception.class}): #{exception.message.inspect}")
|
110
|
+
obj.set_backtrace exception.backtrace
|
111
|
+
obj
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# This exception is raised if a parser error occurs.
|
116
|
+
class ParserError < JSONError; end
|
117
|
+
|
118
|
+
# This exception is raised if the nesting of parsed data structures is too
|
119
|
+
# deep.
|
120
|
+
class NestingError < ParserError; end
|
121
|
+
|
122
|
+
# :stopdoc:
|
123
|
+
class CircularDatastructure < NestingError; end
|
124
|
+
# :startdoc:
|
125
|
+
|
126
|
+
# This exception is raised if a generator or unparser error occurs.
|
127
|
+
class GeneratorError < JSONError; end
|
128
|
+
# For backwards compatibility
|
129
|
+
UnparserError = GeneratorError
|
130
|
+
|
131
|
+
# This exception is raised if the required unicode support is missing on the
|
132
|
+
# system. Usually this means that the iconv library is not installed.
|
133
|
+
class MissingUnicodeSupport < JSONError; end
|
134
|
+
|
135
|
+
module_function
|
136
|
+
|
137
|
+
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
138
|
+
#
|
139
|
+
# _opts_ can have the following
|
140
|
+
# keys:
|
141
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
142
|
+
# structures. Disable depth checking with :max_nesting => false. It
|
143
|
+
# defaults to 100.
|
144
|
+
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
145
|
+
# defiance of RFC 7159 to be parsed by the Parser. This option defaults
|
146
|
+
# to false.
|
147
|
+
# * *symbolize_names*: If set to true, returns symbols for the names
|
148
|
+
# (keys) in a JSON object. Otherwise strings are returned. Strings are
|
149
|
+
# the default.
|
150
|
+
# * *create_additions*: If set to false, the Parser doesn't create
|
151
|
+
# additions even if a matching class and create_id was found. This option
|
152
|
+
# defaults to false.
|
153
|
+
# * *object_class*: Defaults to Hash
|
154
|
+
# * *array_class*: Defaults to Array
|
155
|
+
def parse(source, opts = {})
|
156
|
+
Parser.new(source, opts).parse
|
157
|
+
end
|
158
|
+
|
159
|
+
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
160
|
+
# The bang version of the parse method defaults to the more dangerous values
|
161
|
+
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
162
|
+
#
|
163
|
+
# _opts_ can have the following keys:
|
164
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
165
|
+
# structures. Enable depth checking with :max_nesting => anInteger. The
|
166
|
+
# parse! methods defaults to not doing max depth checking: This can be
|
167
|
+
# dangerous if someone wants to fill up your stack.
|
168
|
+
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
|
169
|
+
# defiance of RFC 7159 to be parsed by the Parser. This option defaults
|
170
|
+
# to true.
|
171
|
+
# * *create_additions*: If set to false, the Parser doesn't create
|
172
|
+
# additions even if a matching class and create_id was found. This option
|
173
|
+
# defaults to false.
|
174
|
+
def parse!(source, opts = {})
|
175
|
+
opts = {
|
176
|
+
:max_nesting => false,
|
177
|
+
:allow_nan => true
|
178
|
+
}.merge(opts)
|
179
|
+
Parser.new(source, opts).parse
|
180
|
+
end
|
181
|
+
|
182
|
+
# Generate a JSON document from the Ruby data structure _obj_ and return
|
183
|
+
# it. _state_ is * a JSON::State object,
|
184
|
+
# * or a Hash like object (responding to to_hash),
|
185
|
+
# * an object convertible into a hash by a to_h method,
|
186
|
+
# that is used as or to configure a State object.
|
187
|
+
#
|
188
|
+
# It defaults to a state object, that creates the shortest possible JSON text
|
189
|
+
# in one line, checks for circular data structures and doesn't allow NaN,
|
190
|
+
# Infinity, and -Infinity.
|
191
|
+
#
|
192
|
+
# A _state_ hash can have the following keys:
|
193
|
+
# * *indent*: a string used to indent levels (default: ''),
|
194
|
+
# * *space*: a string that is put after, a : or , delimiter (default: ''),
|
195
|
+
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
196
|
+
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
197
|
+
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
198
|
+
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
199
|
+
# generated, otherwise an exception is thrown if these values are
|
200
|
+
# encountered. This options defaults to false.
|
201
|
+
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
202
|
+
# structures from which JSON is to be generated. Disable depth checking
|
203
|
+
# with :max_nesting => false, it defaults to 100.
|
204
|
+
#
|
205
|
+
# See also the fast_generate for the fastest creation method with the least
|
206
|
+
# amount of sanity checks, and the pretty_generate method for some
|
207
|
+
# defaults for pretty output.
|
208
|
+
def generate(obj, opts = nil)
|
209
|
+
if State === opts
|
210
|
+
state, opts = opts, nil
|
211
|
+
else
|
212
|
+
state = SAFE_STATE_PROTOTYPE.dup
|
213
|
+
end
|
214
|
+
if opts
|
215
|
+
if opts.respond_to? :to_hash
|
216
|
+
opts = opts.to_hash
|
217
|
+
elsif opts.respond_to? :to_h
|
218
|
+
opts = opts.to_h
|
219
|
+
else
|
220
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
221
|
+
end
|
222
|
+
state = state.configure(opts)
|
223
|
+
end
|
224
|
+
state.generate(obj)
|
225
|
+
end
|
226
|
+
|
227
|
+
# :stopdoc:
|
228
|
+
# I want to deprecate these later, so I'll first be silent about them, and
|
229
|
+
# later delete them.
|
230
|
+
alias unparse generate
|
231
|
+
module_function :unparse
|
232
|
+
# :startdoc:
|
233
|
+
|
234
|
+
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
235
|
+
# This method disables the checks for circles in Ruby objects.
|
236
|
+
#
|
237
|
+
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
238
|
+
# _obj_ argument because this will cause JSON to go into an infinite loop.
|
239
|
+
def fast_generate(obj, opts = nil)
|
240
|
+
if State === opts
|
241
|
+
state, opts = opts, nil
|
242
|
+
else
|
243
|
+
state = FAST_STATE_PROTOTYPE.dup
|
244
|
+
end
|
245
|
+
if opts
|
246
|
+
if opts.respond_to? :to_hash
|
247
|
+
opts = opts.to_hash
|
248
|
+
elsif opts.respond_to? :to_h
|
249
|
+
opts = opts.to_h
|
250
|
+
else
|
251
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
252
|
+
end
|
253
|
+
state.configure(opts)
|
254
|
+
end
|
255
|
+
state.generate(obj)
|
256
|
+
end
|
257
|
+
|
258
|
+
# :stopdoc:
|
259
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
260
|
+
alias fast_unparse fast_generate
|
261
|
+
module_function :fast_unparse
|
262
|
+
# :startdoc:
|
263
|
+
|
264
|
+
# Generate a JSON document from the Ruby data structure _obj_ and return it.
|
265
|
+
# The returned document is a prettier form of the document returned by
|
266
|
+
# #unparse.
|
267
|
+
#
|
268
|
+
# The _opts_ argument can be used to configure the generator. See the
|
269
|
+
# generate method for a more detailed explanation.
|
270
|
+
def pretty_generate(obj, opts = nil)
|
271
|
+
if State === opts
|
272
|
+
state, opts = opts, nil
|
273
|
+
else
|
274
|
+
state = PRETTY_STATE_PROTOTYPE.dup
|
275
|
+
end
|
276
|
+
if opts
|
277
|
+
if opts.respond_to? :to_hash
|
278
|
+
opts = opts.to_hash
|
279
|
+
elsif opts.respond_to? :to_h
|
280
|
+
opts = opts.to_h
|
281
|
+
else
|
282
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
283
|
+
end
|
284
|
+
state.configure(opts)
|
285
|
+
end
|
286
|
+
state.generate(obj)
|
287
|
+
end
|
288
|
+
|
289
|
+
# :stopdoc:
|
290
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
291
|
+
alias pretty_unparse pretty_generate
|
292
|
+
module_function :pretty_unparse
|
293
|
+
# :startdoc:
|
294
|
+
|
295
|
+
class << self
|
296
|
+
# The global default options for the JSON.load method:
|
297
|
+
# :max_nesting: false
|
298
|
+
# :allow_nan: true
|
299
|
+
# :allow_blank: true
|
300
|
+
attr_accessor :load_default_options
|
301
|
+
end
|
302
|
+
self.load_default_options = {
|
303
|
+
:max_nesting => false,
|
304
|
+
:allow_nan => true,
|
305
|
+
:allow_blank => true,
|
306
|
+
:create_additions => true,
|
307
|
+
}
|
308
|
+
|
309
|
+
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
310
|
+
# either be a string-like object, an IO-like object, or an object responding
|
311
|
+
# to the read method. If _proc_ was given, it will be called with any nested
|
312
|
+
# Ruby object as an argument recursively in depth first order. To modify the
|
313
|
+
# default options pass in the optional _options_ argument as well.
|
314
|
+
#
|
315
|
+
# BEWARE: This method is meant to serialise data from trusted user input,
|
316
|
+
# like from your own database server or clients under your control, it could
|
317
|
+
# be dangerous to allow untrusted users to pass JSON sources into it. The
|
318
|
+
# default options for the parser can be changed via the load_default_options
|
319
|
+
# method.
|
320
|
+
#
|
321
|
+
# This method is part of the implementation of the load/dump interface of
|
322
|
+
# Marshal and YAML.
|
323
|
+
def load(source, proc = nil, options = {})
|
324
|
+
opts = load_default_options.merge options
|
325
|
+
if source.respond_to? :to_str
|
326
|
+
source = source.to_str
|
327
|
+
elsif source.respond_to? :to_io
|
328
|
+
source = source.to_io.read
|
329
|
+
elsif source.respond_to?(:read)
|
330
|
+
source = source.read
|
331
|
+
end
|
332
|
+
if opts[:allow_blank] && (source.nil? || source.empty?)
|
333
|
+
source = 'null'
|
334
|
+
end
|
335
|
+
result = parse(source, opts)
|
336
|
+
recurse_proc(result, &proc) if proc
|
337
|
+
result
|
338
|
+
end
|
339
|
+
|
340
|
+
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
341
|
+
def recurse_proc(result, &proc)
|
342
|
+
case result
|
343
|
+
when Array
|
344
|
+
result.each { |x| recurse_proc x, &proc }
|
345
|
+
proc.call result
|
346
|
+
when Hash
|
347
|
+
result.each { |x, y| recurse_proc x, &proc; recurse_proc y, &proc }
|
348
|
+
proc.call result
|
349
|
+
else
|
350
|
+
proc.call result
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
alias restore load
|
355
|
+
module_function :restore
|
356
|
+
|
357
|
+
class << self
|
358
|
+
# The global default options for the JSON.dump method:
|
359
|
+
# :max_nesting: false
|
360
|
+
# :allow_nan: true
|
361
|
+
# :allow_blank: true
|
362
|
+
attr_accessor :dump_default_options
|
363
|
+
end
|
364
|
+
self.dump_default_options = {
|
365
|
+
:max_nesting => false,
|
366
|
+
:allow_nan => true,
|
367
|
+
}
|
368
|
+
|
369
|
+
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
370
|
+
# the result.
|
371
|
+
#
|
372
|
+
# If anIO (an IO-like object or an object that responds to the write method)
|
373
|
+
# was given, the resulting JSON is written to it.
|
374
|
+
#
|
375
|
+
# If the number of nested arrays or objects exceeds _limit_, an ArgumentError
|
376
|
+
# exception is raised. This argument is similar (but not exactly the
|
377
|
+
# same!) to the _limit_ argument in Marshal.dump.
|
378
|
+
#
|
379
|
+
# The default options for the generator can be changed via the
|
380
|
+
# dump_default_options method.
|
381
|
+
#
|
382
|
+
# This method is part of the implementation of the load/dump interface of
|
383
|
+
# Marshal and YAML.
|
384
|
+
def dump(obj, anIO = nil, limit = nil)
|
385
|
+
if anIO and limit.nil?
|
386
|
+
anIO = anIO.to_io if anIO.respond_to?(:to_io)
|
387
|
+
unless anIO.respond_to?(:write)
|
388
|
+
limit = anIO
|
389
|
+
anIO = nil
|
390
|
+
end
|
391
|
+
end
|
392
|
+
opts = JSON.dump_default_options
|
393
|
+
opts = opts.merge(:max_nesting => limit) if limit
|
394
|
+
result = generate(obj, opts)
|
395
|
+
if anIO
|
396
|
+
anIO.write result
|
397
|
+
anIO
|
398
|
+
else
|
399
|
+
result
|
400
|
+
end
|
401
|
+
rescue JSON::NestingError
|
402
|
+
raise ArgumentError, "exceed depth limit"
|
403
|
+
end
|
404
|
+
|
405
|
+
# Encodes string using Ruby's _String.encode_
|
406
|
+
def self.iconv(to, from, string)
|
407
|
+
string.encode(to, from)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
module ::Kernel
|
412
|
+
private
|
413
|
+
|
414
|
+
# Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
|
415
|
+
# one line.
|
416
|
+
def j(*objs)
|
417
|
+
objs.each do |obj|
|
418
|
+
puts JSON::generate(obj, :allow_nan => true, :max_nesting => false)
|
419
|
+
end
|
420
|
+
nil
|
421
|
+
end
|
422
|
+
|
423
|
+
# Outputs _objs_ to STDOUT as JSON strings in a pretty format, with
|
424
|
+
# indentation and over many lines.
|
425
|
+
def jj(*objs)
|
426
|
+
objs.each do |obj|
|
427
|
+
puts JSON::pretty_generate(obj, :allow_nan => true, :max_nesting => false)
|
428
|
+
end
|
429
|
+
nil
|
430
|
+
end
|
431
|
+
|
432
|
+
# If _object_ is string-like, parse the string and return the parsed result as
|
433
|
+
# a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
|
434
|
+
# structure object and return it.
|
435
|
+
#
|
436
|
+
# The _opts_ argument is passed through to generate/parse respectively. See
|
437
|
+
# generate and parse for their documentation.
|
438
|
+
def JSON(object, *args)
|
439
|
+
if object.respond_to? :to_str
|
440
|
+
JSON.parse(object.to_str, args.first)
|
441
|
+
else
|
442
|
+
JSON.generate(object, args.first)
|
443
|
+
end
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
# Extends any Class to include _json_creatable?_ method.
|
448
|
+
class ::Class
|
449
|
+
# Returns true if this class can be used to create an instance
|
450
|
+
# from a serialised JSON string. The class has to implement a class
|
451
|
+
# method _json_create_ that expects a hash as first parameter. The hash
|
452
|
+
# should include the required data.
|
453
|
+
def json_creatable?
|
454
|
+
respond_to?(:json_create)
|
455
|
+
end
|
456
|
+
end
|