json_pure 2.6.3 → 2.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +46 -0
- data/README.md +4 -4
- data/json_pure.gemspec +7 -6
- data/lib/json/add/bigdecimal.rb +37 -8
- data/lib/json/add/complex.rb +28 -5
- data/lib/json/add/date.rb +26 -6
- data/lib/json/add/date_time.rb +25 -8
- data/lib/json/add/exception.rb +24 -6
- data/lib/json/add/ostruct.rb +26 -6
- data/lib/json/add/range.rb +32 -7
- data/lib/json/add/rational.rb +27 -5
- data/lib/json/add/regexp.rb +25 -7
- data/lib/json/add/set.rb +25 -6
- data/lib/json/add/struct.rb +28 -6
- data/lib/json/add/symbol.rb +27 -4
- data/lib/json/add/time.rb +26 -5
- data/lib/json/common.rb +27 -33
- data/lib/json/pure/generator.rb +62 -28
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +9 -0
- metadata +6 -7
- data/VERSION +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1677fc4d72bfe47d97c5d12b87322e3416397f74e70bcb74ae58ff47f9029708
|
4
|
+
data.tar.gz: 788a9dd2fb8d894897b2ad1bc5459c4386a148c4afe454a4f376e0b0a303c54e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c46741ede86d254d0b9bd432baf2194ab087495eee883e5503845a0cf83488709d0be8d88a2f87ab741d6f3381faf744261177c2371489d7aef1ad449bb8c528
|
7
|
+
data.tar.gz: c060614c537d46ac9946565a828ee17464221dc80eaed69ecc6b58c6d416b5b4d0e1397f2614af10bb23d0a2d69e99863e3d7522266ec9def718d8ec3df673fc
|
data/CHANGES.md
CHANGED
@@ -1,5 +1,38 @@
|
|
1
1
|
# Changes
|
2
2
|
|
3
|
+
### 2023-12-05 (2.7.1)
|
4
|
+
|
5
|
+
* JSON.dump: handle unenclosed hashes regression #554
|
6
|
+
* Overload kwargs in JSON.dump #556
|
7
|
+
* [DOC] RDoc for additions #557
|
8
|
+
* Fix JSON.dump overload combination #558
|
9
|
+
|
10
|
+
### 2023-12-01 (2.7.0)
|
11
|
+
|
12
|
+
* Add a strict option to Generator #519
|
13
|
+
* `escape_slash` option was renamed as `script_safe` and now also escape U+2028 and U+2029. `escape_slash` is now an alias of `script_safe` #525
|
14
|
+
* Remove unnecessary initialization of create_id in JSON.parse() #454
|
15
|
+
* Improvements to Hash#to_json in pure implementation generator #203
|
16
|
+
* Use ruby_xfree to free buffers #518
|
17
|
+
* Fix "unexpected token" offset for Infinity #507
|
18
|
+
* Avoid using deprecated BigDecimal.new on JRuby #546
|
19
|
+
* Removed code for Ruby 1.8 #540
|
20
|
+
* Rename JSON::ParseError to JSON:ParserError #530
|
21
|
+
* Call super in included hook #486
|
22
|
+
* JRuby requires a minimum of Java 8 #516
|
23
|
+
* Always indent even if empty #517
|
24
|
+
|
25
|
+
### 2022-11-30 (2.6.3)
|
26
|
+
|
27
|
+
* bugfix json/pure mixing escaped with literal unicode raises Encoding::CompatibilityError #483
|
28
|
+
* Stop including the parser source __LINE__ in exceptions #470
|
29
|
+
|
30
|
+
### 2022-11-17 (2.6.2)
|
31
|
+
|
32
|
+
* Remove unknown keyword arg from DateTime.parse #488
|
33
|
+
* Ignore java artifacts by @hsbt #489
|
34
|
+
* Fix parser bug for empty string allocation #496
|
35
|
+
|
3
36
|
### 2021-10-24 (2.6.1)
|
4
37
|
|
5
38
|
* Restore version.rb with 2.6.1
|
@@ -105,6 +138,19 @@
|
|
105
138
|
I changed these mentions to be consistent with the Ruby license setting in
|
106
139
|
the gemspec files which were already correct now.
|
107
140
|
|
141
|
+
## 2017-01-13 (1.8.6)
|
142
|
+
* Be compatible with ancient ruby 1.8 (maybe?)
|
143
|
+
|
144
|
+
## 2015-09-11 (1.8.5)
|
145
|
+
* Be compatible with ruby 2.4.0
|
146
|
+
* There were still some mentions of dual GPL licensing in the source, but JSON
|
147
|
+
has just the Ruby license that itself includes an explicit dual-licensing
|
148
|
+
clause that allows covered software to be distributed under the terms of
|
149
|
+
the Simplified BSD License instead for all ruby versions >= 1.9.3. This is
|
150
|
+
however a GPL compatible license according to the Free Software Foundation.
|
151
|
+
I changed these mentions to be consistent with the Ruby license setting in
|
152
|
+
the gemspec files which were already correct now.
|
153
|
+
|
108
154
|
## 2015-06-01 (1.8.3)
|
109
155
|
* Fix potential memory leak, thx to nobu.
|
110
156
|
|
data/README.md
CHANGED
@@ -12,8 +12,7 @@ will be two variants available:
|
|
12
12
|
extensions, which are both part of the ruby standard library.
|
13
13
|
* The quite a bit faster native extension variant, which is in parts
|
14
14
|
implemented in C or Java and comes with its own unicode conversion
|
15
|
-
functions and a parser generated by the
|
16
|
-
http://www.complang.org/ragel/ .
|
15
|
+
functions and a parser generated by the [Ragel] state machine compiler.
|
17
16
|
|
18
17
|
Both variants of the JSON generator generate UTF-8 character sequences by
|
19
18
|
default. If an :ascii\_only option with a true value is given, they escape all
|
@@ -71,8 +70,7 @@ with:
|
|
71
70
|
## Compiling the extensions yourself
|
72
71
|
|
73
72
|
If you want to create the `parser.c` file from its `parser.rl` file or draw nice
|
74
|
-
graphviz images of the state machines, you need
|
75
|
-
http://www.complang.org/ragel/
|
73
|
+
graphviz images of the state machines, you need [Ragel].
|
76
74
|
|
77
75
|
## Usage
|
78
76
|
|
@@ -423,3 +421,5 @@ The latest version of this library can be downloaded at
|
|
423
421
|
Online Documentation should be located at
|
424
422
|
|
425
423
|
* https://www.rubydoc.info/gems/json
|
424
|
+
|
425
|
+
[Ragel]: http://www.colm.net/open-source/ragel/
|
data/json_pure.gemspec
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
version = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line|
|
2
|
+
/^\s*VERSION\s*=\s*'(.*)'/ =~ line and break $1
|
3
|
+
end rescue nil
|
2
4
|
|
3
5
|
Gem::Specification.new do |s|
|
4
6
|
s.name = "json_pure".freeze
|
5
|
-
s.version =
|
7
|
+
s.version = version
|
6
8
|
|
7
9
|
s.summary = "JSON Implementation for Ruby".freeze
|
8
10
|
s.description = "This is a JSON implementation in pure Ruby.".freeze
|
@@ -16,7 +18,6 @@ Gem::Specification.new do |s|
|
|
16
18
|
"CHANGES.md".freeze,
|
17
19
|
"LICENSE".freeze,
|
18
20
|
"README.md".freeze,
|
19
|
-
"VERSION".freeze,
|
20
21
|
"json_pure.gemspec".freeze,
|
21
22
|
"lib/json.rb".freeze,
|
22
23
|
"lib/json/add/bigdecimal.rb".freeze,
|
@@ -41,12 +42,12 @@ Gem::Specification.new do |s|
|
|
41
42
|
"lib/json/pure/parser.rb".freeze,
|
42
43
|
"lib/json/version.rb".freeze,
|
43
44
|
]
|
44
|
-
s.homepage = "
|
45
|
+
s.homepage = "https://flori.github.io/json".freeze
|
45
46
|
s.metadata = {
|
46
47
|
'bug_tracker_uri' => 'https://github.com/flori/json/issues',
|
47
48
|
'changelog_uri' => 'https://github.com/flori/json/blob/master/CHANGES.md',
|
48
|
-
'documentation_uri' => '
|
49
|
-
'homepage_uri' =>
|
49
|
+
'documentation_uri' => 'https://flori.github.io/json/doc/index.html',
|
50
|
+
'homepage_uri' => s.homepage,
|
50
51
|
'source_code_uri' => 'https://github.com/flori/json',
|
51
52
|
'wiki_uri' => 'https://github.com/flori/json/wiki'
|
52
53
|
}
|
data/lib/json/add/bigdecimal.rb
CHANGED
@@ -2,19 +2,36 @@
|
|
2
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
3
3
|
require 'json'
|
4
4
|
end
|
5
|
-
|
5
|
+
begin
|
6
|
+
require 'bigdecimal'
|
7
|
+
rescue LoadError
|
8
|
+
end
|
6
9
|
|
7
10
|
class BigDecimal
|
8
|
-
|
9
|
-
#
|
10
|
-
# method used for JSON marshalling support.
|
11
|
+
|
12
|
+
# See #as_json.
|
11
13
|
def self.json_create(object)
|
12
14
|
BigDecimal._load object['b']
|
13
15
|
end
|
14
16
|
|
15
|
-
#
|
17
|
+
# Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
|
18
|
+
# to serialize and deserialize a \BigDecimal object;
|
19
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
20
|
+
#
|
21
|
+
# \Method <tt>BigDecimal#as_json</tt> serializes +self+,
|
22
|
+
# returning a 2-element hash representing +self+:
|
23
|
+
#
|
24
|
+
# require 'json/add/bigdecimal'
|
25
|
+
# x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
|
26
|
+
# y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
|
27
|
+
# z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
|
28
|
+
#
|
29
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
|
30
|
+
#
|
31
|
+
# BigDecimal.json_create(x) # => 0.2e1
|
32
|
+
# BigDecimal.json_create(y) # => 0.2e1
|
33
|
+
# BigDecimal.json_create(z) # => 0.2e1
|
16
34
|
#
|
17
|
-
# method used for JSON marshalling support.
|
18
35
|
def as_json(*)
|
19
36
|
{
|
20
37
|
JSON.create_id => self.class.name,
|
@@ -22,8 +39,20 @@ class BigDecimal
|
|
22
39
|
}
|
23
40
|
end
|
24
41
|
|
25
|
-
#
|
42
|
+
# Returns a JSON string representing +self+:
|
43
|
+
#
|
44
|
+
# require 'json/add/bigdecimal'
|
45
|
+
# puts BigDecimal(2).to_json
|
46
|
+
# puts BigDecimal(2.0, 4).to_json
|
47
|
+
# puts BigDecimal(Complex(2, 0)).to_json
|
48
|
+
#
|
49
|
+
# Output:
|
50
|
+
#
|
51
|
+
# {"json_class":"BigDecimal","b":"27:0.2e1"}
|
52
|
+
# {"json_class":"BigDecimal","b":"36:0.2e1"}
|
53
|
+
# {"json_class":"BigDecimal","b":"27:0.2e1"}
|
54
|
+
#
|
26
55
|
def to_json(*args)
|
27
56
|
as_json.to_json(*args)
|
28
57
|
end
|
29
|
-
end
|
58
|
+
end if defined?(::BigDecimal)
|
data/lib/json/add/complex.rb
CHANGED
@@ -5,14 +5,27 @@ end
|
|
5
5
|
|
6
6
|
class Complex
|
7
7
|
|
8
|
-
#
|
9
|
-
# value <tt>i</tt>, to a Complex object.
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
Complex(object['r'], object['i'])
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
15
|
-
# object
|
13
|
+
# Methods <tt>Complex#as_json</tt> and +Complex.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Complex object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Complex#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/complex'
|
21
|
+
# x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0}
|
22
|
+
# y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Complex object:
|
25
|
+
#
|
26
|
+
# Complex.json_create(x) # => (2+0i)
|
27
|
+
# Complex.json_create(y) # => (2.0+4i)
|
28
|
+
#
|
16
29
|
def as_json(*)
|
17
30
|
{
|
18
31
|
JSON.create_id => self.class.name,
|
@@ -21,7 +34,17 @@ class Complex
|
|
21
34
|
}
|
22
35
|
end
|
23
36
|
|
24
|
-
#
|
37
|
+
# Returns a JSON string representing +self+:
|
38
|
+
#
|
39
|
+
# require 'json/add/complex'
|
40
|
+
# puts Complex(2).to_json
|
41
|
+
# puts Complex(2.0, 4).to_json
|
42
|
+
#
|
43
|
+
# Output:
|
44
|
+
#
|
45
|
+
# {"json_class":"Complex","r":2,"i":0}
|
46
|
+
# {"json_class":"Complex","r":2.0,"i":4}
|
47
|
+
#
|
25
48
|
def to_json(*args)
|
26
49
|
as_json.to_json(*args)
|
27
50
|
end
|
data/lib/json/add/date.rb
CHANGED
@@ -6,16 +6,29 @@ require 'date'
|
|
6
6
|
|
7
7
|
class Date
|
8
8
|
|
9
|
-
#
|
10
|
-
# <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
|
9
|
+
# See #as_json.
|
11
10
|
def self.json_create(object)
|
12
11
|
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
13
12
|
end
|
14
13
|
|
15
14
|
alias start sg unless method_defined?(:start)
|
16
15
|
|
17
|
-
#
|
18
|
-
# object
|
16
|
+
# Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used
|
17
|
+
# to serialize and deserialize a \Date object;
|
18
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
19
|
+
#
|
20
|
+
# \Method <tt>Date#as_json</tt> serializes +self+,
|
21
|
+
# returning a 2-element hash representing +self+:
|
22
|
+
#
|
23
|
+
# require 'json/add/date'
|
24
|
+
# x = Date.today.as_json
|
25
|
+
# # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
|
26
|
+
#
|
27
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Date object:
|
28
|
+
#
|
29
|
+
# Date.json_create(x)
|
30
|
+
# # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
|
31
|
+
#
|
19
32
|
def as_json(*)
|
20
33
|
{
|
21
34
|
JSON.create_id => self.class.name,
|
@@ -26,8 +39,15 @@ class Date
|
|
26
39
|
}
|
27
40
|
end
|
28
41
|
|
29
|
-
#
|
30
|
-
#
|
42
|
+
# Returns a JSON string representing +self+:
|
43
|
+
#
|
44
|
+
# require 'json/add/date'
|
45
|
+
# puts Date.today.to_json
|
46
|
+
#
|
47
|
+
# Output:
|
48
|
+
#
|
49
|
+
# {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
|
50
|
+
#
|
31
51
|
def to_json(*args)
|
32
52
|
as_json.to_json(*args)
|
33
53
|
end
|
data/lib/json/add/date_time.rb
CHANGED
@@ -6,9 +6,7 @@ require 'date'
|
|
6
6
|
|
7
7
|
class DateTime
|
8
8
|
|
9
|
-
#
|
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.
|
9
|
+
# See #as_json.
|
12
10
|
def self.json_create(object)
|
13
11
|
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
14
12
|
of_a, of_b = object['of'].split('/')
|
@@ -23,8 +21,21 @@ class DateTime
|
|
23
21
|
|
24
22
|
alias start sg unless method_defined?(:start)
|
25
23
|
|
26
|
-
#
|
27
|
-
# object
|
24
|
+
# Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used
|
25
|
+
# to serialize and deserialize a \DateTime object;
|
26
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
27
|
+
#
|
28
|
+
# \Method <tt>DateTime#as_json</tt> serializes +self+,
|
29
|
+
# returning a 2-element hash representing +self+:
|
30
|
+
#
|
31
|
+
# require 'json/add/datetime'
|
32
|
+
# x = DateTime.now.as_json
|
33
|
+
# # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
|
34
|
+
#
|
35
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \DateTime object:
|
36
|
+
#
|
37
|
+
# DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
|
38
|
+
#
|
28
39
|
def as_json(*)
|
29
40
|
{
|
30
41
|
JSON.create_id => self.class.name,
|
@@ -39,9 +50,15 @@ class DateTime
|
|
39
50
|
}
|
40
51
|
end
|
41
52
|
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
53
|
+
# Returns a JSON string representing +self+:
|
54
|
+
#
|
55
|
+
# require 'json/add/datetime'
|
56
|
+
# puts DateTime.now.to_json
|
57
|
+
#
|
58
|
+
# Output:
|
59
|
+
#
|
60
|
+
# {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
|
61
|
+
#
|
45
62
|
def to_json(*args)
|
46
63
|
as_json.to_json(*args)
|
47
64
|
end
|
data/lib/json/add/exception.rb
CHANGED
@@ -5,16 +5,27 @@ end
|
|
5
5
|
|
6
6
|
class Exception
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
result = new(object['m'])
|
12
11
|
result.set_backtrace object['b']
|
13
12
|
result
|
14
13
|
end
|
15
14
|
|
16
|
-
#
|
17
|
-
# object
|
15
|
+
# Methods <tt>Exception#as_json</tt> and +Exception.json_create+ may be used
|
16
|
+
# to serialize and deserialize a \Exception object;
|
17
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
18
|
+
#
|
19
|
+
# \Method <tt>Exception#as_json</tt> serializes +self+,
|
20
|
+
# returning a 2-element hash representing +self+:
|
21
|
+
#
|
22
|
+
# require 'json/add/exception'
|
23
|
+
# x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil}
|
24
|
+
#
|
25
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Exception object:
|
26
|
+
#
|
27
|
+
# Exception.json_create(x) # => #<Exception: Foo>
|
28
|
+
#
|
18
29
|
def as_json(*)
|
19
30
|
{
|
20
31
|
JSON.create_id => self.class.name,
|
@@ -23,8 +34,15 @@ class Exception
|
|
23
34
|
}
|
24
35
|
end
|
25
36
|
|
26
|
-
#
|
27
|
-
#
|
37
|
+
# Returns a JSON string representing +self+:
|
38
|
+
#
|
39
|
+
# require 'json/add/exception'
|
40
|
+
# puts Exception.new('Foo').to_json
|
41
|
+
#
|
42
|
+
# Output:
|
43
|
+
#
|
44
|
+
# {"json_class":"Exception","m":"Foo","b":null}
|
45
|
+
#
|
28
46
|
def to_json(*args)
|
29
47
|
as_json.to_json(*args)
|
30
48
|
end
|
data/lib/json/add/ostruct.rb
CHANGED
@@ -6,14 +6,27 @@ require 'ostruct'
|
|
6
6
|
|
7
7
|
class OpenStruct
|
8
8
|
|
9
|
-
#
|
10
|
-
# <tt>t</tt> serialized by <tt>to_json</tt>.
|
9
|
+
# See #as_json.
|
11
10
|
def self.json_create(object)
|
12
11
|
new(object['t'] || object[:t])
|
13
12
|
end
|
14
13
|
|
15
|
-
#
|
16
|
-
# object
|
14
|
+
# Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used
|
15
|
+
# to serialize and deserialize a \OpenStruct object;
|
16
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
17
|
+
#
|
18
|
+
# \Method <tt>OpenStruct#as_json</tt> serializes +self+,
|
19
|
+
# returning a 2-element hash representing +self+:
|
20
|
+
#
|
21
|
+
# require 'json/add/ostruct'
|
22
|
+
# x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
|
23
|
+
# # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
|
24
|
+
#
|
25
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object:
|
26
|
+
#
|
27
|
+
# OpenStruct.json_create(x)
|
28
|
+
# # => #<OpenStruct name='Rowdy', age=nil>
|
29
|
+
#
|
17
30
|
def as_json(*)
|
18
31
|
klass = self.class.name
|
19
32
|
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
@@ -23,8 +36,15 @@ class OpenStruct
|
|
23
36
|
}
|
24
37
|
end
|
25
38
|
|
26
|
-
#
|
27
|
-
#
|
39
|
+
# Returns a JSON string representing +self+:
|
40
|
+
#
|
41
|
+
# require 'json/add/ostruct'
|
42
|
+
# puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
|
43
|
+
#
|
44
|
+
# Output:
|
45
|
+
#
|
46
|
+
# {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
|
47
|
+
#
|
28
48
|
def to_json(*args)
|
29
49
|
as_json.to_json(*args)
|
30
50
|
end
|
data/lib/json/add/range.rb
CHANGED
@@ -5,14 +5,29 @@ end
|
|
5
5
|
|
6
6
|
class Range
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>a</tt> serialized by <tt>to_json</tt>.
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
new(*object['a'])
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
15
|
-
# object
|
13
|
+
# Methods <tt>Range#as_json</tt> and +Range.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Range object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Range#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/range'
|
21
|
+
# x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
|
22
|
+
# y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
|
23
|
+
# z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
|
24
|
+
#
|
25
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Range object:
|
26
|
+
#
|
27
|
+
# Range.json_create(x) # => 1..4
|
28
|
+
# Range.json_create(y) # => 1...4
|
29
|
+
# Range.json_create(z) # => "a".."d"
|
30
|
+
#
|
16
31
|
def as_json(*)
|
17
32
|
{
|
18
33
|
JSON.create_id => self.class.name,
|
@@ -20,9 +35,19 @@ class Range
|
|
20
35
|
}
|
21
36
|
end
|
22
37
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
38
|
+
# Returns a JSON string representing +self+:
|
39
|
+
#
|
40
|
+
# require 'json/add/range'
|
41
|
+
# puts (1..4).to_json
|
42
|
+
# puts (1...4).to_json
|
43
|
+
# puts ('a'..'d').to_json
|
44
|
+
#
|
45
|
+
# Output:
|
46
|
+
#
|
47
|
+
# {"json_class":"Range","a":[1,4,false]}
|
48
|
+
# {"json_class":"Range","a":[1,4,true]}
|
49
|
+
# {"json_class":"Range","a":["a","d",false]}
|
50
|
+
#
|
26
51
|
def to_json(*args)
|
27
52
|
as_json.to_json(*args)
|
28
53
|
end
|
data/lib/json/add/rational.rb
CHANGED
@@ -4,14 +4,28 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class Rational
|
7
|
-
|
8
|
-
#
|
7
|
+
|
8
|
+
# See #as_json.
|
9
9
|
def self.json_create(object)
|
10
10
|
Rational(object['n'], object['d'])
|
11
11
|
end
|
12
12
|
|
13
|
-
#
|
14
|
-
# object
|
13
|
+
# Methods <tt>Rational#as_json</tt> and +Rational.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Rational object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Rational#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/rational'
|
21
|
+
# x = Rational(2, 3).as_json
|
22
|
+
# # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Rational object:
|
25
|
+
#
|
26
|
+
# Rational.json_create(x)
|
27
|
+
# # => (2/3)
|
28
|
+
#
|
15
29
|
def as_json(*)
|
16
30
|
{
|
17
31
|
JSON.create_id => self.class.name,
|
@@ -20,7 +34,15 @@ class Rational
|
|
20
34
|
}
|
21
35
|
end
|
22
36
|
|
23
|
-
#
|
37
|
+
# Returns a JSON string representing +self+:
|
38
|
+
#
|
39
|
+
# require 'json/add/rational'
|
40
|
+
# puts Rational(2, 3).to_json
|
41
|
+
#
|
42
|
+
# Output:
|
43
|
+
#
|
44
|
+
# {"json_class":"Rational","n":2,"d":3}
|
45
|
+
#
|
24
46
|
def to_json(*args)
|
25
47
|
as_json.to_json(*args)
|
26
48
|
end
|
data/lib/json/add/regexp.rb
CHANGED
@@ -5,15 +5,26 @@ end
|
|
5
5
|
|
6
6
|
class Regexp
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by
|
10
|
-
# <tt>to_json</tt>
|
8
|
+
# See #as_json.
|
11
9
|
def self.json_create(object)
|
12
10
|
new(object['s'], object['o'])
|
13
11
|
end
|
14
12
|
|
15
|
-
#
|
16
|
-
# object
|
13
|
+
# Methods <tt>Regexp#as_json</tt> and +Regexp.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Regexp object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Regexp#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/regexp'
|
21
|
+
# x = /foo/.as_json
|
22
|
+
# # => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Regexp object:
|
25
|
+
#
|
26
|
+
# Regexp.json_create(x) # => /foo/
|
27
|
+
#
|
17
28
|
def as_json(*)
|
18
29
|
{
|
19
30
|
JSON.create_id => self.class.name,
|
@@ -22,8 +33,15 @@ class Regexp
|
|
22
33
|
}
|
23
34
|
end
|
24
35
|
|
25
|
-
#
|
26
|
-
#
|
36
|
+
# Returns a JSON string representing +self+:
|
37
|
+
#
|
38
|
+
# require 'json/add/regexp'
|
39
|
+
# puts /foo/.to_json
|
40
|
+
#
|
41
|
+
# Output:
|
42
|
+
#
|
43
|
+
# {"json_class":"Regexp","o":0,"s":"foo"}
|
44
|
+
#
|
27
45
|
def to_json(*args)
|
28
46
|
as_json.to_json(*args)
|
29
47
|
end
|
data/lib/json/add/set.rb
CHANGED
@@ -4,16 +4,27 @@ end
|
|
4
4
|
defined?(::Set) or require 'set'
|
5
5
|
|
6
6
|
class Set
|
7
|
-
|
8
|
-
#
|
9
|
-
# method used for JSON marshalling support.
|
7
|
+
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
new object['a']
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
13
|
+
# Methods <tt>Set#as_json</tt> and +Set.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Set object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Set#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/set'
|
21
|
+
# x = Set.new(%w/foo bar baz/).as_json
|
22
|
+
# # => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Set object:
|
25
|
+
#
|
26
|
+
# Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}>
|
15
27
|
#
|
16
|
-
# method used for JSON marshalling support.
|
17
28
|
def as_json(*)
|
18
29
|
{
|
19
30
|
JSON.create_id => self.class.name,
|
@@ -21,7 +32,15 @@ class Set
|
|
21
32
|
}
|
22
33
|
end
|
23
34
|
|
24
|
-
#
|
35
|
+
# Returns a JSON string representing +self+:
|
36
|
+
#
|
37
|
+
# require 'json/add/set'
|
38
|
+
# puts Set.new(%w/foo bar baz/).to_json
|
39
|
+
#
|
40
|
+
# Output:
|
41
|
+
#
|
42
|
+
# {"json_class":"Set","a":["foo","bar","baz"]}
|
43
|
+
#
|
25
44
|
def to_json(*args)
|
26
45
|
as_json.to_json(*args)
|
27
46
|
end
|
data/lib/json/add/struct.rb
CHANGED
@@ -5,14 +5,28 @@ end
|
|
5
5
|
|
6
6
|
class Struct
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>v</tt> serialized by <tt>to_json</tt>.
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
new(*object['v'])
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
15
|
-
# object
|
13
|
+
# Methods <tt>Struct#as_json</tt> and +Struct.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Struct object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Struct#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/struct'
|
21
|
+
# Customer = Struct.new('Customer', :name, :address, :zip)
|
22
|
+
# x = Struct::Customer.new.as_json
|
23
|
+
# # => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}
|
24
|
+
#
|
25
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Struct object:
|
26
|
+
#
|
27
|
+
# Struct::Customer.json_create(x)
|
28
|
+
# # => #<struct Struct::Customer name=nil, address=nil, zip=nil>
|
29
|
+
#
|
16
30
|
def as_json(*)
|
17
31
|
klass = self.class.name
|
18
32
|
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
@@ -22,8 +36,16 @@ class Struct
|
|
22
36
|
}
|
23
37
|
end
|
24
38
|
|
25
|
-
#
|
26
|
-
#
|
39
|
+
# Returns a JSON string representing +self+:
|
40
|
+
#
|
41
|
+
# require 'json/add/struct'
|
42
|
+
# Customer = Struct.new('Customer', :name, :address, :zip)
|
43
|
+
# puts Struct::Customer.new.to_json
|
44
|
+
#
|
45
|
+
# Output:
|
46
|
+
#
|
47
|
+
# {"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
|
48
|
+
#
|
27
49
|
def to_json(*args)
|
28
50
|
as_json.to_json(*args)
|
29
51
|
end
|
data/lib/json/add/symbol.rb
CHANGED
@@ -1,11 +1,26 @@
|
|
1
|
+
|
1
2
|
#frozen_string_literal: false
|
2
3
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
3
4
|
require 'json'
|
4
5
|
end
|
5
6
|
|
6
7
|
class Symbol
|
7
|
-
|
8
|
-
#
|
8
|
+
|
9
|
+
# Methods <tt>Symbol#as_json</tt> and +Symbol.json_create+ may be used
|
10
|
+
# to serialize and deserialize a \Symbol object;
|
11
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
12
|
+
#
|
13
|
+
# \Method <tt>Symbol#as_json</tt> serializes +self+,
|
14
|
+
# returning a 2-element hash representing +self+:
|
15
|
+
#
|
16
|
+
# require 'json/add/symbol'
|
17
|
+
# x = :foo.as_json
|
18
|
+
# # => {"json_class"=>"Symbol", "s"=>"foo"}
|
19
|
+
#
|
20
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Symbol object:
|
21
|
+
#
|
22
|
+
# Symbol.json_create(x) # => :foo
|
23
|
+
#
|
9
24
|
def as_json(*)
|
10
25
|
{
|
11
26
|
JSON.create_id => self.class.name,
|
@@ -13,12 +28,20 @@ class Symbol
|
|
13
28
|
}
|
14
29
|
end
|
15
30
|
|
16
|
-
#
|
31
|
+
# Returns a JSON string representing +self+:
|
32
|
+
#
|
33
|
+
# require 'json/add/symbol'
|
34
|
+
# puts :foo.to_json
|
35
|
+
#
|
36
|
+
# Output:
|
37
|
+
#
|
38
|
+
# # {"json_class":"Symbol","s":"foo"}
|
39
|
+
#
|
17
40
|
def to_json(*a)
|
18
41
|
as_json.to_json(*a)
|
19
42
|
end
|
20
43
|
|
21
|
-
#
|
44
|
+
# See #as_json.
|
22
45
|
def self.json_create(o)
|
23
46
|
o['s'].to_sym
|
24
47
|
end
|
data/lib/json/add/time.rb
CHANGED
@@ -5,7 +5,7 @@ end
|
|
5
5
|
|
6
6
|
class Time
|
7
7
|
|
8
|
-
#
|
8
|
+
# See #as_json.
|
9
9
|
def self.json_create(object)
|
10
10
|
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
|
11
11
|
object['n'] = usec * 1000
|
@@ -17,8 +17,22 @@ class Time
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
#
|
21
|
-
# object
|
20
|
+
# Methods <tt>Time#as_json</tt> and +Time.json_create+ may be used
|
21
|
+
# to serialize and deserialize a \Time object;
|
22
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
23
|
+
#
|
24
|
+
# \Method <tt>Time#as_json</tt> serializes +self+,
|
25
|
+
# returning a 2-element hash representing +self+:
|
26
|
+
#
|
27
|
+
# require 'json/add/time'
|
28
|
+
# x = Time.now.as_json
|
29
|
+
# # => {"json_class"=>"Time", "s"=>1700931656, "n"=>472846644}
|
30
|
+
#
|
31
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Time object:
|
32
|
+
#
|
33
|
+
# Time.json_create(x)
|
34
|
+
# # => 2023-11-25 11:00:56.472846644 -0600
|
35
|
+
#
|
22
36
|
def as_json(*)
|
23
37
|
nanoseconds = [ tv_usec * 1000 ]
|
24
38
|
respond_to?(:tv_nsec) and nanoseconds << tv_nsec
|
@@ -30,8 +44,15 @@ class Time
|
|
30
44
|
}
|
31
45
|
end
|
32
46
|
|
33
|
-
#
|
34
|
-
#
|
47
|
+
# Returns a JSON string representing +self+:
|
48
|
+
#
|
49
|
+
# require 'json/add/time'
|
50
|
+
# puts Time.now.to_json
|
51
|
+
#
|
52
|
+
# Output:
|
53
|
+
#
|
54
|
+
# {"json_class":"Time","s":1700931678,"n":980650786}
|
55
|
+
#
|
35
56
|
def to_json(*args)
|
36
57
|
as_json.to_json(*args)
|
37
58
|
end
|
data/lib/json/common.rb
CHANGED
@@ -3,6 +3,9 @@ require 'json/version'
|
|
3
3
|
require 'json/generic_object'
|
4
4
|
|
5
5
|
module JSON
|
6
|
+
NOT_SET = Object.new.freeze
|
7
|
+
private_constant :NOT_SET
|
8
|
+
|
6
9
|
class << self
|
7
10
|
# :call-seq:
|
8
11
|
# JSON[object] -> new_array or new_string
|
@@ -295,19 +298,9 @@ module JSON
|
|
295
298
|
#
|
296
299
|
def generate(obj, opts = nil)
|
297
300
|
if State === opts
|
298
|
-
state
|
301
|
+
state = opts
|
299
302
|
else
|
300
|
-
state = State.new
|
301
|
-
end
|
302
|
-
if opts
|
303
|
-
if opts.respond_to? :to_hash
|
304
|
-
opts = opts.to_hash
|
305
|
-
elsif opts.respond_to? :to_h
|
306
|
-
opts = opts.to_h
|
307
|
-
else
|
308
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
309
|
-
end
|
310
|
-
state = state.configure(opts)
|
303
|
+
state = State.new(opts)
|
311
304
|
end
|
312
305
|
state.generate(obj)
|
313
306
|
end
|
@@ -334,19 +327,9 @@ module JSON
|
|
334
327
|
# JSON.fast_generate(a)
|
335
328
|
def fast_generate(obj, opts = nil)
|
336
329
|
if State === opts
|
337
|
-
state
|
330
|
+
state = opts
|
338
331
|
else
|
339
|
-
state = JSON.create_fast_state
|
340
|
-
end
|
341
|
-
if opts
|
342
|
-
if opts.respond_to? :to_hash
|
343
|
-
opts = opts.to_hash
|
344
|
-
elsif opts.respond_to? :to_h
|
345
|
-
opts = opts.to_h
|
346
|
-
else
|
347
|
-
raise TypeError, "can't convert #{opts.class} into Hash"
|
348
|
-
end
|
349
|
-
state.configure(opts)
|
332
|
+
state = JSON.create_fast_state.configure(opts)
|
350
333
|
end
|
351
334
|
state.generate(obj)
|
352
335
|
end
|
@@ -592,13 +575,13 @@ module JSON
|
|
592
575
|
# Sets or returns the default options for the JSON.dump method.
|
593
576
|
# Initially:
|
594
577
|
# opts = JSON.dump_default_options
|
595
|
-
# opts # => {:max_nesting=>false, :allow_nan=>true, :
|
578
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :script_safe=>false}
|
596
579
|
attr_accessor :dump_default_options
|
597
580
|
end
|
598
581
|
self.dump_default_options = {
|
599
582
|
:max_nesting => false,
|
600
583
|
:allow_nan => true,
|
601
|
-
:
|
584
|
+
:script_safe => false,
|
602
585
|
}
|
603
586
|
|
604
587
|
# :call-seq:
|
@@ -628,16 +611,18 @@ module JSON
|
|
628
611
|
# puts File.read(path)
|
629
612
|
# Output:
|
630
613
|
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
|
631
|
-
def dump(obj, anIO = nil, limit = nil)
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
614
|
+
def dump(obj, anIO = nil, limit = nil, kwargs = nil)
|
615
|
+
io_limit_opt = [anIO, limit, kwargs].compact
|
616
|
+
kwargs = io_limit_opt.pop if io_limit_opt.last.is_a?(Hash)
|
617
|
+
anIO, limit = io_limit_opt
|
618
|
+
if anIO.respond_to?(:to_io)
|
619
|
+
anIO = anIO.to_io
|
620
|
+
elsif limit.nil? && !anIO.respond_to?(:write)
|
621
|
+
anIO, limit = nil, anIO
|
638
622
|
end
|
639
623
|
opts = JSON.dump_default_options
|
640
624
|
opts = opts.merge(:max_nesting => limit) if limit
|
625
|
+
opts = merge_dump_options(opts, **kwargs) if kwargs
|
641
626
|
result = generate(obj, opts)
|
642
627
|
if anIO
|
643
628
|
anIO.write result
|
@@ -653,6 +638,15 @@ module JSON
|
|
653
638
|
def self.iconv(to, from, string)
|
654
639
|
string.encode(to, from)
|
655
640
|
end
|
641
|
+
|
642
|
+
def merge_dump_options(opts, strict: NOT_SET)
|
643
|
+
opts = opts.merge(strict: strict) if NOT_SET != strict
|
644
|
+
opts
|
645
|
+
end
|
646
|
+
|
647
|
+
class << self
|
648
|
+
private :merge_dump_options
|
649
|
+
end
|
656
650
|
end
|
657
651
|
|
658
652
|
module ::Kernel
|
data/lib/json/pure/generator.rb
CHANGED
@@ -37,25 +37,34 @@ module JSON
|
|
37
37
|
'\\' => '\\\\',
|
38
38
|
} # :nodoc:
|
39
39
|
|
40
|
-
|
40
|
+
ESCAPE_PATTERN = /[\/"\\\x0-\x1f]/n # :nodoc:
|
41
|
+
|
42
|
+
SCRIPT_SAFE_MAP = MAP.merge(
|
41
43
|
'/' => '\\/',
|
44
|
+
"\u2028".b => '\u2028',
|
45
|
+
"\u2029".b => '\u2029',
|
42
46
|
)
|
43
47
|
|
48
|
+
SCRIPT_SAFE_ESCAPE_PATTERN = Regexp.union(ESCAPE_PATTERN, "\u2028".b, "\u2029".b)
|
49
|
+
|
44
50
|
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
|
45
51
|
# UTF16 big endian characters as \u????, and return it.
|
46
|
-
def utf8_to_json(string,
|
52
|
+
def utf8_to_json(string, script_safe = false) # :nodoc:
|
47
53
|
string = string.dup
|
48
54
|
string.force_encoding(::Encoding::ASCII_8BIT)
|
49
|
-
|
50
|
-
|
55
|
+
if script_safe
|
56
|
+
string.gsub!(SCRIPT_SAFE_ESCAPE_PATTERN) { SCRIPT_SAFE_MAP[$&] || $& }
|
57
|
+
else
|
58
|
+
string.gsub!(ESCAPE_PATTERN) { MAP[$&] || $& }
|
59
|
+
end
|
51
60
|
string.force_encoding(::Encoding::UTF_8)
|
52
61
|
string
|
53
62
|
end
|
54
63
|
|
55
|
-
def utf8_to_json_ascii(string,
|
64
|
+
def utf8_to_json_ascii(string, script_safe = false) # :nodoc:
|
56
65
|
string = string.dup
|
57
66
|
string.force_encoding(::Encoding::ASCII_8BIT)
|
58
|
-
map =
|
67
|
+
map = script_safe ? SCRIPT_SAFE_MAP : MAP
|
59
68
|
string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& }
|
60
69
|
string.gsub!(/(
|
61
70
|
(?:
|
@@ -115,7 +124,8 @@ module JSON
|
|
115
124
|
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
116
125
|
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
117
126
|
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
118
|
-
# * *
|
127
|
+
# * *script_safe*: true if U+2028, U+2029 and forward slash (/) should be escaped
|
128
|
+
# as to make the JSON object safe to interpolate in a script tag (default: false).
|
119
129
|
# * *check_circular*: is deprecated now, use the :max_nesting option instead,
|
120
130
|
# * *max_nesting*: sets the maximum level of data structure nesting in
|
121
131
|
# the generated JSON, max_nesting = 0 if no maximum should be checked.
|
@@ -130,7 +140,8 @@ module JSON
|
|
130
140
|
@array_nl = ''
|
131
141
|
@allow_nan = false
|
132
142
|
@ascii_only = false
|
133
|
-
@
|
143
|
+
@script_safe = false
|
144
|
+
@strict = false
|
134
145
|
@buffer_initial_length = 1024
|
135
146
|
configure opts
|
136
147
|
end
|
@@ -158,7 +169,11 @@ module JSON
|
|
158
169
|
|
159
170
|
# If this attribute is set to true, forward slashes will be escaped in
|
160
171
|
# all json strings.
|
161
|
-
attr_accessor :
|
172
|
+
attr_accessor :script_safe
|
173
|
+
|
174
|
+
# If this attribute is set to true, attempting to serialize types not
|
175
|
+
# supported by the JSON spec will raise a JSON::GeneratorError
|
176
|
+
attr_accessor :strict
|
162
177
|
|
163
178
|
# :stopdoc:
|
164
179
|
attr_reader :buffer_initial_length
|
@@ -200,8 +215,13 @@ module JSON
|
|
200
215
|
end
|
201
216
|
|
202
217
|
# Returns true, if forward slashes are escaped. Otherwise returns false.
|
203
|
-
def
|
204
|
-
@
|
218
|
+
def script_safe?
|
219
|
+
@script_safe
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns true, if forward slashes are escaped. Otherwise returns false.
|
223
|
+
def strict?
|
224
|
+
@strict
|
205
225
|
end
|
206
226
|
|
207
227
|
# Configure this State instance with the Hash _opts_, and return
|
@@ -214,7 +234,7 @@ module JSON
|
|
214
234
|
else
|
215
235
|
raise TypeError, "can't convert #{opts.class} into Hash"
|
216
236
|
end
|
217
|
-
|
237
|
+
opts.each do |key, value|
|
218
238
|
instance_variable_set "@#{key}", value
|
219
239
|
end
|
220
240
|
@indent = opts[:indent] if opts.key?(:indent)
|
@@ -226,7 +246,16 @@ module JSON
|
|
226
246
|
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
|
227
247
|
@depth = opts[:depth] || 0
|
228
248
|
@buffer_initial_length ||= opts[:buffer_initial_length]
|
229
|
-
|
249
|
+
|
250
|
+
@script_safe = if opts.key?(:script_safe)
|
251
|
+
!!opts[:script_safe]
|
252
|
+
elsif opts.key?(:escape_slash)
|
253
|
+
!!opts[:escape_slash]
|
254
|
+
else
|
255
|
+
false
|
256
|
+
end
|
257
|
+
|
258
|
+
@strict = !!opts[:strict] if opts.key?(:strict)
|
230
259
|
|
231
260
|
if !opts.key?(:max_nesting) # defaults to 100
|
232
261
|
@max_nesting = 100
|
@@ -243,7 +272,7 @@ module JSON
|
|
243
272
|
# passed to the configure method.
|
244
273
|
def to_h
|
245
274
|
result = {}
|
246
|
-
|
275
|
+
instance_variables.each do |iv|
|
247
276
|
iv = iv.to_s[1..-1]
|
248
277
|
result[iv.to_sym] = self[iv]
|
249
278
|
end
|
@@ -287,7 +316,13 @@ module JSON
|
|
287
316
|
# Converts this object to a string (calling #to_s), converts
|
288
317
|
# it to a JSON string, and returns the result. This is a fallback, if no
|
289
318
|
# special method #to_json was defined for some object.
|
290
|
-
def to_json(
|
319
|
+
def to_json(generator_state)
|
320
|
+
if generator_state.strict?
|
321
|
+
raise GeneratorError, "#{self.class} not allowed in JSON"
|
322
|
+
else
|
323
|
+
to_s.to_json
|
324
|
+
end
|
325
|
+
end
|
291
326
|
end
|
292
327
|
|
293
328
|
module Hash
|
@@ -310,21 +345,18 @@ module JSON
|
|
310
345
|
end
|
311
346
|
|
312
347
|
def json_transform(state)
|
313
|
-
delim =
|
314
|
-
|
315
|
-
result = '{'
|
316
|
-
result << state.object_nl
|
348
|
+
delim = ",#{state.object_nl}"
|
349
|
+
result = "{#{state.object_nl}"
|
317
350
|
depth = state.depth += 1
|
318
351
|
first = true
|
319
352
|
indent = !state.object_nl.empty?
|
320
|
-
each { |key,value|
|
353
|
+
each { |key, value|
|
321
354
|
result << delim unless first
|
322
355
|
result << state.indent * depth if indent
|
323
|
-
result
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
if value.respond_to?(:to_json)
|
356
|
+
result = "#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}"
|
357
|
+
if state.strict?
|
358
|
+
raise GeneratorError, "#{value.class} not allowed in JSON"
|
359
|
+
elsif value.respond_to?(:to_json)
|
328
360
|
result << value.to_json(state)
|
329
361
|
else
|
330
362
|
result << %{"#{String(value)}"}
|
@@ -365,7 +397,9 @@ module JSON
|
|
365
397
|
each { |value|
|
366
398
|
result << delim unless first
|
367
399
|
result << state.indent * depth if indent
|
368
|
-
if
|
400
|
+
if state.strict?
|
401
|
+
raise GeneratorError, "#{value.class} not allowed in JSON"
|
402
|
+
elsif value.respond_to?(:to_json)
|
369
403
|
result << value.to_json(state)
|
370
404
|
else
|
371
405
|
result << %{"#{String(value)}"}
|
@@ -419,9 +453,9 @@ module JSON
|
|
419
453
|
string = encode(::Encoding::UTF_8)
|
420
454
|
end
|
421
455
|
if state.ascii_only?
|
422
|
-
'"' << JSON.utf8_to_json_ascii(string, state.
|
456
|
+
'"' << JSON.utf8_to_json_ascii(string, state.script_safe) << '"'
|
423
457
|
else
|
424
|
-
'"' << JSON.utf8_to_json(string, state.
|
458
|
+
'"' << JSON.utf8_to_json(string, state.script_safe) << '"'
|
425
459
|
end
|
426
460
|
end
|
427
461
|
|
data/lib/json/version.rb
CHANGED
data/lib/json.rb
CHANGED
@@ -285,6 +285,15 @@ require 'json/common'
|
|
285
285
|
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
286
286
|
# JSON.generate(obj, max_nesting: 2)
|
287
287
|
#
|
288
|
+
# ====== Escaping Options
|
289
|
+
#
|
290
|
+
# Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
|
291
|
+
# and <tt>'/'</tt> should be escaped as to make the JSON object safe to interpolate in script
|
292
|
+
# tags.
|
293
|
+
#
|
294
|
+
# Options +ascii_only+ (boolean) specifies wether all characters outside the ASCII range
|
295
|
+
# should be escaped.
|
296
|
+
#
|
288
297
|
# ====== Output Options
|
289
298
|
#
|
290
299
|
# The default formatting options generate the most compact
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_pure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Frank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This is a JSON implementation in pure Ruby.
|
14
14
|
email: flori@ping.de
|
@@ -20,7 +20,6 @@ files:
|
|
20
20
|
- CHANGES.md
|
21
21
|
- LICENSE
|
22
22
|
- README.md
|
23
|
-
- VERSION
|
24
23
|
- json_pure.gemspec
|
25
24
|
- lib/json.rb
|
26
25
|
- lib/json/add/bigdecimal.rb
|
@@ -44,14 +43,14 @@ files:
|
|
44
43
|
- lib/json/pure/generator.rb
|
45
44
|
- lib/json/pure/parser.rb
|
46
45
|
- lib/json/version.rb
|
47
|
-
homepage:
|
46
|
+
homepage: https://flori.github.io/json
|
48
47
|
licenses:
|
49
48
|
- Ruby
|
50
49
|
metadata:
|
51
50
|
bug_tracker_uri: https://github.com/flori/json/issues
|
52
51
|
changelog_uri: https://github.com/flori/json/blob/master/CHANGES.md
|
53
|
-
documentation_uri:
|
54
|
-
homepage_uri:
|
52
|
+
documentation_uri: https://flori.github.io/json/doc/index.html
|
53
|
+
homepage_uri: https://flori.github.io/json
|
55
54
|
source_code_uri: https://github.com/flori/json
|
56
55
|
wiki_uri: https://github.com/flori/json/wiki
|
57
56
|
post_install_message:
|
@@ -73,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
72
|
- !ruby/object:Gem::Version
|
74
73
|
version: '0'
|
75
74
|
requirements: []
|
76
|
-
rubygems_version: 3.
|
75
|
+
rubygems_version: 3.5.0.dev
|
77
76
|
signing_key:
|
78
77
|
specification_version: 4
|
79
78
|
summary: JSON Implementation for Ruby
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.6.3
|