rapidjson 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -3
- data/ext/rapidjson/buffer.hh +1 -0
- data/ext/rapidjson/cext.cc +27 -25
- data/ext/rapidjson/encoder.hh +97 -38
- data/ext/rapidjson/extconf.rb +1 -1
- data/ext/rapidjson/parser.hh +2 -2
- data/lib/rapidjson/active_support_encoder.rb +26 -0
- data/lib/rapidjson/json_gem.rb +79 -28
- data/lib/rapidjson/version.rb +2 -2
- data/lib/rapidjson.rb +48 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4824f0636d8f801de840ebf32bd639258ce1e21a063228beaabd2b4ad6e1760a
|
4
|
+
data.tar.gz: 04bdbf36fdfd8b2ef289d8ba6e68b9d34c731cd1ffae61c154dd276ed56dc35e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74f3d0a620d505b0aa55601830f294794e8a938ee087d9fcc228fc6a83452c2b255b1ddf9c0b3aea1d086808259e6f75744fcd6fb9528361090d2bea9d35a612
|
7
|
+
data.tar.gz: cff30c2bafab60f467ee59cf8a63691f93dd2de1fb5f07573e54a1303f3f279f9100ec9e1c6991cf5c9e874c25df3f6dd52b3e549aef3e510d0d068ce50f9877
|
data/README.md
CHANGED
@@ -73,13 +73,10 @@ Many of these optimization can be found in all popular Ruby JSON libraries
|
|
73
73
|
yajl 35.510 (± 2.8%) i/s - 180.000 in 5.070803s
|
74
74
|
json 22.105 (± 0.0%) i/s - 112.000 in 5.067063s
|
75
75
|
oj 15.163 (± 6.6%) i/s - 76.000 in 5.042864s
|
76
|
-
fast_jsonparser 10.791 (± 0.0%) i/s - 54.000 in 5.004290s
|
77
76
|
rapidjson 148.263 (± 2.0%) i/s - 742.000 in 5.006370s
|
78
77
|
```
|
79
78
|
Notes: oj seems unusually bad at this test, and is usually faster than yajl and
|
80
79
|
json, and comparable to rapidjson.
|
81
|
-
`fast_jsonparser` has been slow since Ruby 3.0, likely a bug, if fixed I'd
|
82
|
-
expect it to be the fasest JSON parser.
|
83
80
|
|
84
81
|
Other libraries may include modes to avoid constructing all objects. Currently
|
85
82
|
RapidJSON only focuses on the patterns and APIs users are likely to actually
|
@@ -91,6 +88,27 @@ I spent a week working on YAJL/yajl-ruby, and though I really liked the library,
|
|
91
88
|
|
92
89
|
However, if you're happy with your current Ruby JSON library (including `json`) you should keep using it. They're all very good.
|
93
90
|
|
91
|
+
## JSON gem compatibility
|
92
|
+
|
93
|
+
Contrary to some other JSON libraries, `RapidJSON` doesn't provice a monkey patch to entirely replace the stdlib JSON gem.
|
94
|
+
|
95
|
+
However it does provide a module that behave like the stdlib JSON gem and that can be used to monkey patch existing code.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
module SomeLibrary
|
99
|
+
def do_stuff(payload)
|
100
|
+
JSON.parse(payload)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
SomeLibrary::JSON = RapidJSON::JSONGem
|
107
|
+
```
|
108
|
+
|
109
|
+
Note that this module only use `RapidJSON` when it's certain it is safe to do so. If the JSON gem is called with
|
110
|
+
some options that `RapidJSON` doesn't support, it automatically fallbacks to calling the JSON gem.
|
111
|
+
|
94
112
|
## Development
|
95
113
|
|
96
114
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/ext/rapidjson/buffer.hh
CHANGED
data/ext/rapidjson/cext.cc
CHANGED
@@ -4,31 +4,32 @@
|
|
4
4
|
#include "rapidjson/prettywriter.h"
|
5
5
|
#include "rapidjson/error/en.h"
|
6
6
|
|
7
|
-
static VALUE rb_mRapidjson;
|
8
7
|
static VALUE rb_eParseError;
|
9
8
|
static VALUE rb_eEncodeError;
|
10
|
-
|
11
|
-
static ID id_to_json;
|
12
|
-
static ID id_to_s;
|
9
|
+
static VALUE rb_cRapidJSONFragment;
|
13
10
|
|
14
11
|
#include "encoder.hh"
|
15
12
|
#include "parser.hh"
|
13
|
+
#include "json_escape.h"
|
16
14
|
|
17
15
|
using namespace rapidjson;
|
18
16
|
|
19
17
|
typedef RubyStringBuffer DefaultBuffer;
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
19
|
+
static VALUE
|
20
|
+
dump(VALUE _self, VALUE obj, VALUE pretty, VALUE as_json, VALUE allow_nan) {
|
21
|
+
// NB: as_json here is not marked by the extension, but is always on the stack
|
22
|
+
if (RTEST(pretty)) {
|
23
|
+
RubyObjectEncoder<DefaultBuffer, PrettyWriter<DefaultBuffer> > encoder(as_json, RTEST(allow_nan));
|
24
|
+
return encoder.encode(obj);
|
25
|
+
} else {
|
26
|
+
RubyObjectEncoder<DefaultBuffer, Writer<DefaultBuffer> > encoder(as_json, RTEST(allow_nan));
|
27
|
+
return encoder.encode(obj);
|
28
|
+
}
|
29
29
|
}
|
30
30
|
|
31
|
-
|
31
|
+
static VALUE
|
32
|
+
load(VALUE _self, VALUE string) {
|
32
33
|
RubyObjectHandler handler;
|
33
34
|
Reader reader;
|
34
35
|
char *cstring = StringValueCStr(string); // fixme?
|
@@ -43,7 +44,8 @@ VALUE parse(VALUE _self, VALUE string) {
|
|
43
44
|
return handler.GetRoot();
|
44
45
|
}
|
45
46
|
|
46
|
-
|
47
|
+
static VALUE
|
48
|
+
valid_json_p(VALUE _self, VALUE string) {
|
47
49
|
NullHandler handler;
|
48
50
|
Reader reader;
|
49
51
|
char *cstring = StringValueCStr(string); // fixme?
|
@@ -60,18 +62,18 @@ VALUE valid_json_p(VALUE _self, VALUE string) {
|
|
60
62
|
extern "C" void
|
61
63
|
Init_rapidjson(void)
|
62
64
|
{
|
63
|
-
|
64
|
-
|
65
|
+
VALUE rb_mRapidJSON = rb_const_get(rb_cObject, rb_intern("RapidJSON"));
|
66
|
+
VALUE rb_cCoder = rb_const_get(rb_mRapidJSON, rb_intern("Coder"));
|
67
|
+
rb_cRapidJSONFragment = rb_const_get(rb_mRapidJSON, rb_intern("Fragment"));;
|
68
|
+
rb_global_variable(&rb_cRapidJSONFragment);
|
65
69
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
rb_define_module_function(rb_mRapidjson, "dump", encode, 1);
|
70
|
+
rb_define_private_method(rb_cCoder, "_dump", dump, 4);
|
71
|
+
rb_define_method(rb_cCoder, "load", load, 1);
|
72
|
+
rb_define_method(rb_cCoder, "valid_json?", valid_json_p, 1);
|
70
73
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
+
VALUE rb_eRapidJSONError = rb_const_get(rb_mRapidJSON, rb_intern("Error"));
|
75
|
+
rb_eParseError = rb_define_class_under(rb_mRapidJSON, "ParseError", rb_eRapidJSONError);
|
76
|
+
rb_eEncodeError = rb_define_class_under(rb_mRapidJSON, "EncodeError", rb_eRapidJSONError);
|
74
77
|
|
75
|
-
|
76
|
-
rb_eEncodeError = rb_define_class_under(rb_mRapidjson, "EncodeError", rb_eStandardError);
|
78
|
+
rb_define_singleton_method(rb_mRapidJSON, "json_escape", escape_json, 1);
|
77
79
|
}
|
data/ext/rapidjson/encoder.hh
CHANGED
@@ -9,33 +9,60 @@ template <typename B = RubyStringBuffer, typename W=Writer<B> >
|
|
9
9
|
class RubyObjectEncoder {
|
10
10
|
B buf;
|
11
11
|
W writer;
|
12
|
+
VALUE as_json;
|
13
|
+
bool allow_nan;
|
12
14
|
|
13
15
|
void encode_array(VALUE ary) {
|
14
16
|
writer.StartArray();
|
15
17
|
int length = RARRAY_LEN(ary);
|
16
18
|
RARRAY_PTR_USE(ary, ptr, {
|
17
19
|
for (int i = 0; i < length; i++) {
|
18
|
-
encode_any(ptr[i]);
|
20
|
+
encode_any(ptr[i], true);
|
19
21
|
}
|
20
22
|
});
|
21
23
|
writer.EndArray();
|
22
24
|
}
|
23
25
|
|
26
|
+
static VALUE string_to_utf8_compatible(VALUE str) {
|
27
|
+
rb_encoding *enc = rb_enc_get(str);
|
28
|
+
if (enc == rb_utf8_encoding() || enc == rb_usascii_encoding()) {
|
29
|
+
return str;
|
30
|
+
} else {
|
31
|
+
return rb_str_export_to_enc(str, rb_utf8_encoding());
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
24
35
|
void encode_key(VALUE key) {
|
25
36
|
switch(rb_type(key)) {
|
37
|
+
case T_STRING:
|
38
|
+
break;
|
26
39
|
case T_SYMBOL:
|
27
40
|
key = rb_sym2str(key);
|
28
|
-
|
29
|
-
case
|
30
|
-
|
31
|
-
|
41
|
+
break;
|
42
|
+
case T_FIXNUM:
|
43
|
+
case T_BIGNUM:
|
44
|
+
key = rb_String(key);
|
45
|
+
break;
|
32
46
|
default:
|
33
|
-
{
|
34
|
-
|
47
|
+
if (NIL_P(as_json)) {
|
48
|
+
rb_raise(rb_eTypeError, "Invalid object key type: %" PRIsVALUE, rb_obj_class(key));
|
49
|
+
UNREACHABLE_RETURN();
|
50
|
+
}
|
51
|
+
|
52
|
+
VALUE args[2] = { key, Qtrue };
|
53
|
+
key = rb_proc_call_with_block(as_json, 2, args, Qnil);
|
54
|
+
if (rb_obj_class(key) == rb_cRapidJSONFragment) {
|
55
|
+
VALUE str = rb_struct_aref(key, INT2FIX(0));
|
35
56
|
Check_Type(str, T_STRING);
|
36
|
-
|
57
|
+
return encode_raw_json_str(str);
|
37
58
|
}
|
59
|
+
|
60
|
+
break;
|
38
61
|
}
|
62
|
+
|
63
|
+
Check_Type(key, T_STRING);
|
64
|
+
key = string_to_utf8_compatible(key);
|
65
|
+
writer.Key(RSTRING_PTR(key), RSTRING_LEN(key), false);
|
39
66
|
}
|
40
67
|
|
41
68
|
static int encode_hash_i_cb(VALUE key, VALUE val, VALUE ctx) {
|
@@ -46,7 +73,7 @@ class RubyObjectEncoder {
|
|
46
73
|
|
47
74
|
void encode_hash_i(VALUE key, VALUE val) {
|
48
75
|
encode_key(key);
|
49
|
-
encode_any(val);
|
76
|
+
encode_any(val, true);
|
50
77
|
}
|
51
78
|
|
52
79
|
void encode_hash(VALUE hash) {
|
@@ -60,27 +87,48 @@ class RubyObjectEncoder {
|
|
60
87
|
}
|
61
88
|
|
62
89
|
void encode_bignum(VALUE b) {
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
writer.Int64(ll);
|
72
|
-
} else {
|
73
|
-
unsigned long long ull = rb_big2ull(b);
|
74
|
-
writer.Uint64(ull);
|
75
|
-
}
|
90
|
+
// Some T_BIGNUM might be small enough to fit in long long or unsigned long long
|
91
|
+
// but this being the slow path, it's not really worth it.
|
92
|
+
VALUE str = rb_String(b);
|
93
|
+
Check_Type(str, T_STRING);
|
94
|
+
|
95
|
+
// We should be able to use RawNumber here, but it's buggy
|
96
|
+
// https://github.com/Tencent/rapidjson/issues/852
|
97
|
+
writer.RawValue(RSTRING_PTR(str), RSTRING_LEN(str), kNumberType);
|
76
98
|
}
|
77
99
|
|
78
|
-
void encode_float(VALUE v) {
|
100
|
+
void encode_float(VALUE v, bool generic) {
|
79
101
|
double f = rb_float_value(v);
|
80
|
-
|
102
|
+
if (!isfinite(f)) {
|
103
|
+
if (!allow_nan && !NIL_P(as_json) && generic) {
|
104
|
+
return encode_generic(v);
|
105
|
+
}
|
106
|
+
if (f == (-1.0 / 0.0)) {
|
107
|
+
if (allow_nan) {
|
108
|
+
writer.RawValue("-Infinity", 9, kObjectType);
|
109
|
+
} else {
|
110
|
+
rb_raise(rb_eEncodeError, "-Float::INFINITY is not allowed in JSON");
|
111
|
+
}
|
112
|
+
} else if (isinf(f)) {
|
113
|
+
if (allow_nan) {
|
114
|
+
writer.RawValue("Infinity", 8, kObjectType);
|
115
|
+
} else {
|
116
|
+
rb_raise(rb_eEncodeError, "Float::INFINITY is not allowed in JSON");
|
117
|
+
}
|
118
|
+
} else if (isnan(f)) {
|
119
|
+
if (allow_nan) {
|
120
|
+
writer.RawValue("NaN", 3, kObjectType);
|
121
|
+
} else {
|
122
|
+
rb_raise(rb_eEncodeError, "Float::NAN is not allowed in JSON");
|
123
|
+
}
|
124
|
+
}
|
125
|
+
} else {
|
126
|
+
writer.Double(f);
|
127
|
+
}
|
81
128
|
}
|
82
129
|
|
83
130
|
void encode_string(VALUE v) {
|
131
|
+
v = string_to_utf8_compatible(v);
|
84
132
|
writer.String(RSTRING_PTR(v), RSTRING_LEN(v), false);
|
85
133
|
}
|
86
134
|
|
@@ -92,22 +140,20 @@ class RubyObjectEncoder {
|
|
92
140
|
const char *cstr = RSTRING_PTR(s);
|
93
141
|
size_t len = RSTRING_LEN(s);
|
94
142
|
|
95
|
-
|
143
|
+
writer.RawValue(cstr, len, kObjectType);
|
96
144
|
}
|
97
145
|
|
98
146
|
void encode_generic(VALUE obj) {
|
99
|
-
if (
|
100
|
-
|
101
|
-
|
102
|
-
encode_raw_json_str(str);
|
103
|
-
} else {
|
104
|
-
VALUE str = rb_funcall(obj, id_to_s, 0);
|
105
|
-
Check_Type(str, T_STRING);
|
106
|
-
encode_string(str);
|
147
|
+
if (NIL_P(as_json)) {
|
148
|
+
rb_raise(rb_eTypeError, "Don't know how to serialize %" PRIsVALUE " to JSON", rb_obj_class(obj));
|
149
|
+
UNREACHABLE_RETURN();
|
107
150
|
}
|
151
|
+
|
152
|
+
VALUE args[2] = { obj, Qfalse };
|
153
|
+
encode_any(rb_proc_call_with_block(as_json, 2, args, Qnil), false);
|
108
154
|
}
|
109
155
|
|
110
|
-
void encode_any(VALUE v) {
|
156
|
+
void encode_any(VALUE v, bool generic) {
|
111
157
|
switch(rb_type(v)) {
|
112
158
|
case T_NIL:
|
113
159
|
writer.Null();
|
@@ -123,7 +169,7 @@ class RubyObjectEncoder {
|
|
123
169
|
case T_BIGNUM:
|
124
170
|
return encode_bignum(v);
|
125
171
|
case T_FLOAT:
|
126
|
-
return encode_float(v);
|
172
|
+
return encode_float(v, generic);
|
127
173
|
case T_HASH:
|
128
174
|
return encode_hash(v);
|
129
175
|
case T_ARRAY:
|
@@ -132,19 +178,32 @@ class RubyObjectEncoder {
|
|
132
178
|
return encode_string(v);
|
133
179
|
case T_SYMBOL:
|
134
180
|
return encode_symbol(v);
|
181
|
+
case T_STRUCT:
|
182
|
+
if (rb_obj_class(v) == rb_cRapidJSONFragment) {
|
183
|
+
VALUE str = rb_struct_aref(v, INT2FIX(0));
|
184
|
+
Check_Type(str, T_STRING);
|
185
|
+
return encode_raw_json_str(str);
|
186
|
+
}
|
187
|
+
// fall through
|
135
188
|
default:
|
136
|
-
|
189
|
+
if (generic) {
|
190
|
+
encode_generic(v);
|
191
|
+
} else {
|
192
|
+
rb_raise(rb_eTypeError, "Don't know how to serialize %" PRIsVALUE " to JSON", rb_obj_class(v));
|
193
|
+
}
|
137
194
|
}
|
138
195
|
}
|
139
196
|
|
140
197
|
public:
|
141
|
-
RubyObjectEncoder(): buf(), writer(buf), depth(0) {
|
198
|
+
RubyObjectEncoder(VALUE as_json_proc, bool allow_nan_): buf(), writer(buf), depth(0) {
|
199
|
+
as_json = as_json_proc;
|
200
|
+
allow_nan = allow_nan_;
|
142
201
|
};
|
143
202
|
|
144
203
|
int depth;
|
145
204
|
|
146
205
|
VALUE encode(VALUE obj) {
|
147
|
-
encode_any(obj);
|
206
|
+
encode_any(obj, true);
|
148
207
|
return buf.GetRubyString();
|
149
208
|
}
|
150
209
|
};
|
data/ext/rapidjson/extconf.rb
CHANGED
data/ext/rapidjson/parser.hh
CHANGED
@@ -63,10 +63,10 @@ struct RubyObjectHandler : public BaseReaderHandler<UTF8<>, RubyObjectHandler> {
|
|
63
63
|
}
|
64
64
|
|
65
65
|
bool Key(const char* str, SizeType length, bool copy) {
|
66
|
-
#ifdef
|
66
|
+
#ifdef HAVE_RB_ENC_INTERNED_STR
|
67
67
|
VALUE val = rb_enc_interned_str(str, length, rb_utf8_encoding());
|
68
68
|
#else
|
69
|
-
VALUE val =
|
69
|
+
VALUE val = rb_enc_str_new(str, length, rb_utf8_encoding());
|
70
70
|
#endif
|
71
71
|
return PutKey(val);
|
72
72
|
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RapidJSON
|
2
|
+
class ActiveSupportEncoder
|
3
|
+
def initialize(options = nil)
|
4
|
+
@options = options
|
5
|
+
@coder = RapidJSON::Coder.new do |value, is_key|
|
6
|
+
if is_key
|
7
|
+
value.to_s
|
8
|
+
else
|
9
|
+
value.as_json
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Encode the given object into a JSON string
|
15
|
+
def encode(value)
|
16
|
+
if @options && !@options.empty?
|
17
|
+
value = value.as_json(@options.dup)
|
18
|
+
end
|
19
|
+
json = @coder.dump(value)
|
20
|
+
if ActiveSupport::JSON::Encoding.escape_html_entities_in_json
|
21
|
+
json = RapidJSON.json_escape(json)
|
22
|
+
end
|
23
|
+
json
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/rapidjson/json_gem.rb
CHANGED
@@ -1,36 +1,87 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module RapidJSON
|
4
|
+
module JSONGem
|
5
|
+
GeneratorError = RapidJSON::EncodeError
|
6
|
+
|
7
|
+
GEM = ::JSON
|
8
|
+
private_constant :GEM
|
9
|
+
|
10
|
+
STATE = ::JSON::State.new
|
11
|
+
private_constant :STATE
|
12
|
+
|
13
|
+
TO_JSON_PROC = lambda do |object, is_key|
|
14
|
+
if !is_key && object.respond_to?(:to_json)
|
15
|
+
if Float === object
|
16
|
+
# Avoid calling .to_json on NaN/Infinity/-Infinity
|
17
|
+
# Prefers our exception to one raised by ::JSON
|
18
|
+
object
|
19
|
+
else
|
20
|
+
Fragment.new(object.to_json(STATE))
|
21
|
+
end
|
22
|
+
elsif object.respond_to?(:to_s)
|
23
|
+
object.to_s
|
24
|
+
else
|
25
|
+
raise TypeError, "Can't serialize #{object.class} to JSON"
|
26
|
+
end
|
7
27
|
end
|
8
|
-
|
9
|
-
end
|
28
|
+
private_constant :TO_JSON_PROC
|
10
29
|
|
11
|
-
|
12
|
-
|
13
|
-
RapidJSON.encode(obj)
|
14
|
-
end
|
30
|
+
GENERATE_CODER = Coder.new(&TO_JSON_PROC)
|
31
|
+
private_constant :GENERATE_CODER
|
15
32
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
33
|
+
DUMP_CODER = Coder.new(allow_nan: true, &TO_JSON_PROC)
|
34
|
+
private_constant :DUMP_CODER
|
19
35
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
36
|
+
PRETTY_CODER = Coder.new(pretty: true, &TO_JSON_PROC)
|
37
|
+
private_constant :PRETTY_CODER
|
24
38
|
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
# Note, we very explictly fallback to the JSON gem when we receive unknown options.
|
40
|
+
# Unknown options may be required for security reasons (e.g. escape_slash: true)
|
41
|
+
# so ignoring them could lead to security vulnerabilities.
|
42
|
+
class << self
|
43
|
+
def load(string, proc = nil, options = nil)
|
44
|
+
if proc.nil? && options.nil?
|
45
|
+
DEFAULT_CODER.load(string)
|
46
|
+
else
|
47
|
+
GEM.load(string, proc, options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse(string, opts = nil)
|
52
|
+
if opts.nil?
|
53
|
+
DEFAULT_CODER.load(string)
|
54
|
+
else
|
55
|
+
GEM.load(string, options)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def dump(object, anIO = nil, limit = nil)
|
60
|
+
if anIO.nil? && limit.nil?
|
61
|
+
DUMP_CODER.dump(object)
|
62
|
+
else
|
63
|
+
GEM.dump(object, anIO, limit)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def generate(object, opts = nil)
|
68
|
+
if opts.nil?
|
69
|
+
GENERATE_CODER.dump(object)
|
70
|
+
else
|
71
|
+
GEM.generate(object, opts)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def method_missing(name, *args)
|
78
|
+
GEM.public_send(name, *args)
|
79
|
+
end
|
80
|
+
ruby2_keywords :method_missing
|
81
|
+
|
82
|
+
def respond_to_missing?(name, include_private = false)
|
83
|
+
GEM.respond_to?(name, include_private)
|
84
|
+
end
|
30
85
|
end
|
31
86
|
end
|
32
87
|
end
|
33
|
-
|
34
|
-
[Hash, Array, String, Integer, Float, TrueClass, FalseClass, NilClass].each do |klass|
|
35
|
-
klass.include RapidJSON::JSONGemCompact
|
36
|
-
end
|
data/lib/rapidjson/version.rb
CHANGED
data/lib/rapidjson.rb
CHANGED
@@ -1,9 +1,54 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "rapidjson/version"
|
4
|
-
require_relative "rapidjson/rapidjson"
|
5
4
|
|
6
|
-
module
|
5
|
+
module RapidJSON
|
7
6
|
class Error < StandardError; end
|
8
|
-
|
7
|
+
|
8
|
+
Fragment = Struct.new(:to_json)
|
9
|
+
|
10
|
+
class Coder
|
11
|
+
def initialize(pretty: false, allow_nan: false, &to_json)
|
12
|
+
@pretty = pretty
|
13
|
+
@to_json_proc = to_json
|
14
|
+
@allow_nan = allow_nan
|
15
|
+
end
|
16
|
+
|
17
|
+
def dump(object)
|
18
|
+
_dump(object, @pretty, @to_json_proc, @allow_nan)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
require_relative "rapidjson/rapidjson"
|
24
|
+
|
25
|
+
module RapidJSON
|
26
|
+
class << self
|
27
|
+
def load(string)
|
28
|
+
DEFAULT_CODER.load(string)
|
29
|
+
end
|
30
|
+
alias_method :parse, :load
|
31
|
+
|
32
|
+
def dump(object)
|
33
|
+
DEFAULT_CODER.dump(object)
|
34
|
+
end
|
35
|
+
alias_method :encode, :dump
|
36
|
+
|
37
|
+
def pretty_encode(object)
|
38
|
+
PRETTY_CODER.dump(object)
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid_json?(string)
|
42
|
+
DEFAULT_CODER.valid_json?(string)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
DEFAULT_CODER = Coder.new
|
47
|
+
private_constant :DEFAULT_CODER
|
48
|
+
|
49
|
+
PRETTY_CODER = Coder.new(pretty: true)
|
50
|
+
private_constant :PRETTY_CODER
|
9
51
|
end
|
52
|
+
|
53
|
+
require "rapidjson/json_gem"
|
54
|
+
require "rapidjson/active_support_encoder"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapidjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hawthorn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Fast JSON encoder/decoder based using RapidJSON
|
14
14
|
email:
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- ext/rapidjson/rapidjson/include/rapidjson/uri.h
|
68
68
|
- ext/rapidjson/rapidjson/include/rapidjson/writer.h
|
69
69
|
- lib/rapidjson.rb
|
70
|
+
- lib/rapidjson/active_support_encoder.rb
|
70
71
|
- lib/rapidjson/json_gem.rb
|
71
72
|
- lib/rapidjson/version.rb
|
72
73
|
homepage: https://github.com/jhawthorn/rapidjson
|
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
92
|
- !ruby/object:Gem::Version
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
|
-
rubygems_version: 3.
|
95
|
+
rubygems_version: 3.4.10
|
95
96
|
signing_key:
|
96
97
|
specification_version: 4
|
97
98
|
summary: Fast JSON encoder/decoder based using RapidJSON
|