rapidjson 0.2.3 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e6a9c6ee55bf39622dca0dec0df0edb42de2f121dfd81b6711e3e730c7beb90
4
- data.tar.gz: 2d3f80f79aa7e61cc0340f253891bfb4c6f67d3c80c1bf8a6e23b132d7dd8334
3
+ metadata.gz: 66b8d422b4ba65041b7eae2691bcc909200f5de6bc10e4f5666fd5af4cc3eb96
4
+ data.tar.gz: 777b97d64191635565e01998b74b505493615d1afe5dd0803ce89ac2f1093451
5
5
  SHA512:
6
- metadata.gz: f0e242022084c035eec3fc8deede180bf390f9275aae83bf923ee7ab2076bb5abc1860eded72bc6c43aa8eb9803d492b7477f1923fa343cc2348d72dff809872
7
- data.tar.gz: 34092b2851dade4bf8adcec4dcf8e8fe66a561a41910c9ff99ac721d6aad48d6340bde69f586973d1f004583aa5eb5c5d278117375418edfe0365a91f339a963
6
+ metadata.gz: bc7821fd5c479fcb6f4609b935fd52c27542a89cde1e0242a794a247b8d4f6ba391f893ffbc75ad6e3d973f5d4149cea92d85e2543386e145b6bfb5f48092bbd
7
+ data.tar.gz: 98f5655e8863c56f103a1793b5517174dcd62064deccdbf86304fee89c08fa494e76c6f0fd1f367db54448bc68d27d3a21b4fb6034ffec99e8ad2a4c7a9e619d
data/README.md CHANGED
@@ -2,7 +2,7 @@
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.
5
+ ActiveSupport integration, `json` gem emulation, and no monkey patches.
6
6
 
7
7
  ## Installation
8
8
 
@@ -44,24 +44,24 @@ RapidJSON.pretty_encode(json_string)
44
44
  # }
45
45
  ```
46
46
 
47
- By default the encoder is "strict" and will raise an exception
47
+ By default the encoder is "strict" and will raise an exception.
48
48
 
49
49
  ## ActiveSupport
50
50
 
51
51
  RapidJSON provides a drop-in replacement ActiveSupport encoder, with very good compatibility.
52
52
  Add the following to an initializer to opt-in.
53
53
 
54
- ```
54
+ ```ruby
55
55
  # config/initializers/rapidjson.rb
56
56
 
57
57
  ActiveSupport::JSON::Encoding.json_encoder = RapidJSON::ActiveSupportEncoder
58
58
  ```
59
59
 
60
- This makes `model.to_json` ~15x faster, and `nested_hash.to_json` ~27x faster (compred using Rails 7.0)
60
+ This makes `model.to_json` ~15x faster, and `nested_hash.to_json` ~27x faster (compared using Rails 7.0)
61
61
 
62
62
  ## JSON gem compatibility
63
63
 
64
- Contrary to some other JSON libraries, `RapidJSON` doesn't provice a monkey patch to entirely replace the stdlib JSON gem.
64
+ Contrary to some other JSON libraries, `RapidJSON` doesn't provide a monkey patch to entirely replace the stdlib JSON gem.
65
65
 
66
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
67
 
