json 1.8.6-java → 2.11.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/BSDL +22 -0
- data/CHANGES.md +619 -0
- data/COPYING +56 -0
- data/LEGAL +8 -0
- data/README.md +268 -0
- data/json.gemspec +63 -0
- data/lib/json/add/bigdecimal.rb +41 -11
- data/lib/json/add/complex.rb +32 -9
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/date.rb +27 -7
- data/lib/json/add/date_time.rb +26 -9
- data/lib/json/add/exception.rb +25 -7
- data/lib/json/add/ostruct.rb +32 -9
- data/lib/json/add/range.rb +33 -8
- data/lib/json/add/rational.rb +30 -8
- data/lib/json/add/regexp.rb +28 -10
- data/lib/json/add/set.rb +48 -0
- data/lib/json/add/struct.rb +29 -7
- data/lib/json/add/symbol.rb +34 -7
- data/lib/json/add/time.rb +29 -15
- data/lib/json/common.rb +916 -316
- data/lib/json/ext/generator/state.rb +106 -0
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/ext.rb +34 -10
- data/lib/json/generic_object.rb +11 -6
- data/lib/json/truffle_ruby/generator.rb +690 -0
- data/lib/json/version.rb +3 -6
- data/lib/json.rb +560 -35
- metadata +29 -82
- data/lib/json/pure/generator.rb +0 -530
- data/lib/json/pure/parser.rb +0 -359
- data/lib/json/pure.rb +0 -21
- data/tests/fixtures/fail1.json +0 -1
- data/tests/fixtures/fail10.json +0 -1
- data/tests/fixtures/fail11.json +0 -1
- data/tests/fixtures/fail12.json +0 -1
- data/tests/fixtures/fail13.json +0 -1
- data/tests/fixtures/fail14.json +0 -1
- data/tests/fixtures/fail18.json +0 -1
- data/tests/fixtures/fail19.json +0 -1
- data/tests/fixtures/fail2.json +0 -1
- data/tests/fixtures/fail20.json +0 -1
- data/tests/fixtures/fail21.json +0 -1
- data/tests/fixtures/fail22.json +0 -1
- data/tests/fixtures/fail23.json +0 -1
- data/tests/fixtures/fail24.json +0 -1
- data/tests/fixtures/fail25.json +0 -1
- data/tests/fixtures/fail27.json +0 -2
- data/tests/fixtures/fail28.json +0 -2
- data/tests/fixtures/fail3.json +0 -1
- data/tests/fixtures/fail4.json +0 -1
- data/tests/fixtures/fail5.json +0 -1
- data/tests/fixtures/fail6.json +0 -1
- data/tests/fixtures/fail7.json +0 -1
- data/tests/fixtures/fail8.json +0 -1
- data/tests/fixtures/fail9.json +0 -1
- data/tests/fixtures/pass1.json +0 -56
- data/tests/fixtures/pass15.json +0 -1
- data/tests/fixtures/pass16.json +0 -1
- data/tests/fixtures/pass17.json +0 -1
- data/tests/fixtures/pass2.json +0 -1
- data/tests/fixtures/pass26.json +0 -1
- data/tests/fixtures/pass3.json +0 -6
- data/tests/setup_variant.rb +0 -11
- data/tests/test_json.rb +0 -519
- data/tests/test_json_addition.rb +0 -196
- data/tests/test_json_encoding.rb +0 -65
- data/tests/test_json_fixtures.rb +0 -35
- data/tests/test_json_generate.rb +0 -348
- data/tests/test_json_generic_object.rb +0 -75
- data/tests/test_json_string_matching.rb +0 -39
- data/tests/test_json_unicode.rb +0 -72
data/COPYING
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a) place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b) use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c) give non-standard binaries non-standard names, with
|
21
|
+
instructions on where to get the original software distribution.
|
22
|
+
|
23
|
+
d) make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or binary form,
|
26
|
+
provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a) distribute the binaries and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b) accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c) give non-standard binaries non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d) make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under these terms.
|
43
|
+
|
44
|
+
For the list of those files and their copying conditions, see the
|
45
|
+
file LEGAL.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
data/LEGAL
ADDED
data/README.md
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
# JSON implementation for Ruby
|
2
|
+
|
3
|
+
[](https://github.com/ruby/json/actions/workflows/ci.yml)
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
This is an implementation of the JSON specification according to RFC 7159
|
8
|
+
http://www.ietf.org/rfc/rfc7159.txt .
|
9
|
+
|
10
|
+
The JSON generator generate UTF-8 character sequences by default.
|
11
|
+
If an :ascii\_only option with a true value is given, they escape all
|
12
|
+
non-ASCII and control characters with \uXXXX escape sequences, and support
|
13
|
+
UTF-16 surrogate pairs in order to be able to generate the whole range of
|
14
|
+
unicode code points.
|
15
|
+
|
16
|
+
All strings, that are to be encoded as JSON strings, should be UTF-8 byte
|
17
|
+
sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
|
18
|
+
encoded, please use the to\_json\_raw\_object method of String (which produces
|
19
|
+
an object, that contains a byte array) and decode the result on the receiving
|
20
|
+
endpoint.
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Install the gem and add to the application's Gemfile by executing:
|
25
|
+
|
26
|
+
$ bundle add json
|
27
|
+
|
28
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
29
|
+
|
30
|
+
$ gem install json
|
31
|
+
|
32
|
+
## Basic Usage
|
33
|
+
|
34
|
+
To use JSON you can
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'json'
|
38
|
+
```
|
39
|
+
|
40
|
+
Now you can parse a JSON document into a ruby data structure by calling
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
JSON.parse(document)
|
44
|
+
```
|
45
|
+
|
46
|
+
If you want to generate a JSON document from a ruby data structure call
|
47
|
+
```ruby
|
48
|
+
JSON.generate(data)
|
49
|
+
```
|
50
|
+
|
51
|
+
You can also use the `pretty_generate` method (which formats the output more
|
52
|
+
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
|
53
|
+
checks generate performs, e. g. nesting deepness checks).
|
54
|
+
|
55
|
+
## Casting non native types
|
56
|
+
|
57
|
+
JSON documents can only support Hashes, Arrays, Strings, Integers and Floats.
|
58
|
+
|
59
|
+
By default if you attempt to serialize something else, `JSON.generate` will
|
60
|
+
search for a `#to_json` method on that object:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
Position = Struct.new(:latitude, :longitude) do
|
64
|
+
def to_json(state = nil, *)
|
65
|
+
JSON::State.from_state(state).generate({
|
66
|
+
latitude: latitude,
|
67
|
+
longitude: longitude,
|
68
|
+
})
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
JSON.generate([
|
73
|
+
Position.new(12323.234, 435345.233),
|
74
|
+
Position.new(23434.676, 159435.324),
|
75
|
+
]) # => [{"latitude":12323.234,"longitude":435345.233},{"latitude":23434.676,"longitude":159435.324}]
|
76
|
+
```
|
77
|
+
|
78
|
+
If a `#to_json` method isn't defined on the object, `JSON.generate` will fallback to call `#to_s`:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
JSON.generate(Object.new) # => "#<Object:0x000000011e768b98>"
|
82
|
+
```
|
83
|
+
|
84
|
+
Both of these behavior can be disabled using the `strict: true` option:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
|
88
|
+
JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
|
89
|
+
```
|
90
|
+
|
91
|
+
## JSON::Coder
|
92
|
+
|
93
|
+
Since `#to_json` methods are global, it can sometimes be problematic if you need a given type to be
|
94
|
+
serialized in different ways in different locations.
|
95
|
+
|
96
|
+
Instead it is recommended to use the newer `JSON::Coder` API:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
module MyApp
|
100
|
+
API_JSON_CODER = JSON::Coder.new do |object|
|
101
|
+
case object
|
102
|
+
when Time
|
103
|
+
object.iso8601(3)
|
104
|
+
else
|
105
|
+
object
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
111
|
+
```
|
112
|
+
|
113
|
+
The provided block is called for all objects that don't have a native JSON equivalent, and
|
114
|
+
must return a Ruby object that has a native JSON equivalent.
|
115
|
+
|
116
|
+
## Combining JSON fragments
|
117
|
+
|
118
|
+
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
posts_json = cache.fetch_multi(post_ids) do |post_id|
|
122
|
+
JSON.generate(Post.find(post_id))
|
123
|
+
end
|
124
|
+
posts_json.map! { |post_json| JSON::Fragment.new(post_json) }
|
125
|
+
JSON.generate({ posts: posts_json, count: posts_json.count })
|
126
|
+
```
|
127
|
+
|
128
|
+
## Round-tripping arbitrary types
|
129
|
+
|
130
|
+
> [!CAUTION]
|
131
|
+
> You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
|
132
|
+
> as it can lead to remote code execution vulnerabilities.
|
133
|
+
|
134
|
+
To create a JSON document from a ruby data structure, you can call
|
135
|
+
`JSON.generate` like that:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
139
|
+
# => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
|
140
|
+
```
|
141
|
+
|
142
|
+
To get back a ruby data structure from a JSON document, you have to call
|
143
|
+
JSON.parse on it:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
JSON.parse json
|
147
|
+
# => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
|
148
|
+
```
|
149
|
+
|
150
|
+
Note, that the range from the original data structure is a simple
|
151
|
+
string now. The reason for this is, that JSON doesn't support ranges
|
152
|
+
or arbitrary classes. In this case the json library falls back to call
|
153
|
+
`Object#to_json`, which is the same as `#to_s.to_json`.
|
154
|
+
|
155
|
+
It's possible to add JSON support serialization to arbitrary classes by
|
156
|
+
simply implementing a more specialized version of the `#to_json method`, that
|
157
|
+
should return a JSON object (a hash converted to JSON with `#to_json`) like
|
158
|
+
this (don't forget the `*a` for all the arguments):
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
class Range
|
162
|
+
def to_json(*a)
|
163
|
+
{
|
164
|
+
'json_class' => self.class.name, # = 'Range'
|
165
|
+
'data' => [ first, last, exclude_end? ]
|
166
|
+
}.to_json(*a)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
```
|
170
|
+
|
171
|
+
The hash key `json_class` is the class, that will be asked to deserialise the
|
172
|
+
JSON representation later. In this case it's `Range`, but any namespace of
|
173
|
+
the form `A::B` or `::A::B` will do. All other keys are arbitrary and can be
|
174
|
+
used to store the necessary data to configure the object to be deserialised.
|
175
|
+
|
176
|
+
If the key `json_class` is found in a JSON object, the JSON parser checks
|
177
|
+
if the given class responds to the `json_create` class method. If so, it is
|
178
|
+
called with the JSON object converted to a Ruby hash. So a range can
|
179
|
+
be deserialised by implementing `Range.json_create` like this:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
class Range
|
183
|
+
def self.json_create(o)
|
184
|
+
new(*o['data'])
|
185
|
+
end
|
186
|
+
end
|
187
|
+
```
|
188
|
+
|
189
|
+
Now it possible to serialise/deserialise ranges as well:
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
193
|
+
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
194
|
+
JSON.parse json
|
195
|
+
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
196
|
+
json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
197
|
+
# => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
|
198
|
+
JSON.unsafe_load json
|
199
|
+
# => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
|
200
|
+
```
|
201
|
+
|
202
|
+
`JSON.generate` always creates the shortest possible string representation of a
|
203
|
+
ruby data structure in one line. This is good for data storage or network
|
204
|
+
protocols, but not so good for humans to read. Fortunately there's also
|
205
|
+
`JSON.pretty_generate` (or `JSON.pretty_generate`) that creates a more readable
|
206
|
+
output:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
|
210
|
+
[
|
211
|
+
1,
|
212
|
+
2,
|
213
|
+
{
|
214
|
+
"a": 3.141
|
215
|
+
},
|
216
|
+
false,
|
217
|
+
true,
|
218
|
+
null,
|
219
|
+
{
|
220
|
+
"json_class": "Range",
|
221
|
+
"data": [
|
222
|
+
4,
|
223
|
+
10,
|
224
|
+
false
|
225
|
+
]
|
226
|
+
}
|
227
|
+
]
|
228
|
+
```
|
229
|
+
|
230
|
+
There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
|
231
|
+
`pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
|
232
|
+
the `pp` library's `pp` methods.
|
233
|
+
|
234
|
+
## Development
|
235
|
+
|
236
|
+
### Release
|
237
|
+
|
238
|
+
Update the `lib/json/version.rb` file.
|
239
|
+
|
240
|
+
```
|
241
|
+
rbenv shell 2.6.5
|
242
|
+
rake build
|
243
|
+
gem push pkg/json-2.3.0.gem
|
244
|
+
|
245
|
+
rbenv shell jruby-9.2.9.0
|
246
|
+
rake build
|
247
|
+
gem push pkg/json-2.3.0-java.gem
|
248
|
+
```
|
249
|
+
|
250
|
+
## Author
|
251
|
+
|
252
|
+
Florian Frank <mailto:flori@ping.de>
|
253
|
+
|
254
|
+
## License
|
255
|
+
|
256
|
+
Ruby License, see https://www.ruby-lang.org/en/about/license.txt.
|
257
|
+
|
258
|
+
## Download
|
259
|
+
|
260
|
+
The latest version of this library can be downloaded at
|
261
|
+
|
262
|
+
* https://rubygems.org/gems/json
|
263
|
+
|
264
|
+
Online Documentation should be located at
|
265
|
+
|
266
|
+
* https://www.rubydoc.info/gems/json
|
267
|
+
|
268
|
+
[Ragel]: http://www.colm.net/open-source/ragel/
|
data/json.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
version = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line|
|
4
|
+
/^\s*VERSION\s*=\s*'(.*)'/ =~ line and break $1
|
5
|
+
end rescue nil
|
6
|
+
|
7
|
+
spec = Gem::Specification.new do |s|
|
8
|
+
java_ext = Gem::Platform === s.platform && s.platform =~ 'java' || RUBY_ENGINE == 'jruby'
|
9
|
+
|
10
|
+
s.name = "json"
|
11
|
+
s.version = version
|
12
|
+
|
13
|
+
s.summary = "JSON Implementation for Ruby"
|
14
|
+
s.homepage = "https://github.com/ruby/json"
|
15
|
+
s.metadata = {
|
16
|
+
'bug_tracker_uri' => 'https://github.com/ruby/json/issues',
|
17
|
+
'changelog_uri' => 'https://github.com/ruby/json/blob/master/CHANGES.md',
|
18
|
+
'documentation_uri' => 'https://docs.ruby-lang.org/en/master/JSON.html',
|
19
|
+
'homepage_uri' => s.homepage,
|
20
|
+
'source_code_uri' => 'https://github.com/ruby/json',
|
21
|
+
}
|
22
|
+
|
23
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2.7")
|
24
|
+
|
25
|
+
if java_ext
|
26
|
+
s.description = "A JSON implementation as a JRuby extension."
|
27
|
+
s.author = "Daniel Luz"
|
28
|
+
s.email = "dev+ruby@mernen.com"
|
29
|
+
else
|
30
|
+
s.description = "This is a JSON implementation as a Ruby extension in C."
|
31
|
+
s.authors = ["Florian Frank"]
|
32
|
+
s.email = "flori@ping.de"
|
33
|
+
end
|
34
|
+
|
35
|
+
s.licenses = ["Ruby"]
|
36
|
+
|
37
|
+
s.extra_rdoc_files = ["README.md"]
|
38
|
+
s.rdoc_options = ["--title", "JSON implementation for Ruby", "--main", "README.md"]
|
39
|
+
|
40
|
+
s.files = [
|
41
|
+
"CHANGES.md",
|
42
|
+
"COPYING",
|
43
|
+
"BSDL",
|
44
|
+
"LEGAL",
|
45
|
+
"README.md",
|
46
|
+
"json.gemspec",
|
47
|
+
*Dir["lib/**/*.rb"],
|
48
|
+
]
|
49
|
+
|
50
|
+
if java_ext
|
51
|
+
s.platform = 'java'
|
52
|
+
s.files += Dir["lib/json/ext/**/*.jar"]
|
53
|
+
else
|
54
|
+
s.extensions = Dir["ext/json/**/extconf.rb"]
|
55
|
+
s.files += Dir["ext/json/**/*.{c,h}"]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
if RUBY_ENGINE == 'jruby' && $0 == __FILE__
|
60
|
+
Gem::Builder.new(spec).build
|
61
|
+
else
|
62
|
+
spec
|
63
|
+
end
|
data/lib/json/add/bigdecimal.rb
CHANGED
@@ -1,28 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
|
-
|
5
|
+
begin
|
6
|
+
require 'bigdecimal'
|
7
|
+
rescue LoadError
|
8
|
+
end
|
5
9
|
|
6
10
|
class BigDecimal
|
7
|
-
|
8
|
-
#
|
9
|
-
# method used for JSON marshalling support.
|
11
|
+
|
12
|
+
# See #as_json.
|
10
13
|
def self.json_create(object)
|
11
14
|
BigDecimal._load object['b']
|
12
15
|
end
|
13
16
|
|
14
|
-
#
|
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
|
15
34
|
#
|
16
|
-
# method used for JSON marshalling support.
|
17
35
|
def as_json(*)
|
18
36
|
{
|
19
37
|
JSON.create_id => self.class.name,
|
20
|
-
'b' => _dump,
|
38
|
+
'b' => _dump.force_encoding(Encoding::UTF_8),
|
21
39
|
}
|
22
40
|
end
|
23
41
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
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
|
+
#
|
55
|
+
def to_json(*args)
|
56
|
+
as_json.to_json(*args)
|
27
57
|
end
|
28
|
-
end
|
58
|
+
end if defined?(::BigDecimal)
|
data/lib/json/add/complex.rb
CHANGED
@@ -1,18 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
|
-
defined?(::Complex) or require 'complex'
|
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,8 +34,18 @@ class Complex
|
|
21
34
|
}
|
22
35
|
end
|
23
36
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
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
|
+
#
|
48
|
+
def to_json(*args)
|
49
|
+
as_json.to_json(*args)
|
27
50
|
end
|
28
|
-
end
|
51
|
+
end
|
data/lib/json/add/core.rb
CHANGED
data/lib/json/add/date.rb
CHANGED
@@ -1,21 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
require 'date'
|
5
6
|
|
6
|
-
# Date serialization/deserialization
|
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
@@ -1,14 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
require 'date'
|
5
6
|
|
6
|
-
# DateTime serialization/deserialization
|
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
|