oj 3.17.3 → 3.17.8

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -0
  3. data/ext/oj/cache.c +72 -39
  4. data/ext/oj/cache.h +5 -0
  5. data/ext/oj/circarray.c +6 -2
  6. data/ext/oj/code.c +38 -28
  7. data/ext/oj/code.h +2 -0
  8. data/ext/oj/compat.c +63 -31
  9. data/ext/oj/custom.c +43 -8
  10. data/ext/oj/dump.c +120 -41
  11. data/ext/oj/dump.h +10 -0
  12. data/ext/oj/dump_compat.c +19 -15
  13. data/ext/oj/dump_object.c +38 -12
  14. data/ext/oj/dump_strict.c +2 -1
  15. data/ext/oj/extconf.rb +7 -0
  16. data/ext/oj/fast.c +132 -72
  17. data/ext/oj/intern.c +80 -77
  18. data/ext/oj/mimic_json.c +5 -29
  19. data/ext/oj/object.c +47 -17
  20. data/ext/oj/odd.c +6 -2
  21. data/ext/oj/oj.c +393 -44
  22. data/ext/oj/oj.h +10 -3
  23. data/ext/oj/parse.c +75 -7
  24. data/ext/oj/parse.h +23 -2
  25. data/ext/oj/parser.c +219 -71
  26. data/ext/oj/parser.h +1 -1
  27. data/ext/oj/rails.c +188 -45
  28. data/ext/oj/reader.c +22 -6
  29. data/ext/oj/reader.h +1 -0
  30. data/ext/oj/resolve.c +16 -6
  31. data/ext/oj/resolve.h +9 -0
  32. data/ext/oj/rxclass.c +16 -0
  33. data/ext/oj/rxclass.h +1 -0
  34. data/ext/oj/safe.c +62 -48
  35. data/ext/oj/safe.h +14 -2
  36. data/ext/oj/saj.c +60 -17
  37. data/ext/oj/saj2.c +44 -28
  38. data/ext/oj/simd.h +5 -3
  39. data/ext/oj/sparse.c +23 -6
  40. data/ext/oj/stream_writer.c +19 -1
  41. data/ext/oj/strict.c +42 -4
  42. data/ext/oj/string_writer.c +40 -10
  43. data/ext/oj/usual.c +17 -5
  44. data/ext/oj/util.c +27 -0
  45. data/ext/oj/util.h +2 -1
  46. data/ext/oj/val_stack.c +11 -22
  47. data/ext/oj/val_stack.h +70 -30
  48. data/ext/oj/wab.c +17 -26
  49. data/lib/oj/generic_object.rb +82 -0
  50. data/lib/oj/json.rb +1 -71
  51. data/lib/oj/mimic.rb +0 -1
  52. data/lib/oj/version.rb +1 -1
  53. data/pages/Compatibility.md +1 -1
  54. data/pages/Custom.md +2 -3
  55. data/pages/InstallOptions.md +8 -6
  56. data/pages/Modes.md +1 -1
  57. data/pages/Options.md +5 -6
  58. data/pages/Parser.md +2 -2
  59. data/pages/Rails.md +6 -0
  60. data/pages/Security.md +21 -7
  61. metadata +17 -16
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Loaded on first use of JSON::GenericObject so that requiring Oj does not
4
+ # also require ostruct, which is no longer a default gem as of Ruby 4.0.
5
+ begin
6
+ require 'ostruct'
7
+ rescue LoadError
8
+ raise LoadError.new("JSON::GenericObject requires 'ostruct'. Add it to your Gemfile or install it with `gem install ostruct`.")
9
+ end
10
+
11
+ module JSON
12
+ # Taken directly from the json gem. Shamelessly copied. It is similar in
13
+ # some ways to the Oj::Bag class or the Oj::EasyHash class.
14
+ class GenericObject < OpenStruct
15
+ class << self
16
+ alias [] new
17
+
18
+ def json_creatable?
19
+ @json_creatable
20
+ end
21
+
22
+ attr_writer :json_creatable
23
+
24
+ def json_create(data)
25
+ data = data.dup
26
+ data.delete JSON.create_id
27
+ self[data]
28
+ end
29
+
30
+ def from_hash(object)
31
+ case
32
+ when object.respond_to?(:to_hash)
33
+ result = new
34
+ object.to_hash.each do |key, value|
35
+ result[key] = from_hash(value)
36
+ end
37
+ result
38
+ when object.respond_to?(:to_ary)
39
+ object.to_ary.map { |a| from_hash(a) }
40
+ else
41
+ object
42
+ end
43
+ end
44
+
45
+ def load(source, proc = nil, opts = {})
46
+ result = ::JSON.load(source, proc, opts.merge(:object_class => self))
47
+ result.nil? ? new : result
48
+ end
49
+
50
+ def dump(obj, *args)
51
+ ::JSON.dump(obj, *args)
52
+ end
53
+
54
+ end # self
55
+
56
+ self.json_creatable = false
57
+
58
+ def to_hash
59
+ table
60
+ end
61
+
62
+ def [](name)
63
+ __send__(name)
64
+ end unless method_defined?(:[])
65
+
66
+ def []=(name, value)
67
+ __send__("#{name}=", value)
68
+ end unless method_defined?(:[]=)
69
+
70
+ def |(other)
71
+ self.class[other.to_hash.merge(to_hash)]
72
+ end
73
+
74
+ def as_json(*)
75
+ { JSON.create_id => self.class.name }.merge to_hash
76
+ end
77
+
78
+ def to_json(*a)
79
+ as_json.to_json(*a)
80
+ end
81
+ end
82
+ end # JSON
data/lib/oj/json.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'ostruct'
2
1
  require 'oj/state'
