json-maglev- 1.5.4 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +11 -1
- data/Rakefile +14 -12
- data/VERSION +1 -1
- data/ext/json/ext/generator/extconf.rb +0 -7
- data/ext/json/ext/generator/generator.c +55 -10
- data/ext/json/ext/generator/generator.h +7 -5
- data/ext/json/ext/parser/extconf.rb +0 -3
- data/ext/json/ext/parser/parser.c +418 -207
- data/ext/json/ext/parser/parser.h +11 -10
- data/ext/json/ext/parser/parser.rl +178 -104
- data/install.rb +1 -8
- data/java/src/json/ext/Generator.java +3 -3
- data/java/src/json/ext/GeneratorMethods.java +2 -2
- data/java/src/json/ext/GeneratorService.java +1 -1
- data/java/src/json/ext/GeneratorState.java +41 -13
- data/java/src/json/ext/OptionsReader.java +1 -1
- data/java/src/json/ext/Parser.java +382 -107
- data/java/src/json/ext/Parser.rl +97 -28
- data/java/src/json/ext/ParserService.java +1 -1
- data/java/src/json/ext/Utils.java +1 -1
- data/json.gemspec +5 -6
- data/json_pure.gemspec +5 -9
- data/lib/json.rb +4 -4
- data/lib/json/add/complex.rb +22 -0
- data/lib/json/add/core.rb +9 -241
- 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/range.rb +29 -0
- data/lib/json/add/rational.rb +22 -0
- data/lib/json/add/regexp.rb +30 -0
- data/lib/json/add/struct.rb +30 -0
- data/lib/json/add/symbol.rb +25 -0
- data/lib/json/add/time.rb +35 -0
- data/lib/json/common.rb +47 -35
- data/lib/json/ext.rb +2 -15
- data/lib/json/pure/generator.rb +17 -2
- data/lib/json/pure/parser.rb +89 -55
- data/lib/json/version.rb +1 -1
- data/tests/test_json.rb +36 -0
- data/tests/test_json_addition.rb +8 -1
- data/tests/test_json_generate.rb +34 -1
- data/tools/server.rb +1 -0
- metadata +20 -24
- data/bin/edit_json.rb +0 -9
- data/bin/prettify_json.rb +0 -48
- data/lib/json/Array.xpm +0 -21
- data/lib/json/FalseClass.xpm +0 -21
- data/lib/json/Hash.xpm +0 -21
- data/lib/json/Key.xpm +0 -73
- data/lib/json/NilClass.xpm +0 -21
- data/lib/json/Numeric.xpm +0 -28
- data/lib/json/String.xpm +0 -96
- data/lib/json/TrueClass.xpm +0 -21
- data/lib/json/add/rails.rb +0 -8
- data/lib/json/editor.rb +0 -1369
- data/lib/json/json.xpm +0 -1499
@@ -0,0 +1,34 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
# Date serialization/deserialization
|
7
|
+
class Date
|
8
|
+
|
9
|
+
# Deserializes JSON string by converting Julian year <tt>y</tt>, month
|
10
|
+
# <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
|
11
|
+
def self.json_create(object)
|
12
|
+
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
13
|
+
end
|
14
|
+
|
15
|
+
alias start sg unless method_defined?(:start)
|
16
|
+
|
17
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
18
|
+
# object.
|
19
|
+
def as_json(*)
|
20
|
+
{
|
21
|
+
JSON.create_id => self.class.name,
|
22
|
+
'y' => year,
|
23
|
+
'm' => month,
|
24
|
+
'd' => day,
|
25
|
+
'sg' => start,
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
# Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
|
30
|
+
# <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
|
31
|
+
def to_json(*args)
|
32
|
+
as_json.to_json(*args)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
# DateTime serialization/deserialization
|
7
|
+
class DateTime
|
8
|
+
|
9
|
+
# Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
|
10
|
+
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
|
11
|
+
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
|
12
|
+
def self.json_create(object)
|
13
|
+
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
14
|
+
of_a, of_b = object['of'].split('/')
|
15
|
+
if of_b and of_b != '0'
|
16
|
+
args << Rational(of_a.to_i, of_b.to_i)
|
17
|
+
else
|
18
|
+
args << of_a
|
19
|
+
end
|
20
|
+
args << object['sg']
|
21
|
+
civil(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
alias start sg unless method_defined?(:start)
|
25
|
+
|
26
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
27
|
+
# object.
|
28
|
+
def as_json(*)
|
29
|
+
{
|
30
|
+
JSON.create_id => self.class.name,
|
31
|
+
'y' => year,
|
32
|
+
'm' => month,
|
33
|
+
'd' => day,
|
34
|
+
'H' => hour,
|
35
|
+
'M' => min,
|
36
|
+
'S' => sec,
|
37
|
+
'of' => offset.to_s,
|
38
|
+
'sg' => start,
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
|
43
|
+
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
|
44
|
+
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
|
45
|
+
def to_json(*args)
|
46
|
+
as_json.to_json(*args)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Exception serialization/deserialization
|
6
|
+
class Exception
|
7
|
+
|
8
|
+
# Deserializes JSON string by constructing new Exception object with message
|
9
|
+
# <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
|
10
|
+
def self.json_create(object)
|
11
|
+
result = new(object['m'])
|
12
|
+
result.set_backtrace object['b']
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
17
|
+
# object.
|
18
|
+
def as_json(*)
|
19
|
+
{
|
20
|
+
JSON.create_id => self.class.name,
|
21
|
+
'm' => message,
|
22
|
+
'b' => backtrace,
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
# Stores class name (Exception) with message <tt>m</tt> and backtrace array
|
27
|
+
# <tt>b</tt> as JSON string
|
28
|
+
def to_json(*args)
|
29
|
+
as_json.to_json(*args)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Range serialization/deserialization
|
6
|
+
class Range
|
7
|
+
|
8
|
+
# Deserializes JSON string by constructing new Range object with arguments
|
9
|
+
# <tt>a</tt> serialized by <tt>to_json</tt>.
|
10
|
+
def self.json_create(object)
|
11
|
+
new(*object['a'])
|
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
|
+
{
|
18
|
+
JSON.create_id => self.class.name,
|
19
|
+
'a' => [ first, last, exclude_end? ]
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
# Stores class name (Range) with JSON array of arguments <tt>a</tt> which
|
24
|
+
# include <tt>first</tt> (integer), <tt>last</tt> (integer), and
|
25
|
+
# <tt>exclude_end?</tt> (boolean) as JSON string.
|
26
|
+
def to_json(*args)
|
27
|
+
as_json.to_json(*args)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
defined?(::Rational) or require 'rational'
|
5
|
+
|
6
|
+
class Rational
|
7
|
+
def self.json_create(object)
|
8
|
+
Rational(object['n'], object['d'])
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_json(*)
|
12
|
+
{
|
13
|
+
JSON.create_id => self.class.name,
|
14
|
+
'n' => numerator,
|
15
|
+
'd' => denominator,
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_json(*)
|
20
|
+
as_json.to_json
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Regexp serialization/deserialization
|
6
|
+
class Regexp
|
7
|
+
|
8
|
+
# Deserializes JSON string by constructing new Regexp object with source
|
9
|
+
# <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by
|
10
|
+
# <tt>to_json</tt>
|
11
|
+
def self.json_create(object)
|
12
|
+
new(object['s'], object['o'])
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
16
|
+
# object.
|
17
|
+
def as_json(*)
|
18
|
+
{
|
19
|
+
JSON.create_id => self.class.name,
|
20
|
+
'o' => options,
|
21
|
+
's' => source,
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
# Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
|
26
|
+
# (Regexp or String) as JSON string
|
27
|
+
def to_json(*)
|
28
|
+
as_json.to_json
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Struct serialization/deserialization
|
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
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Symbol serialization/deserialization
|
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,35 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Time serialization/deserialization
|
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 respond_to?(:tv_nsec)
|
14
|
+
at(*object.values_at('s', 'n'))
|
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
|
+
{
|
24
|
+
JSON.create_id => self.class.name,
|
25
|
+
's' => tv_sec,
|
26
|
+
'n' => respond_to?(:tv_nsec) ? tv_nsec : tv_usec * 1000
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Stores class name (Time) with number of seconds since epoch and number of
|
31
|
+
# microseconds for Time as JSON string
|
32
|
+
def to_json(*args)
|
33
|
+
as_json.to_json(*args)
|
34
|
+
end
|
35
|
+
end
|
data/lib/json/common.rb
CHANGED
@@ -2,11 +2,11 @@ require 'json/version'
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
class << self
|
5
|
-
# If _object_ is string-like parse the string and return the parsed result
|
5
|
+
# If _object_ is string-like, parse the string and return the parsed result
|
6
6
|
# as a Ruby data structure. Otherwise generate a JSON text from the Ruby
|
7
7
|
# data structure object and return it.
|
8
8
|
#
|
9
|
-
# The _opts_ argument is passed through to generate/parse respectively
|
9
|
+
# The _opts_ argument is passed through to generate/parse respectively. See
|
10
10
|
# generate and parse for their documentation.
|
11
11
|
def [](object, opts = {})
|
12
12
|
if object.respond_to? :to_str
|
@@ -16,7 +16,7 @@ module JSON
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
# Returns the JSON parser class
|
19
|
+
# Returns the JSON parser class that is used by JSON. This is either
|
20
20
|
# JSON::Ext::Parser or JSON::Pure::Parser.
|
21
21
|
attr_reader :parser
|
22
22
|
|
@@ -28,7 +28,7 @@ module JSON
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# Return the constant located at _path_. The format of _path_ has to be
|
31
|
-
# either ::A::B::C or A::B::C. In any case A has to be located at the top
|
31
|
+
# either ::A::B::C or A::B::C. In any case, A has to be located at the top
|
32
32
|
# level (absolute namespace path?). If there doesn't exist a constant at
|
33
33
|
# the given path, an ArgumentError is raised.
|
34
34
|
def deep_const_get(path) # :nodoc:
|
@@ -81,15 +81,15 @@ module JSON
|
|
81
81
|
$VERBOSE = old
|
82
82
|
end
|
83
83
|
|
84
|
-
# Returns the JSON generator
|
84
|
+
# Returns the JSON generator module that is used by JSON. This is
|
85
85
|
# either JSON::Ext::Generator or JSON::Pure::Generator.
|
86
86
|
attr_reader :generator
|
87
87
|
|
88
|
-
# Returns the JSON generator state class
|
89
|
-
#
|
88
|
+
# Returns the JSON generator state class that is used by JSON. This is
|
89
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
|
90
90
|
attr_accessor :state
|
91
91
|
|
92
|
-
# This is create identifier,
|
92
|
+
# This is create identifier, which is used to decide if the _json_create_
|
93
93
|
# hook of a class should be called. It defaults to 'json_class'.
|
94
94
|
attr_accessor :create_id
|
95
95
|
end
|
@@ -104,10 +104,10 @@ module JSON
|
|
104
104
|
# The base exception for JSON errors.
|
105
105
|
class JSONError < StandardError; end
|
106
106
|
|
107
|
-
# This exception is raised
|
107
|
+
# This exception is raised if a parser error occurs.
|
108
108
|
class ParserError < JSONError; end
|
109
109
|
|
110
|
-
# This exception is raised
|
110
|
+
# This exception is raised if the nesting of parsed data structures is too
|
111
111
|
# deep.
|
112
112
|
class NestingError < ParserError; end
|
113
113
|
|
@@ -115,13 +115,13 @@ module JSON
|
|
115
115
|
class CircularDatastructure < NestingError; end
|
116
116
|
# :startdoc:
|
117
117
|
|
118
|
-
# This exception is raised
|
118
|
+
# This exception is raised if a generator or unparser error occurs.
|
119
119
|
class GeneratorError < JSONError; end
|
120
120
|
# For backwards compatibility
|
121
121
|
UnparserError = GeneratorError
|
122
122
|
|
123
|
-
# This exception is raised
|
124
|
-
# system. Usually this means
|
123
|
+
# This exception is raised if the required unicode support is missing on the
|
124
|
+
# system. Usually this means that the iconv library is not installed.
|
125
125
|
class MissingUnicodeSupport < JSONError; end
|
126
126
|
|
127
127
|
module_function
|
@@ -131,16 +131,16 @@ module JSON
|
|
131
131
|
# _opts_ can have the following
|
132
132
|
# keys:
|
133
133
|
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
134
|
-
# structures. Disable depth checking with :max_nesting => false
|
134
|
+
# structures. Disable depth checking with :max_nesting => false. It defaults
|
135
135
|
# to 19.
|
136
136
|
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
137
137
|
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
138
138
|
# to false.
|
139
139
|
# * *symbolize_names*: If set to true, returns symbols for the names
|
140
|
-
# (keys) in a JSON object. Otherwise strings are returned
|
140
|
+
# (keys) in a JSON object. Otherwise strings are returned. Strings are
|
141
141
|
# the default.
|
142
142
|
# * *create_additions*: If set to false, the Parser doesn't create
|
143
|
-
# additions even if a
|
143
|
+
# additions even if a matching class and create_id was found. This option
|
144
144
|
# defaults to true.
|
145
145
|
# * *object_class*: Defaults to Hash
|
146
146
|
# * *array_class*: Defaults to Array
|
@@ -149,19 +149,19 @@ module JSON
|
|
149
149
|
end
|
150
150
|
|
151
151
|
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
152
|
-
# The bang version of the parse method
|
152
|
+
# The bang version of the parse method defaults to the more dangerous values
|
153
153
|
# for the _opts_ hash, so be sure only to parse trusted _source_ documents.
|
154
154
|
#
|
155
155
|
# _opts_ can have the following keys:
|
156
156
|
# * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
157
157
|
# structures. Enable depth checking with :max_nesting => anInteger. The parse!
|
158
|
-
# methods defaults to not doing max depth checking: This can be dangerous
|
158
|
+
# methods defaults to not doing max depth checking: This can be dangerous
|
159
159
|
# if someone wants to fill up your stack.
|
160
160
|
# * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
|
161
161
|
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
|
162
162
|
# to true.
|
163
163
|
# * *create_additions*: If set to false, the Parser doesn't create
|
164
|
-
# additions even if a
|
164
|
+
# additions even if a matching class and create_id was found. This option
|
165
165
|
# defaults to true.
|
166
166
|
def parse!(source, opts = {})
|
167
167
|
opts = {
|
@@ -188,7 +188,7 @@ module JSON
|
|
188
188
|
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
189
189
|
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
190
190
|
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
191
|
-
# generated, otherwise an exception is thrown
|
191
|
+
# generated, otherwise an exception is thrown if these values are
|
192
192
|
# encountered. This options defaults to false.
|
193
193
|
# * *max_nesting*: The maximum depth of nesting allowed in the data
|
194
194
|
# structures from which JSON is to be generated. Disable depth checking
|
@@ -196,9 +196,13 @@ module JSON
|
|
196
196
|
#
|
197
197
|
# See also the fast_generate for the fastest creation method with the least
|
198
198
|
# amount of sanity checks, and the pretty_generate method for some
|
199
|
-
# defaults for
|
199
|
+
# defaults for pretty output.
|
200
200
|
def generate(obj, opts = nil)
|
201
|
-
|
201
|
+
if State === opts
|
202
|
+
state, opts = opts, nil
|
203
|
+
else
|
204
|
+
state = SAFE_STATE_PROTOTYPE.dup
|
205
|
+
end
|
202
206
|
if opts
|
203
207
|
if opts.respond_to? :to_hash
|
204
208
|
opts = opts.to_hash
|
@@ -223,9 +227,13 @@ module JSON
|
|
223
227
|
# This method disables the checks for circles in Ruby objects.
|
224
228
|
#
|
225
229
|
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
226
|
-
# _obj_ argument
|
230
|
+
# _obj_ argument because this will cause JSON to go into an infinite loop.
|
227
231
|
def fast_generate(obj, opts = nil)
|
228
|
-
|
232
|
+
if State === opts
|
233
|
+
state, opts = opts, nil
|
234
|
+
else
|
235
|
+
state = FAST_STATE_PROTOTYPE.dup
|
236
|
+
end
|
229
237
|
if opts
|
230
238
|
if opts.respond_to? :to_hash
|
231
239
|
opts = opts.to_hash
|
@@ -249,10 +257,14 @@ module JSON
|
|
249
257
|
# The returned document is a prettier form of the document returned by
|
250
258
|
# #unparse.
|
251
259
|
#
|
252
|
-
# The _opts_ argument can be used to configure the generator
|
260
|
+
# The _opts_ argument can be used to configure the generator. See the
|
253
261
|
# generate method for a more detailed explanation.
|
254
262
|
def pretty_generate(obj, opts = nil)
|
255
|
-
|
263
|
+
if State === opts
|
264
|
+
state, opts = opts, nil
|
265
|
+
else
|
266
|
+
state = PRETTY_STATE_PROTOTYPE.dup
|
267
|
+
end
|
256
268
|
if opts
|
257
269
|
if opts.respond_to? :to_hash
|
258
270
|
opts = opts.to_hash
|
@@ -273,7 +285,7 @@ module JSON
|
|
273
285
|
# :startdoc:
|
274
286
|
|
275
287
|
# Load a ruby data structure from a JSON _source_ and return it. A source can
|
276
|
-
# either be a string-like object, an IO
|
288
|
+
# either be a string-like object, an IO-like object, or an object responding
|
277
289
|
# to the read method. If _proc_ was given, it will be called with any nested
|
278
290
|
# Ruby object as an argument recursively in depth first order.
|
279
291
|
#
|
@@ -312,10 +324,10 @@ module JSON
|
|
312
324
|
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
|
313
325
|
# the result.
|
314
326
|
#
|
315
|
-
# If anIO (an IO
|
327
|
+
# If anIO (an IO-like object or an object that responds to the write method)
|
316
328
|
# was given, the resulting JSON is written to it.
|
317
329
|
#
|
318
|
-
# If the number of nested arrays or objects exceeds _limit_ an ArgumentError
|
330
|
+
# If the number of nested arrays or objects exceeds _limit_, an ArgumentError
|
319
331
|
# exception is raised. This argument is similar (but not exactly the
|
320
332
|
# same!) to the _limit_ argument in Marshal.dump.
|
321
333
|
#
|
@@ -396,11 +408,11 @@ module ::Kernel
|
|
396
408
|
nil
|
397
409
|
end
|
398
410
|
|
399
|
-
# If _object_ is string-like parse the string and return the parsed result as
|
400
|
-
# a Ruby data structure. Otherwise generate a JSON text from the Ruby data
|
411
|
+
# If _object_ is string-like, parse the string and return the parsed result as
|
412
|
+
# a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
|
401
413
|
# structure object and return it.
|
402
414
|
#
|
403
|
-
# The _opts_ argument is passed through to generate/parse respectively
|
415
|
+
# The _opts_ argument is passed through to generate/parse respectively. See
|
404
416
|
# generate and parse for their documentation.
|
405
417
|
def JSON(object, *args)
|
406
418
|
if object.respond_to? :to_str
|
@@ -413,10 +425,10 @@ end
|
|
413
425
|
|
414
426
|
# Extends any Class to include _json_creatable?_ method.
|
415
427
|
class ::Class
|
416
|
-
# Returns true
|
428
|
+
# Returns true if this class can be used to create an instance
|
417
429
|
# from a serialised JSON string. The class has to implement a class
|
418
|
-
# method _json_create_ that expects a hash as first parameter
|
419
|
-
# the required data.
|
430
|
+
# method _json_create_ that expects a hash as first parameter. The hash
|
431
|
+
# should include the required data.
|
420
432
|
def json_creatable?
|
421
433
|
respond_to?(:json_create)
|
422
434
|
end
|