json 2.9.1 → 2.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +7 -0
- data/LEGAL +0 -52
- data/README.md +75 -2
- data/ext/json/ext/fbuffer/fbuffer.h +1 -10
- data/ext/json/ext/generator/generator.c +394 -263
- data/ext/json/ext/parser/parser.c +762 -2522
- data/json.gemspec +3 -4
- data/lib/json/add/symbol.rb +7 -2
- data/lib/json/common.rb +107 -10
- data/lib/json/ext/generator/state.rb +1 -11
- data/lib/json/ext.rb +26 -4
- data/lib/json/truffle_ruby/generator.rb +111 -50
- data/lib/json/version.rb +1 -1
- metadata +6 -11
- data/ext/json/ext/parser/parser.rl +0 -1465
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24fd6da1cbd6a70e528edd9a58413c13ff660a9bae2df816b85dc13f31e81bfc
|
4
|
+
data.tar.gz: a58df228819f0e5742cae810d6c94e80d11a4bff6af309e8c0dc3b511c85a47b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74fa1f2a05b69cd507b21d387568a404a7417dbe4838ff632d5a1ecb7cf9ad8ac40f499718b8df9e5d3b338f5e938ad13177b4fa38f9b083532780a3080ed006
|
7
|
+
data.tar.gz: e31f05d9ad12c09f1433b93080c40df60fc5062c45a23378ae21579d73856cb9bab79e32df34601b9bad0525845bffb43403c450ac41b272e5682fbf6bd63a40
|
data/CHANGES.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Changes
|
2
2
|
|
3
|
+
### 2025-02-10 (2.10.0)
|
4
|
+
|
5
|
+
* `strict: true` now accept symbols as values. Previously they'd only be accepted as hash keys.
|
6
|
+
* The C extension Parser has been entirely reimplemented from scratch.
|
7
|
+
* Introduced `JSON::Coder` as a new API allowing to customize how non native types are serialized in a non-global way.
|
8
|
+
* The Java implementation of the generator received many optimizations.
|
9
|
+
|
3
10
|
### 2024-12-18 (2.9.1)
|
4
11
|
|
5
12
|
* Fix support for Solaris 10.
|
data/LEGAL
CHANGED
@@ -6,55 +6,3 @@
|
|
6
6
|
All the files in this distribution are covered under either the Ruby's
|
7
7
|
license (see the file COPYING) or public-domain except some files
|
8
8
|
mentioned below.
|
9
|
-
|
10
|
-
== MIT License
|
11
|
-
>>>
|
12
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
13
|
-
a copy of this software and associated documentation files (the
|
14
|
-
"Software"), to deal in the Software without restriction, including
|
15
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
16
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
17
|
-
permit persons to whom the Software is furnished to do so, subject to
|
18
|
-
the following conditions:
|
19
|
-
|
20
|
-
The above copyright notice and this permission notice shall be
|
21
|
-
included in all copies or substantial portions of the Software.
|
22
|
-
|
23
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
24
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
25
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
26
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
27
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
28
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
29
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
30
|
-
|
31
|
-
== Old-style BSD license
|
32
|
-
>>>
|
33
|
-
Redistribution and use in source and binary forms, with or without
|
34
|
-
modification, are permitted provided that the following conditions
|
35
|
-
are met:
|
36
|
-
1. Redistributions of source code must retain the above copyright
|
37
|
-
notice, this list of conditions and the following disclaimer.
|
38
|
-
2. Redistributions in binary form must reproduce the above copyright
|
39
|
-
notice, this list of conditions and the following disclaimer in the
|
40
|
-
documentation and/or other materials provided with the distribution.
|
41
|
-
3. Neither the name of the University nor the names of its contributors
|
42
|
-
may be used to endorse or promote products derived from this software
|
43
|
-
without specific prior written permission.
|
44
|
-
|
45
|
-
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
46
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
47
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
48
|
-
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
49
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
50
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
51
|
-
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
52
|
-
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
53
|
-
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
54
|
-
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
55
|
-
SUCH DAMAGE.
|
56
|
-
|
57
|
-
IMPORTANT NOTE::
|
58
|
-
|
59
|
-
From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
|
60
|
-
paragraph 3 above is now null and void.
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
29
29
|
|
30
30
|
$ gem install json
|
31
31
|
|
32
|
-
## Usage
|
32
|
+
## Basic Usage
|
33
33
|
|
34
34
|
To use JSON you can
|
35
35
|
|
@@ -52,7 +52,80 @@ You can also use the `pretty_generate` method (which formats the output more
|
|
52
52
|
verbosely and nicely) or `fast_generate` (which doesn't do any of the security
|
53
53
|
checks generate performs, e. g. nesting deepness checks).
|
54
54
|
|
55
|
-
##
|
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
|
56
129
|
|
57
130
|
> [!CAUTION]
|
58
131
|
> You should never use `JSON.unsafe_load` nor `JSON.parse(str, create_additions: true)` to parse untrusted user input,
|
@@ -59,17 +59,11 @@ typedef struct FBufferStruct {
|
|
59
59
|
#define FBUFFER_PAIR(fb) FBUFFER_PTR(fb), FBUFFER_LEN(fb)
|
60
60
|
|
61
61
|
static void fbuffer_free(FBuffer *fb);
|
62
|
-
#ifndef JSON_GENERATOR
|
63
62
|
static void fbuffer_clear(FBuffer *fb);
|
64
|
-
#endif
|
65
63
|
static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len);
|
66
|
-
#ifdef JSON_GENERATOR
|
67
64
|
static void fbuffer_append_long(FBuffer *fb, long number);
|
68
|
-
#endif
|
69
65
|
static inline void fbuffer_append_char(FBuffer *fb, char newchr);
|
70
|
-
#ifdef JSON_GENERATOR
|
71
66
|
static VALUE fbuffer_finalize(FBuffer *fb);
|
72
|
-
#endif
|
73
67
|
|
74
68
|
static void fbuffer_stack_init(FBuffer *fb, unsigned long initial_length, char *stack_buffer, long stack_buffer_size)
|
75
69
|
{
|
@@ -156,7 +150,6 @@ static void fbuffer_append(FBuffer *fb, const char *newstr, unsigned long len)
|
|
156
150
|
}
|
157
151
|
}
|
158
152
|
|
159
|
-
#ifdef JSON_GENERATOR
|
160
153
|
static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
161
154
|
{
|
162
155
|
const char *newstr = StringValuePtr(str);
|
@@ -166,7 +159,6 @@ static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
|
166
159
|
|
167
160
|
fbuffer_append(fb, newstr, len);
|
168
161
|
}
|
169
|
-
#endif
|
170
162
|
|
171
163
|
static inline void fbuffer_append_char(FBuffer *fb, char newchr)
|
172
164
|
{
|
@@ -175,7 +167,6 @@ static inline void fbuffer_append_char(FBuffer *fb, char newchr)
|
|
175
167
|
fb->len++;
|
176
168
|
}
|
177
169
|
|
178
|
-
#ifdef JSON_GENERATOR
|
179
170
|
static long fltoa(long number, char *buf)
|
180
171
|
{
|
181
172
|
static const char digits[] = "0123456789";
|
@@ -210,5 +201,5 @@ static VALUE fbuffer_finalize(FBuffer *fb)
|
|
210
201
|
return result;
|
211
202
|
}
|
212
203
|
}
|
213
|
-
|
204
|
+
|
214
205
|
#endif
|