3
2
 
4
3
  if defined?(JSON::PRETTY_STATE_PROTOTYPE)
@@ -113,76 +112,7 @@ unless defined?(JSON::PRETTY_STATE_PROTOTYPE)
113
112
  self.parser = ::JSON::Ext::Parser
114
113
  self.generator = ::JSON::Ext::Generator
115
114
 
116
- # Taken directly from the json gem. Shamelessly copied. It is similar in
117
- # some ways to the Oj::Bag class or the Oj::EasyHash class.
118
- class GenericObject < OpenStruct
119
- class << self
120
- alias [] new
121
-
122
- def json_creatable?
123
- @json_creatable
124
- end
125
-
126
- attr_writer :json_creatable
127
-
128
- def json_create(data)
129
- data = data.dup
130
- data.delete JSON.create_id
131
- self[data]
132
- end
133
-
134
- def from_hash(object)
135
- case
136
- when object.respond_to?(:to_hash)
137
- result = new
138
- object.to_hash.each do |key, value|
139
- result[key] = from_hash(value)
140
- end
141
- result
142
- when object.respond_to?(:to_ary)
143
- object.to_ary.map { |a| from_hash(a) }
144
- else
145
- object
146
- end
147
- end
148
-
149
- def load(source, proc = nil, opts = {})
150
- result = ::JSON.load(source, proc, opts.merge(:object_class => self))
151
- result.nil? ? new : result
152
- end
153
-
154
- def dump(obj, *args)
155
- ::JSON.dump(obj, *args)
156
- end
157
-
158
- end # self
159
-
160
- self.json_creatable = false
161
-
162
- def to_hash
163
- table
164
- end
165
-
166
- def [](name)
167
- __send__(name)
168
- end unless method_defined?(:[])
169
-
170
- def []=(name, value)
171
- __send__("#{name}=", value)
172
- end unless method_defined?(:[]=)
173
-
174
- def |(other)
175
- self.class[other.to_hash.merge(to_hash)]
176
- end
177
-
178
- def as_json(*)
179
- { JSON.create_id => self.class.name }.merge to_hash
180
- end
181
-
182
- def to_json(*a)
183
- as_json.to_json(*a)
184
- end
185
- end
115
+ autoload :GenericObject, 'oj/generic_object'
186
116
 
187
117
  end # JSON
188
118
  end
data/lib/oj/mimic.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: false
2
2
 
3
3
  require 'bigdecimal'
4
- require 'ostruct'
5
4
 
6
5
  module Oj
7
6
 
data/lib/oj/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Oj
2
2
  # Current version of the module.
3
- VERSION = '3.17.3'
3
+ VERSION = '3.17.8'
4
4
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  **Ruby**
4
4
 
5
- Oj is compatible with Ruby 2.4+ and RBX.
5
+ Oj is compatible with Ruby 2.7+ and RBX.
6
6
  Support for JRuby has been removed as JRuby no longer supports C extensions and
7
7
  there are bugs in the older versions that are not being fixed.
8
8
 
data/pages/Custom.md CHANGED
@@ -8,9 +8,8 @@ for object dumping and loading. The `:compat` mode mimic the json gem
8
8
  including methods called for encoding and inconsistencies between
9
9
  `JSON.dump()`, `JSON.generate()`, and `JSON()`.
10
10
 
11
- The `:custom` mode is the default mode. It can be configured either by passing
12
- options to the `Oj.dump()` and `Oj.load()` methods or by modifying the default
13
- options.
11
+ The `:custom` mode can be configured either by passing options to the
12
+ `Oj.dump()` and `Oj.load()` methods or by modifying the default options.
14
13
 
