rapidjson 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17354e68c40c730955b333eacdc5232255f5c0e12605a2e35ea0a90dd84def19
4
- data.tar.gz: c3d95c72fb9e4416f33007bf7076ed616e82bd138b2637e0a2e0a57a5b55faed
3
+ metadata.gz: 722ff2bcb2ebfc8ab861d5066e53e938933bd03d2e7f4bca7662233fa0a8a314
4
+ data.tar.gz: 59d25e68af12d81bc3b5517a88de320a76a72c6b5d7cc9d26758084bd604b9fe
5
5
  SHA512:
6
- metadata.gz: 5d3dc6b671e1cc4110c457a8551fdf05b62ef03ff355a93531406b32853fd0b9174bc6a9ae3b743f891fd1ff4fe8eb95506b36bff9753ca1ed8f60970e694ed7
7
- data.tar.gz: 26195d47a4c2a68b190f0d46da8e09d2df7125b89ad53478758c9db74bf59e65c5a0a6b9fb10f47372da45fe25e269a0c6e48c44b36af10f3f22222739e70497
6
+ metadata.gz: 4eea208e695986e0e312c3794ba44852057f86a8159c4d862518482d263f2d13ef94fff481c38e2d7a79f881a13cef08e65eff4d49ded5c85378891bc79dab85
7
+ data.tar.gz: cafbbb2e33c4adecdfac50d1fc0bb34e3c1b019828af29714b0005a8a24955c3914c79e7a9690161589597b3df12928d6adddad8887014fd48fb36b58525f25d
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  (Maybe) Ruby's fastest JSON library! Built using the [RapidJSON C++ library](https://rapidjson.org/)
4
4
 
5
+ No monkey patches, ActiveSupport integration, `json` gem emulation.
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
@@ -42,6 +44,58 @@ RapidJSON.pretty_encode(json_string)
42
44
  # }
43
45
  ```
44
46
 
47
+ By default the encoder is "strict" and will raise an exception
48
+
49
+ ## ActiveSupport
50
+
51
+ RapidJSON provides a drop-in replacement ActiveSupport encoder, with very good compatibility.
52
+ Add the following to an initializer to opt-in.
53
+
54
+ ```
55
+ # config/initializers/rapidjson.rb
56
+
57
+ ActiveSupport::JSON::Encoding.json_encoder = RapidJSON::ActiveSupportEncoder
58
+ ```
59
+
60
+ This makes `model.to_json` ~15x faster, and `nested_hash.to_json` ~27x faster (compred using Rails 7.0)
61
+
62
+ ## JSON gem compatibility
63
+
64
+ Contrary to some other JSON libraries, `RapidJSON` doesn't provice a monkey patch to entirely replace the stdlib JSON gem.
65
+
66
+ However it does provide a module that behave like the stdlib JSON gem and that can be used to monkey patch existing code.
67
+
68
+ ```ruby
69
+ module SomeLibrary
70
+ def do_stuff(payload)
71
+ JSON.parse(payload)
72
+ end
73
+ end
74
+ ```
75
+
76
+ ```ruby
77
+ SomeLibrary::JSON = RapidJSON::JSONGem
78
+ ```
79
+
80
+ Note that this module only use `RapidJSON` when it's certain it is safe to do so. If the JSON gem is called with
81
+ some options that `RapidJSON` doesn't support, it automatically fallbacks to calling the JSON gem.
82
+
83
+ ## Advanced usage
84
+
85
+ By default RapidJSON will only encode "JSON-ready" types: `Hash`, `Array`, `Integer`, `Float`, `String`, `Symbol`, `true`, `false`, and `nil`.
86
+
87
+ RapidJSON::Coder can be initialized with a block which allows the behaviour to be customized. This is how the ActiveSupport encoder and JSON compatibility above are implemented! Just using Ruby :heart:.
88
+
89
+ ```
90
+ RapidJSON::Coder.new do |object, is_key|
91
+ object.to_s # Convert any unknown object to string
92
+ end
93
+ ```
94
+
95
+ The block is called only for unrecognized types. The return value is expected to be a "JSON ready" type which will then be encoded.
96
+
97
+ One additional special type is `RapidJSON::Fragment`, which is interpreted as an existing JSON-encoded string. This can be used to efficiently embed an existing JSON document, or to provide compatibility.
98
+
45
99
  ## Performance
46
100
 
47
101
  Your current JSON parser/encoder is probably fine.
@@ -88,27 +142,6 @@ I spent a week working on YAJL/yajl-ruby, and though I really liked the library,
88
142
 
89
143
  However, if you're happy with your current Ruby JSON library (including `json`) you should keep using it. They're all very good.
90
144
 
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
-
112
145
  ## Development
113
146
 
114
147
  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.
@@ -12,9 +12,16 @@ class RubyStringBuffer {
12
12
  mem = RSTRING_PTR(ruby_string);
13
13
  }
14
14
 
15
- void Reserve(size_t size) {
16
- if (capacity - used < size) {
17
- resize(capacity * 2);
15
+ void Reserve(size_t want) {
16
+ if (capacity - used < want) {
17
+ size_t new_capacity = capacity;
18
+ while (new_capacity - used < want) {
19
+ if (new_capacity >= SIZE_MAX / 2) {
20
+ ruby_malloc_size_overflow(capacity, 2);
21
+ }
22
+ new_capacity <<= 1;
23
+ }
24
+ resize(new_capacity);
18
25
  }
19
26
  }
20
27
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RapidJSON
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapidjson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.3.26
96
+ rubygems_version: 3.4.10
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Fast JSON encoder/decoder based using RapidJSON