@@ -86,7 +86,7 @@ By default RapidJSON will only encode "JSON-ready" types: `Hash`, `Array`, `Inte
86
86
 
87
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
88
 
89
- ```
89
+ ```ruby
90
90
  RapidJSON::Coder.new do |object, is_key|
91
91
  object.to_s # Convert any unknown object to string
92
92
  end
@@ -105,11 +105,11 @@ Unless there's good reason, it's probably best sticking with the standard `json`
105
105
  However this library has a few performance advantages:
106
106
 
107
107
  * JSON parsing
108
- * Performance is achieved mostly through using RapidJSON one of the fastest open source JSON parsing libraries. It supports SIMD (SSE2, SSE4.2, NEON), avoids allocated memory, and has been honed to be if not the fastest library (that honour likely going to simdjson) the library to beat for JSON performance.
108
+ * Performance is achieved mostly through using RapidJSON one of the fastest open source JSON parsing libraries. It supports SIMD (SSE2, SSE4.2, NEON), avoids allocated memory, and has been honed to be if not the fastest library (that honour likely going to simdjson), the library to beat for JSON performance.
109
109
  * Object allocation
110
110
  * Wherever possible we avoid allocating objects. When generating JSON, RapidJSON will write the emitted JSON directly into the buffer of a Ruby string. (This is an optimization most Ruby JSON libraries will have)
111
111
  * When parsing JSON we parse directly form the source string with a single copy
112
- * When building a Hash for a JSON object, we use an fstrings (dedup'd and frozen strings) as the key
112
+ * When building a Hash for a JSON object, we use `fstrings` (dedup'd and frozen strings) as the key
113
113
  * Whenever possible we build Ruby objects from C types (int, char \*, double) rather than constructing intermediate Ruby string objects.
114
114
 
115
115
  Many of these optimization can be found in all popular Ruby JSON libraries
@@ -158,4 +158,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
158
158
 
159
159
  ## Code of Conduct
160
160
 
161
- Everyone interacting in the Rapidjson project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/rapidjson/blob/main/CODE_OF_CONDUCT.md).
161
+ Everyone interacting in the RapidJSON project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/rapidjson/blob/main/CODE_OF_CONDUCT.md).
@@ -77,6 +77,57 @@ valid_json_p(VALUE _self, VALUE string) {
77
77
  return Qtrue;
78
78
  }
79
79
 
80
+ static bool is_json_ready(VALUE obj);
81
+
82
+ static int is_json_ready_hash_i(VALUE key, VALUE val, VALUE arg) {
83
+ bool *result = (bool *)arg;
84
+
85
+ if (!RB_TYPE_P(key, T_STRING) && !RB_TYPE_P(key, T_SYMBOL)) {
86
+ *result = false;
87
+ return ST_STOP;
88
+ }
89
+ if (!is_json_ready(val)) {
90
+ *result = false;
91
+ return ST_STOP;
92
+ }
93
+ return ST_CONTINUE;
94
+ }
95
+
96
+ static bool
97
+ is_json_ready(VALUE obj) {
98
+ switch(rb_type(obj)) {
99
+ case T_NIL:
100
+ case T_FALSE:
101
+ case T_TRUE:
102
+ case T_FIXNUM:
103
+ case T_BIGNUM:
104
+ case T_FLOAT:
105
+ case T_SYMBOL:
106
+ case T_STRING:
107
+ return true;
108
+ case T_HASH:
109
+ {
110
+ bool result = true;
111
+ rb_hash_foreach(obj, is_json_ready_hash_i, (VALUE)&result);
112
+ return result;
113
+ }
114
+ case T_ARRAY:
115
+ for (int i = 0; i < RARRAY_LEN(obj); i++) {
116
+ if (!is_json_ready(RARRAY_AREF(obj, i))) {
117
+ return false;
118
+ }
119
+ }
120
+ return true;
121
+ default:
122
+ return false;
123
+ }
124
+ }
125
+
126
+ static VALUE
127
+ json_ready_p(VALUE _self, VALUE obj) {
128
+ return is_json_ready(obj) ? Qtrue : Qfalse;
129
+ }
130
+
80
131
  extern "C" void
81
132
  Init_rapidjson(void)
82
133
  {
@@ -94,4 +145,5 @@ Init_rapidjson(void)
94
145
  rb_eEncodeError = rb_define_class_under(rb_mRapidJSON, "EncodeError", rb_eRapidJSONError);
95
146
 
96
147
  rb_define_singleton_method(rb_mRapidJSON, "json_escape", escape_json, 1);
148
+ rb_define_singleton_method(rb_mRapidJSON, "json_ready?", json_ready_p, 1);
97
149
  }
@@ -122,7 +122,12 @@ class RubyObjectEncoder {
122
122
  }
123
123
  }
124
124
  } else {
125
- writer.Double(f);
125
+ // TODO: We should avoid relying on to_s and do this conversion
126
+ // ourselves. However it's difficult to get the exact same rounding
127
+ // and truncation that Ruby uses.
128
+ VALUE str = rb_funcall(v, rb_intern("to_s"), 0);
129
+ Check_Type(str, T_STRING);
130
+ encode_raw_json_str(str);
126
131
  }
127
132
  }
128
133
 
@@ -14,7 +14,9 @@ module RapidJSON
14
14
  # Encode the given object into a JSON string
15
15
  def encode(value)
16
16
  if @options && !@options.empty?
17
- value = value.as_json(@options.dup)
17
+ if !RapidJSON.json_ready?(value) || @options.key?(:only) || @options.key?(:except)
18
+ value = value.as_json(@options.dup)
19
+ end
18
20
  end
19
21
  json = @coder.dump(value)
20
22
  if ActiveSupport::JSON::Encoding.escape_html_entities_in_json
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RapidJSON
4
- VERSION = "0.2.3"
4
+ VERSION = "0.4.0"
5
5
  end
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.2.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-27 00:00:00.000000000 Z
11
+ date: 2024-09-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fast JSON encoder/decoder based using RapidJSON
14
14
  email:
@@ -71,14 +71,14 @@ files:
71
71
  - lib/rapidjson/active_support_encoder.rb
72
72
  - lib/rapidjson/json_gem.rb
73
73
  - lib/rapidjson/version.rb
74
- homepage: https://github.com/jhawthorn/rapidjson
74
+ homepage: https://github.com/jhawthorn/rapidjson-ruby
75
75
  licenses:
76
76
  - MIT
77
77
  metadata:
78
- homepage_uri: https://github.com/jhawthorn/rapidjson
79
- source_code_uri: https://github.com/jhawthorn/rapidjson
80
- changelog_uri: https://github.com/jhawthorn/rapidjson
81
- post_install_message:
78
+ homepage_uri: https://github.com/jhawthorn/rapidjson-ruby
79
+ source_code_uri: https://github.com/jhawthorn/rapidjson-ruby
80
+ changelog_uri: https://github.com/jhawthorn/rapidjson-ruby
81
+ post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths:
84
84
  - lib
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.4.10
97
- signing_key:
96
+ rubygems_version: 3.5.16
97
+ signing_key:
98
98
  specification_version: 4
99
99
  summary: Fast JSON encoder/decoder based using RapidJSON
100
100
  test_files: []