15
14
  The ability to create objects from JSON object elements is supported and
16
15
  considers the `:create_additions` option. Special treatment is given to the
@@ -10,11 +10,13 @@ To enable Oj trace feature, it uses `--enable-trace-log` option when installing
10
10
  Then, the trace logs will be displayed when `:trace` option is set to `true`.
11
11
 
12
12
 
13
- ### Enable SIMD instructions
13
+ ### SIMD instructions
14
14
 
15
- ```
16
- $ gem install oj -- --with-sse42
17
- ```
15
+ SIMD optimizations are enabled automatically, so no install option is
16
+ required. At runtime Oj detects the CPU features that are available and
17
+ selects the best implementation: SSE4.2 or SSE2 on x86 / x86_64, NEON on ARM,
18
+ and a scalar fallback when none is available.
18
19
 
19
- To enable the use of SIMD instructions in Oj, it uses the `--with-sse42` option when installing the gem.
20
- This will enable the use of the SSE4.2 instructions in the internal.
20
+ Earlier versions required a `--with-sse42` option at install time to turn on
21
+ SSE4.2. That option has been removed (#982); SSE4.2 is now selected
22
+ automatically when the CPU supports it.
data/pages/Modes.md CHANGED
@@ -121,7 +121,7 @@ information.
121
121
  | :object_nl | String | | | x | x | | x | |
122
122
  | :omit_nil | Boolean | x | x | x | x | x | x | |
123
123
  | :quirks_mode | Boolean | | | 6 | | | x | |
124
- | :safe | String | | | x | | | | |
124
+ | :safe | Boolean | | | x | | | | |
125
125
  | :second_precision | Fixnum | | | | | x | x | |
126
126
  | :space | String | | | x | x | | x | |
127
127
  | :space_before | String | | | x | x | | x | |
data/pages/Options.md CHANGED
@@ -213,8 +213,7 @@ for json gem compatibility.
213
213
 
214
214
  Primary behavior for loading and dumping. The :mode option controls which
215
215
  other options are in effect. For more details see the {file:Modes.md} page. By
216
- default Oj uses the :custom mode which is provides the highest degree of
217
- customization.
216
+ default Oj uses the :object mode.
218
217
 
219
218
  ### :nan [Symbol]
220
219
 
@@ -249,6 +248,10 @@ integer gives better performance.
249
248
 
250
249
  If true, Hash and Object attributes with nil values are omitted.
251
250
 
251
+ ### :omit_null_byte [Boolean]
252
+
253
+ If true, null bytes in strings will be omitted when dumping.
254
+
252
255
  ### :quirks_mode [Boolean]
253
256
 
254
257
  Allow single JSON values instead of documents, default is true (allow). This
@@ -265,10 +268,6 @@ to true.
265
268
 
266
269
  The number of digits after the decimal when dumping the seconds of time.
267
270
 
268
- ### :skip_null_byte [Boolean]
269
-
270
- If true, null bytes in strings will be omitted when dumping.
271
-
272
271
  ### :space
273
272
 
274
273
  String inserted after the ':' character when dumping a JSON object. The
data/pages/Parser.md CHANGED
@@ -280,7 +280,7 @@ Oj::Parser.usual 0.452 110544.876
280
280
  JSON::Ext 1.009 49555.094
281
281
  ```
282
282
 
283
- The `Oj::Parser.new(:saj)` is **1.55** times faster than `Oj.load` and
283
+ The `Oj::Parser.new(:usual)` is **1.55** times faster than `Oj.load` and
284
284
  **2.23** times faster than the JSON gem.
285
285
 
286
286
  ### Object
@@ -298,7 +298,7 @@ Oj::Parser.usual 0.071 703502.033
298
298
  JSON::Ext 0.401 124638.859
299
299
  ```
300
300
 
301
- The `Oj::Parser.new(:saj)` is **3.17** times faster than
301
+ The `Oj::Parser.new(:usual)` is **3.17** times faster than
302
302
  `Oj.compat_load` and **5.64** times faster than the JSON gem.
303
303
 
304
304
  ## Summary
data/pages/Rails.md CHANGED
@@ -89,6 +89,12 @@ The classes that can be put in optimized mode and are optimized when
89
89
  * any class inheriting from ActiveRecord::Base
90
90
  * any other class where all attributes should be dumped
91
91
 
92
+ Both `Oj::Rails` and each `Oj::Rails::Encoder` have `optimize()`, `deoptimize()`, and `optimized?()` methods. The module level methods change the setting for the whole process. An encoder follows those process wide settings until `optimize()` or `deoptimize()` is called on the encoder itself. From then on the encoder keeps a set of its own and later module level changes do not reach it.
93
+
94
+ Options work the same way. An encoder reads `Oj.default_options` when it encodes, so later changes to the defaults reach it, unless it was given options of its own, in which case it keeps the copy it took when it was created. An encoder counts as having options of its own only if the hash it was built with names at least one option Oj knows. A hash of nothing but keys Oj does not have, such as the `escape: false` ActiveSupport 8.1 builds one of its cached encoders with, leaves the encoder on the defaults.
95
+
96
+ Once a class is optimized its `as_json()` method is no longer called, so an application that defines its own `as_json()` on an optimized class, `BigDecimal` for example, will not see it used. Call `Oj::Rails.deoptimize(BigDecimal)` after `Oj.optimize_rails` to put that one class back on the `as_json()` path and leave the rest optimized.
97
+
92
98
  The ActiveSupport decoder is the `JSON.parse()` method. Calling the
93
99
  `Oj::Rails.set_decoder()` method replaces that method with the Oj equivalent.
94
100
 
data/pages/Security.md CHANGED
@@ -11,10 +11,24 @@ auto defined is used with an untrusted source. The `Oj.safe_load()` method
11
11
  sets and uses the most strict and safest options. It should be used by
12
12
  developers who find it difficult to understand the options available in Oj.
13
13
 
14
- The options in Oj are designed to provide flexibility to the developer. This
15
- flexibility allows Objects to be serialized and deserialized. No methods are
16
- ever called on these created Objects but that does not stop the developer from
17
- calling methods on them. As in any system, check your inputs before working with
18
- them. Taking an arbitrary `String` from a user and evaluating it is never a good
19
- idea from an unsecure source. The same is true for `Object` attributes as they
20
- are not more than `String`s. Always check inputs from untrusted sources.
14
+ The options in Oj are designed to provide flexibility to the developer. This flexibility allows Objects to be serialized and deserialized. An Object is deserialized by allocating it without calling its initializer and then setting its instance variables. A few methods are called on what that builds: `exception` and `set_backtrace` on an `Exception`, and `replace` on a subclass of `Hash`, `Array` or `String` for a `self` key. As in any system, check your inputs before working with them. Taking an arbitrary `String` from a user and evaluating it is never a good idea from an unsecure source. The same is true for `Object` attributes as they are not more than `String`s. Always check inputs from untrusted sources.
15
+
16
+ ## The mode a document is loaded in
17
+
18
+ `Oj.load()` uses the `:object` mode unless the mode is changed. That mode takes the class of an Object from the document, so the document decides which of the classes already loaded in the process is allocated and what its instance variables are. Classes are looked up and not created unless `:auto_define` is turned on.
19
+
20
+ For a document from an untrusted source, ask for a mode that does not do that:
21
+
22
+ ```ruby
23
+ Oj.load(json, mode: :strict)
24
+ Oj.strict_load(json)
25
+ Oj.safe_load(json)
26
+ ```
27
+
28
+ or set it for the process:
29
+
30
+ ```ruby
31
+ Oj.default_options = {mode: :strict}
32
+ ```
33
+
34
+ `Oj.object_load()` is the explicit form of the current default and stays in `:object` mode whatever the default is set to.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.17.3
4
+ version: 3.17.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '5'
40
+ - !ruby/object:Gem::Dependency
41
+ name: ostruct
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0.2'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0.2'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: rake-compiler
42
56
  requirement: !ruby/object:Gem::Requirement
@@ -71,20 +85,6 @@ dependencies:
71
85
  - - "~>"
72
86
  - !ruby/object:Gem::Version
73
87
  version: '3.0'
74
- - !ruby/object:Gem::Dependency
75
- name: ostruct
76
- requirement: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: '0.2'
81
- type: :runtime
82
- prerelease: false
83
- version_requirements: !ruby/object:Gem::Requirement
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: '0.2'
88
88
  description: The fastest JSON parser and object serializer.
89
89
  email: peter@ohler.com
90
90
  executables: []
@@ -184,6 +184,7 @@ files:
184
184
  - lib/oj/bag.rb
185
185
  - lib/oj/easy_hash.rb
186
186
  - lib/oj/error.rb
187
+ - lib/oj/generic_object.rb
187
188
  - lib/oj/json.rb
188
189
  - lib/oj/mimic.rb
189
190
  - lib/oj/saj.rb
@@ -231,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
232
  - !ruby/object:Gem::Version
232
233
  version: '0'
233
234
  requirements: []
234
- rubygems_version: 4.0.3
235
+ rubygems_version: 4.0.17
235
236
  specification_version: 4
236
237
  summary: A fast JSON parser and serializer.
237
238
  test_files: []