oj 2.18.3 → 3.13.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1324 -0
- data/README.md +51 -204
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +49 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -68
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +221 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +231 -107
- data/ext/oj/custom.c +1125 -0
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +935 -2513
- data/ext/oj/dump.h +108 -0
- data/ext/oj/dump_compat.c +936 -0
- data/ext/oj/dump_leaf.c +164 -0
- data/ext/oj/dump_object.c +761 -0
- data/ext/oj/dump_strict.c +410 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -54
- data/ext/oj/err.h +52 -46
- data/ext/oj/extconf.rb +21 -30
- data/ext/oj/fast.c +1097 -1080
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +893 -0
- data/ext/oj/object.c +549 -620
- data/ext/oj/odd.c +155 -167
- data/ext/oj/odd.h +37 -63
- data/ext/oj/oj.c +1661 -2063
- data/ext/oj/oj.h +341 -270
- data/ext/oj/parse.c +974 -737
- data/ext/oj/parse.h +105 -97
- data/ext/oj/parser.c +1526 -0
- data/ext/oj/parser.h +90 -0
- data/ext/oj/rails.c +1504 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +141 -163
- data/ext/oj/reader.h +75 -113
- data/ext/oj/resolve.c +45 -93
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +143 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +447 -511
- data/ext/oj/saj2.c +348 -0
- data/ext/oj/scp.c +91 -138
- data/ext/oj/sparse.c +793 -644
- data/ext/oj/stream_writer.c +331 -0
- data/ext/oj/strict.c +145 -109
- data/ext/oj/string_writer.c +493 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +62 -70
- data/ext/oj/val_stack.h +95 -129
- data/ext/oj/validate.c +51 -0
- data/ext/oj/wab.c +622 -0
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +17 -8
- data/lib/oj/error.rb +10 -11
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +158 -19
- data/lib/oj/state.rb +132 -0
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +1 -31
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +327 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- data/test/activerecord/result_test.rb +32 -0
- data/test/activesupport4/decoding_test.rb +108 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +133 -0
- data/test/activesupport5/encoding_test.rb +500 -0
- data/test/activesupport5/encoding_test_cases.rb +98 -0
- data/test/activesupport5/test_helper.rb +72 -0
- data/test/activesupport5/time_zone_test_helpers.rb +39 -0
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +9 -0
- data/test/baz.rb +16 -0
- data/test/bug.rb +11 -46
- data/test/foo.rb +69 -16
- data/test/helper.rb +10 -1
- data/test/isolated/shared.rb +12 -8
- data/test/isolated/test_mimic_rails_after.rb +3 -3
- data/test/isolated/test_mimic_rails_before.rb +3 -3
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +153 -0
- data/test/json_gem/json_encoding_test.rb +107 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +397 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +26 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_compat.rb +30 -28
- data/test/perf_dump.rb +50 -0
- data/test/perf_object.rb +1 -1
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +30 -19
- data/test/perf_wab.rb +131 -0
- data/test/prec.rb +23 -0
- data/test/sample.rb +0 -1
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +219 -102
- data/test/test_custom.rb +533 -0
- data/test/test_fast.rb +107 -35
- data/test/test_file.rb +19 -25
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +72 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +357 -70
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +245 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +39 -2
- data/test/test_strict.rb +186 -7
- data/test/test_various.rb +160 -774
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +90 -2
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +194 -56
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/pages/Encoding.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Oj `:object` Mode Encoding
|
2
|
+
|
3
|
+
Object mode is for fast Ruby object serialization and deserialization. That
|
4
|
+
was the primary purpose of Oj when it was first developed. As such it is the
|
5
|
+
default mode unless changed in the Oj default options. In :object mode Oj
|
6
|
+
generates JSON that follows conventions which allow Class and other
|
7
|
+
information such as Object IDs for circular reference detection to be encoded
|
8
|
+
in a JSON document. The formatting follows these rules.
|
9
|
+
|
10
|
+
* JSON native types, true, false, nil, String, Hash, Array, and Number are
|
11
|
+
encoded normally.
|
12
|
+
|
13
|
+
* A Symbol is encoded as a JSON string with a preceding `':'` character.
|
14
|
+
|
15
|
+
* The `'^'` character denotes a special key value when in a JSON Object sequence.
|
16
|
+
|
17
|
+
* A Ruby String that starts with `':'`or the sequence `'^i'` or `'^r'` are
|
18
|
+
encoded by excaping the first character so that it appears as `'\u005e'` or
|
19
|
+
`'\u003a'` instead of `':'` or `'^'`.
|
20
|
+
|
21
|
+
* A `"^c"` JSON Object key indicates the value should be converted to a Ruby
|
22
|
+
class. The sequence `{"^c":"Oj::Bag"}` is read as the Oj::Bag class.
|
23
|
+
|
24
|
+
* A `"^t"` JSON Object key indicates the value should be converted to a Ruby
|
25
|
+
Time. The sequence `{"^t":1325775487.000000}` is read as Jan 5, 2012 at
|
26
|
+
23:58:07.
|
27
|
+
|
28
|
+
* A `"^o"` JSON Object key indicates the value should be converted to a Ruby
|
29
|
+
Object. The first entry in the JSON Object must be a class with the `"^o"`
|
30
|
+
key. After that each entry is treated as a variable of the Object where the
|
31
|
+
key is the variable name without the preceding `'@'`. An example is
|
32
|
+
`{"^o":"Oj::Bag","x":58,"y":"marbles"}`. `"^O"`is the same except that it
|
33
|
+
is for built in or odd classes that don't obey the normal Ruby
|
34
|
+
rules. Examples are Rational, Date, and DateTime.
|
35
|
+
|
36
|
+
* A `"^u"` JSON Object key indicates the value should be converted to a Ruby
|
37
|
+
Struct. The first entry in the JSON Object must be a class with the
|
38
|
+
`"^u"` key. After that each entry is is given a numeric position in the
|
39
|
+
struct and that is used as the key in the JSON Object. An example is
|
40
|
+
`{"^u":["Range",1,7,false]}`.
|
41
|
+
|
42
|
+
* When encoding an Object, if the variable name does not begin with an
|
43
|
+
`'@'`character then the name preceded by a `'~'` character. This occurs in
|
44
|
+
the Exception class. An example is `{"^o":"StandardError","~mesg":"A
|
45
|
+
Message","~bt":[".\/tests.rb:345:in 'test_exception'"]}`.
|
46
|
+
|
47
|
+
* If a Hash entry has a key that is not a String or Symbol then the entry is
|
48
|
+
encoded with a key of the form `"^#n"` where n is a hex number. The value
|
49
|
+
is an Array where the first element is the key in the Hash and the second
|
50
|
+
is the value. An example is `{"^#3":[2,5]}`.
|
51
|
+
|
52
|
+
* A `"^i"` JSON entry in either an Object or Array is the ID of the Ruby
|
53
|
+
Object being encoded. It is used when the :circular flag is set. It can
|
54
|
+
appear in either a JSON Object or in a JSON Array. In an Object the
|
55
|
+
`"^i"` key has a corresponding reference Fixnum. In an array the sequence
|
56
|
+
will include an embedded reference number. An example is
|
57
|
+
`{"^o":"Oj::Bag","^i":1,"x":["^i2",true],"me":"^r1"}`.
|
58
|
+
|
59
|
+
* A `"^r"`JSON entry in an Object is a references to a Object or Array that
|
60
|
+
already appears in the JSON String. It must match up with a previous
|
61
|
+
`"^i"` ID. An example is `{"^o":"Oj::Bag","^i":1,"x":3,"me":"^r1"}`.
|
62
|
+
|
63
|
+
* If an Array element is a String and starts with `"^i"` then the first
|
64
|
+
character, the `'^'` is encoded as a hex character sequence. An example is
|
65
|
+
`["\u005ei37",3]`.
|
data/pages/JsonGem.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# JSON Quickstart
|
2
|
+
|
3
|
+
To have Oj universally "take over" many methods on the JSON constant (`load`, `parse`, etc.) with
|
4
|
+
their faster Oj counterparts, in a mode that is compatible with the json gem:
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
Oj.mimic_JSON()
|
8
|
+
```
|
9
|
+
|
10
|
+
If the project does not already use the json gem, `JSON` will become available.
|
11
|
+
If the project does require the json gem, `Oj.mimic_JSON()` should be invoked after the
|
12
|
+
json gem has been required.
|
13
|
+
|
14
|
+
For more details and options, read on...
|
15
|
+
|
16
|
+
# Oj JSON Gem Compatibility
|
17
|
+
|
18
|
+
The `:compat` mode mimics the json gem. The json gem is built around the use
|
19
|
+
of the `to_json(*)` method defined for a class. Oj attempts to provide the
|
20
|
+
same functionality by being a drop in replacement for the 2.0.x version of the
|
21
|
+
json gem with a few exceptions. First a description of the json gem behavior
|
22
|
+
and then the differences between the json gem and the Oj.mimic_JSON behavior.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'oj'
|
26
|
+
|
27
|
+
Oj.mimic_JSON()
|
28
|
+
Oj.add_to_json(Array, BigDecimal, Complex, Date, DateTime, Exception, Hash, Integer, OpenStruct, Range, Rational, Regexp, Struct, Time)
|
29
|
+
# Alternativel just call without arguments to add all available.
|
30
|
+
# Oj.add_to_json()
|
31
|
+
```
|
32
|
+
|
33
|
+
The json gem monkey patches core and base library classes with a `to_json(*)`
|
34
|
+
method. This allows calls such as `obj.to_json()` to be used to generate a
|
35
|
+
JSON string. The json gem also provides the JSON.generate(), JSON.dump(), and
|
36
|
+
JSON() functions. These functions generally act the same with some exceptions
|
37
|
+
such as JSON.generate(), JSON(), and to_json raise an exception when
|
38
|
+
attempting to encode infinity while JSON.dump() returns a the string
|
39
|
+
"Infinity". The String class is also monkey patched with to_json_raw() and
|
40
|
+
to_json_raw_object(). Oj in mimic mode mimics this behavior including the
|
41
|
+
seemly inconsistent behavior with NaN and Infinity.
|
42
|
+
|
43
|
+
Any class can define a to_json() method and JSON.generate(), JSON.dump(), and
|
44
|
+
JSON() functions will call that method when an object of that type is
|
45
|
+
encountered when traversing a Hash or Array. The core classes monkey patches
|
46
|
+
can be over-ridden but unless the to_json() method is called directory the
|
47
|
+
to_json() method will be ignored. Oj in mimic mode follow the same logic,
|
48
|
+
|
49
|
+
The json gem includes additions. These additions change the behavior of some
|
50
|
+
library and core classes. These additions also add the as_json() method and
|
51
|
+
json_create() class method. They are activated by requiring the appropriate
|
52
|
+
files. As an example, to get the modified to_json() for the Rational class
|
53
|
+
this line would be added.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
require 'json/add/rational'
|
57
|
+
```
|
58
|
+
|
59
|
+
Oj in mimic mode does not include these files although it will support the
|
60
|
+
modified to_json() methods. In keeping with the goal of providing a faster
|
61
|
+
encoder Oj offers an alternative. To activate faster addition version of the
|
62
|
+
to_json() method call
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Oj.add_to_json(Rational)
|
66
|
+
```
|
67
|
+
|
68
|
+
To revert back to the unoptimized version, just remove the Oj flag on that
|
69
|
+
class.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Oj.remove_to_json(Rational)
|
73
|
+
```
|
74
|
+
|
75
|
+
The classes that can be added are:
|
76
|
+
|
77
|
+
* Array
|
78
|
+
* BigDecimal
|
79
|
+
* Complex
|
80
|
+
* Date
|
81
|
+
* DateTime
|
82
|
+
* Exception
|
83
|
+
* Hash
|
84
|
+
* Integer
|
85
|
+
* OpenStruct
|
86
|
+
* Range
|
87
|
+
* Rational
|
88
|
+
* Regexp
|
89
|
+
* Struct
|
90
|
+
* Time
|
91
|
+
|
92
|
+
The compatibility target version is 2.0.3. The json gem unit tests were used
|
93
|
+
to verify compatibility with a few changes to use Oj instead of the original
|
94
|
+
gem.
|
data/pages/Modes.md
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
# Oj Modes
|
2
|
+
|
3
|
+
Oj uses modes to switch the load and dump behavior. Initially Oj supported on
|
4
|
+
the :object mode which uses a format that allows Ruby object encoding and
|
5
|
+
decoding in a manner that lets almost any Ruby object be encoded and decoded
|
6
|
+
without monkey patching the object classes. From that start other demands were
|
7
|
+
made the were best met by giving Oj multiple modes of operation. The current
|
8
|
+
modes are:
|
9
|
+
|
10
|
+
- `:strict`
|
11
|
+
- `:null`
|
12
|
+
- `:compat` or `:json`
|
13
|
+
- `:rails`
|
14
|
+
- `:object`
|
15
|
+
- `:custom`
|
16
|
+
|
17
|
+
Since modes determine what the JSON output will look like and alternatively
|
18
|
+
what Oj expects when the `Oj.load()` method is called, mixing the output and
|
19
|
+
input mode formats will most likely not behave as intended. If the object mode
|
20
|
+
is used for producing JSON then use object mode for reading. The same is true
|
21
|
+
for each mode. It is possible to mix but only for advanced users.
|
22
|
+
|
23
|
+
## :strict Mode
|
24
|
+
|
25
|
+
Strict mode follows the JSON specifications and only supports the JSON native
|
26
|
+
types, Boolean, nil, String, Hash, Array, and Numbers are encoded as
|
27
|
+
expected. Encountering any other type causes an Exception to be raised. This
|
28
|
+
is the safest mode as it is just simple translation, no code outside Oj or the
|
29
|
+
core Ruby is execution on loading. Very few options are supported by this mode
|
30
|
+
other than formatting options.
|
31
|
+
|
32
|
+
## :null Mode
|
33
|
+
|
34
|
+
Null mode is similar to the :strict mode except that a JSON null is inserted
|
35
|
+
if a non-native type is encountered instead of raising an Exception.
|
36
|
+
|
37
|
+
## :compat or :json Mode
|
38
|
+
|
39
|
+
The `:compat` mode mimics the json gem. The json gem is built around the use
|
40
|
+
of the `to_json(*)` method defined for a class. Oj attempts to provide the
|
41
|
+
same functionality by being a drop in replacement with a few
|
42
|
+
exceptions. To universally replace many `JSON` methods with their faster Oj counterparts,
|
43
|
+
simply run `Oj.mimic_json`. [{file:JsonGem.md}](JsonGem.md) includes more details on
|
44
|
+
compatibility and use.
|
45
|
+
|
46
|
+
## :rails Mode
|
47
|
+
|
48
|
+
The `:rails` mode mimics the ActiveSupport version 5 encoder. Rails and
|
49
|
+
ActiveSupport are built around the use of the `as_json(*)` method defined for
|
50
|
+
a class. Oj attempts to provide the same functionality by being a drop in
|
51
|
+
replacement with a few exceptions. [{file:Rails.md}](Rails.md) includes
|
52
|
+
more details on compatibility and use.
|
53
|
+
|
54
|
+
## :object Mode
|
55
|
+
|
56
|
+
Object mode is for fast Ruby object serialization and deserialization. That
|
57
|
+
was the primary purpose of Oj when it was first developed. As such it is the
|
58
|
+
default mode unless changed in the Oj default options. In :object mode Oj
|
59
|
+
generates JSON that follows conventions which allow Class and other
|
60
|
+
information such as Object IDs for circular reference detection to be encoded
|
61
|
+
in a JSON document. The formatting follows the rules describe on the
|
62
|
+
[{file:Encoding.md}](Encoding.md) page.
|
63
|
+
|
64
|
+
## :custom Mode
|
65
|
+
|
66
|
+
Custom mode honors all options. It provides the most flexibility although it
|
67
|
+
can not be configured to be exactly like any of the other modes. Each mode has
|
68
|
+
some special aspect that makes it unique. For example, the `:object` mode has
|
69
|
+
it's own unique format for object dumping and loading. The `:compat` mode
|
70
|
+
mimic the json gem including methods called for encoding and inconsistencies
|
71
|
+
between `JSON.dump()`, `JSON.generate()`, and `JSON()`. More details on the
|
72
|
+
[{file:Custom.md}](Custom.md) page.
|
73
|
+
|
74
|
+
## :wab Mode
|
75
|
+
|
76
|
+
WAB mode ignores all options except the indent option. Performance of this
|
77
|
+
mode is on slightly better than the :strict and :null modes. It is included to
|
78
|
+
support the [WABuR](https://github.com/ohler55/wabur) project. More details on
|
79
|
+
the [{file:WAB.md}](WAB.md) page.
|
80
|
+
|
81
|
+
## Options Matrix
|
82
|
+
|
83
|
+
Not all options are available in all modes. The options matrix identifies the
|
84
|
+
options available in each mode. An `x` in the matrix indicates the option is
|
85
|
+
supported in that mode. A number indicates the footnotes describe additional
|
86
|
+
information.
|
87
|
+
|
88
|
+
| Option | type | :null | :strict | :compat | :rails | :object | :custom | :wab |
|
89
|
+
| ---------------------- | ------- | ------- | ------- | ------- | ------- | ------- | ------- | ------- |
|
90
|
+
| :allow_blank | Boolean | | | 1 | 1 | | x | |
|
91
|
+
| :allow_gc | Boolean | x | x | x | x | x | x | |
|
92
|
+
| :allow_invalid_unicode | Boolean | | | | | x | x | |
|
93
|
+
| :allow_nan | Boolean | | | x | | x | x | |
|
94
|
+
| :array_class | Class | | | x | x | | x | |
|
95
|
+
| :array_nl | String | | | | | | x | |
|
96
|
+
| :ascii_only | Boolean | x | x | 2 | 2 | x | x | |
|
97
|
+
| :auto_define | Boolean | | | | | x | x | |
|
98
|
+
| :bigdecimal_as_decimal | Boolean | | | | 3 | x | x | |
|
99
|
+
| :bigdecimal_load | Boolean | | | | | | x | |
|
100
|
+
| :compat_bigdecimal | Boolean | | | x | | | x | |
|
101
|
+
| :cache_keys | Boolean | x | x | x | x | | x | |
|
102
|
+
| :cache_strings | Fixnum | x | x | x | x | | x | |
|
103
|
+
| :circular | Boolean | x | x | x | x | x | x | |
|
104
|
+
| :class_cache | Boolean | | | | | x | x | |
|
105
|
+
| :create_additions | Boolean | | | x | x | | x | |
|
106
|
+
| :create_id | String | | | x | x | | x | |
|
107
|
+
| :empty_string | Boolean | | | | | | x | |
|
108
|
+
| :escape_mode | Symbol | | | | | | x | |
|
109
|
+
| :float_precision | Fixnum | x | x | | | | x | |
|
110
|
+
| :hash_class | Class | | | x | x | | x | |
|
111
|
+
| :ignore | Array | | | | | x | x | |
|
112
|
+
| :indent | Integer | x | x | 4 | 4 | x | x | x |
|
113
|
+
| :indent_str | String | | | x | x | | x | |
|
114
|
+
| :integer_range | Range | x | x | x | x | x | x | x |
|
115
|
+
| :match_string | Hash | | | x | x | | x | |
|
116
|
+
| :max_nesting | Fixnum | 5 | 5 | x | | 5 | 5 | |
|
117
|
+
| :mode | Symbol | - | - | - | - | - | - | |
|
118
|
+
| :nan | Symbol | | | | | | x | |
|
119
|
+
| :nilnil | Boolean | | | | | | x | |
|
120
|
+
| :object_class | Class | | | x | | | x | |
|
121
|
+
| :object_nl | String | | | x | x | | x | |
|
122
|
+
| :omit_nil | Boolean | x | x | x | x | x | x | |
|
123
|
+
| :quirks_mode | Boolean | | | 6 | | | x | |
|
124
|
+
| :safe | String | | | x | | | | |
|
125
|
+
| :second_precision | Fixnum | | | | | x | x | |
|
126
|
+
| :space | String | | | x | x | | x | |
|
127
|
+
| :space_before | String | | | x | x | | x | |
|
128
|
+
| :symbol_keys | Boolean | x | x | x | x | x | x | |
|
129
|
+
| :trace | Boolean | x | x | x | x | x | x | x |
|
130
|
+
| :time_format | Symbol | | | | | x | x | |
|
131
|
+
| :use_as_json | Boolean | | | | | | x | |
|
132
|
+
| :use_raw_json | Boolean | | | x | x | x | x | |
|
133
|
+
| :use_to_hash | Boolean | | | | | | x | |
|
134
|
+
| :use_to_json | Boolean | | | | | | x | |
|
135
|
+
--------------------------------------------------------------------------------------------------------
|
136
|
+
|
137
|
+
1. :allow_blank an alias for :nilnil.
|
138
|
+
|
139
|
+
2. The :ascii_only options is an undocumented json gem option.
|
140
|
+
|
141
|
+
3. By default the bigdecimal_as decimal is not set and the default encoding
|
142
|
+
for Rails is as a string. Setting the value to true will encode a
|
143
|
+
BigDecimal as a number which breaks compatibility.
|
144
|
+
Note: after version 3.11.3 both `Oj.generate` and `JSON.generate`
|
145
|
+
will not honour this option in Rails Mode, detais on https://github.com/ohler55/oj/pull/716.
|
146
|
+
|
147
|
+
4. The integer indent value in the default options will be honored by since
|
148
|
+
the json gem expects a String type the indent in calls to 'to_json()',
|
149
|
+
'Oj.generate()', or 'Oj.generate_fast()' expect a String and not an
|
150
|
+
integer.
|
151
|
+
|
152
|
+
5. The max_nesting option is for the json gem and rails only. It exists for
|
153
|
+
compatibility. For other Oj dump modes the maximum nesting is set to over
|
154
|
+
1000. If reference loops exist in the object being dumped then using the
|
155
|
+
`:circular` option is a far better choice. It adds a slight overhead but
|
156
|
+
detects an object that appears more than once in a dump and does not dump
|
157
|
+
that object a second time.
|
158
|
+
|
159
|
+
6. The quirks mode option is no longer supported in the most recent json
|
160
|
+
gem. It is supported by Oj for backward compatibility with older json gem
|
161
|
+
versions.
|
data/pages/Options.md
ADDED
@@ -0,0 +1,327 @@
|
|
1
|
+
# Oj Options
|
2
|
+
|
3
|
+
To change default serialization mode use the following form. Attempting to
|
4
|
+
modify the Oj.default_options Hash directly will not set the changes on the
|
5
|
+
actual default options but on a copy of the Hash:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
Oj.default_options = {:mode => :compat }
|
9
|
+
```
|
10
|
+
|
11
|
+
Another way to make use of options when calling load or dump methods is to
|
12
|
+
pass in a Hash with the options already set in the Hash. This is slightly less
|
13
|
+
efficient than setting the globals for many smaller JSON documents but does
|
14
|
+
provide a more thread safe approach to using custom options for loading and
|
15
|
+
dumping.
|
16
|
+
|
17
|
+
### Options for serializer and parser
|
18
|
+
|
19
|
+
### :allow_blank [Boolean]
|
20
|
+
|
21
|
+
If true a nil input to load will return nil and not raise an Exception.
|
22
|
+
|
23
|
+
### :allow_gc [Boolean]
|
24
|
+
|
25
|
+
Allow or prohibit GC during parsing, default is true (allow).
|
26
|
+
|
27
|
+
### :allow_invalid_unicode [Boolean]
|
28
|
+
|
29
|
+
Allow invalid unicode, default is false (don't allow).
|
30
|
+
|
31
|
+
### :allow_nan
|
32
|
+
|
33
|
+
Alias for the :nan option.
|
34
|
+
|
35
|
+
### :array_class [Class]
|
36
|
+
|
37
|
+
Class to use instead of Array on load.
|
38
|
+
|
39
|
+
### :array_nl
|
40
|
+
|
41
|
+
Trailer appended to the end of an array dump. The default is an empty
|
42
|
+
string. Primarily intended for json gem compatibility. Using just indent as an
|
43
|
+
integer gives better performance.
|
44
|
+
|
45
|
+
### :ascii_only
|
46
|
+
|
47
|
+
If true all non-ASCII character are escaped when dumping. This is the same as
|
48
|
+
setting the :escape_mode options to :ascii and exists for json gem
|
49
|
+
compatibility.
|
50
|
+
|
51
|
+
### :auto_define [Boolean]
|
52
|
+
|
53
|
+
Automatically define classes if they do not exist.
|
54
|
+
|
55
|
+
### :bigdecimal_as_decimal [Boolean]
|
56
|
+
|
57
|
+
If true dump BigDecimal as a decimal number otherwise as a String
|
58
|
+
|
59
|
+
### :bigdecimal_load [Symbol]
|
60
|
+
|
61
|
+
Determines how to load decimals.
|
62
|
+
|
63
|
+
- `:bigdecimal` convert all decimal numbers to BigDecimal.
|
64
|
+
|
65
|
+
- `:float` convert all decimal numbers to Float.
|
66
|
+
|
67
|
+
- `:auto` the most precise for the number of digits is used.
|
68
|
+
|
69
|
+
This can also be set with `:decimal_class` when used as a load or
|
70
|
+
parse option to match the JSON gem. In that case either `Float`,
|
71
|
+
`BigDecimal`, or `nil` can be provided.
|
72
|
+
|
73
|
+
### :cache_keys [Boolean]
|
74
|
+
|
75
|
+
If true Hash keys are cached or interned. There are trade-offs with
|
76
|
+
caching keys. Large caches will use more memory and in extreme cases
|
77
|
+
(like over a million) the cache may be slower than not using
|
78
|
+
it. Repeated parsing of similar JSON docs is where cache_keys shines
|
79
|
+
especially with symbol keys.
|
80
|
+
|
81
|
+
There is a maximum length for cached keys. Any key longer than 34
|
82
|
+
bytes is not cached. Everything still works but the key is not cached.
|
83
|
+
|
84
|
+
### :cache_strings [Int]
|
85
|
+
|
86
|
+
Shorter strings can be cached for better performance. A limit,
|
87
|
+
cache_strings, defines the upper limit on what strings are cached. As
|
88
|
+
with cached keys only strings less than 35 bytes are cached even if
|
89
|
+
the limit is set higher. Setting the limit to zero effectively
|
90
|
+
disables the caching of string values.
|
91
|
+
|
92
|
+
Note that caching for strings is for string values and not Hash keys
|
93
|
+
or Object attributes.
|
94
|
+
|
95
|
+
### :circular [Boolean]
|
96
|
+
|
97
|
+
Detect circular references while dumping. In :compat mode raise a
|
98
|
+
NestingError. For other modes except the :object mode place a null in the
|
99
|
+
output. For :object mode place references in the output that will be used to
|
100
|
+
recreate the looped references on load.
|
101
|
+
|
102
|
+
### :class_cache [Boolean]
|
103
|
+
|
104
|
+
Cache classes for faster parsing. This option should not be used if
|
105
|
+
dynamically modifying classes or reloading classes then don't use this.
|
106
|
+
|
107
|
+
### :compat_bigdecimal [Boolean]
|
108
|
+
|
109
|
+
Determines how to load decimals when in `:compat` mode.
|
110
|
+
|
111
|
+
- `true` convert all decimal numbers to BigDecimal.
|
112
|
+
|
113
|
+
- `false` convert all decimal numbers to Float.
|
114
|
+
|
115
|
+
### :create_additions
|
116
|
+
|
117
|
+
A flag indicating that the :create_id key, when encountered during parsing,
|
118
|
+
should create an Object matching the class name specified in the value
|
119
|
+
associated with the key.
|
120
|
+
|
121
|
+
### :create_id [String]
|
122
|
+
|
123
|
+
The :create_id option specifies that key is used for dumping and loading when
|
124
|
+
specifying the class for an encoded object. The default is `json_create`.
|
125
|
+
|
126
|
+
In the `:custom` mode, setting the `:create_id` to nil will cause Complex,
|
127
|
+
Rational, Range, and Regexp to be output as strings instead of as JSON
|
128
|
+
objects.
|
129
|
+
|
130
|
+
### :empty_string [Boolean]
|
131
|
+
|
132
|
+
If true an empty or all whitespace input will not raise an Exception. The
|
133
|
+
default_options will be honored for :null, :strict, and :custom modes. Ignored
|
134
|
+
for :custom and :wab. The :compat has a more complex set of rules. The JSON
|
135
|
+
gem compatibility is best described by examples.
|
136
|
+
|
137
|
+
```
|
138
|
+
JSON.parse('') => raise
|
139
|
+
JSON.parse(' ') => raise
|
140
|
+
JSON.load('') => nil
|
141
|
+
JSON.load('', nil, allow_blank: false) => raise
|
142
|
+
JSON.load('', nil, allow_blank: true) => nil
|
143
|
+
JSON.load(' ') => raise
|
144
|
+
JSON.load(' ', nil, allow_blank: false) => raise
|
145
|
+
JSON.load(' ', nil, allow_blank: true) => raise
|
146
|
+
```
|
147
|
+
|
148
|
+
### :escape_mode [Symbol]
|
149
|
+
|
150
|
+
Determines the characters to escape when dumping. Only the :ascii and
|
151
|
+
:json modes are supported in :compat mode.
|
152
|
+
|
153
|
+
- `:newline` allows unescaped newlines in the output.
|
154
|
+
|
155
|
+
- `:json` follows the JSON specification. This is the default mode.
|
156
|
+
|
157
|
+
- `:xss_safe` escapes HTML and XML characters such as `&` and `<`.
|
158
|
+
|
159
|
+
- `:ascii` escapes all non-ascii or characters with the hi-bit set.
|
160
|
+
|
161
|
+
- `:unicode_xss` escapes a special unicodes and is xss safe.
|
162
|
+
|
163
|
+
### :float_precision [Fixnum]
|
164
|
+
|
165
|
+
The number of digits of precision when dumping floats, 0 indicates use Ruby directly.
|
166
|
+
|
167
|
+
### :hash_class [Class]
|
168
|
+
|
169
|
+
Class to use instead of Hash on load. This is the same as the :object_class.
|
170
|
+
|
171
|
+
### :ignore [Array]
|
172
|
+
|
173
|
+
Ignore all the classes in the Array when dumping. A value of nil indicates
|
174
|
+
ignore nothing.
|
175
|
+
|
176
|
+
### :indent [Fixnum]
|
177
|
+
|
178
|
+
Number of spaces to indent each element in a JSON document, zero is no newline
|
179
|
+
between JSON elements, negative indicates no newline between top level JSON
|
180
|
+
elements in a stream.
|
181
|
+
|
182
|
+
### :indent_str
|
183
|
+
|
184
|
+
Indentation for each element when dumping. The default is an empty
|
185
|
+
string. Primarily intended for json gem compatibility. Using just indent as an
|
186
|
+
integer gives better performance.
|
187
|
+
|
188
|
+
### :integer_range [Range]
|
189
|
+
|
190
|
+
Dump integers outside range as strings.
|
191
|
+
Note: range bounds must be Fixnum.
|
192
|
+
|
193
|
+
### :match_string
|
194
|
+
|
195
|
+
Provides a means to detect strings that should be used to create non-String
|
196
|
+
objects. The value to the option must be a Hash with keys that are regular
|
197
|
+
expressions and values are class names. For strict json gem compatibility a
|
198
|
+
RegExp should be used. For better performance but sacrificing some regexp
|
199
|
+
options a string can be used and the C version of regex will be used instead.
|
200
|
+
|
201
|
+
### :max_nesting
|
202
|
+
|
203
|
+
The maximum nesting depth on both dump and load that is allowed. This exists
|
204
|
+
for json gem compatibility.
|
205
|
+
|
206
|
+
### :mode [Symbol]
|
207
|
+
|
208
|
+
Primary behavior for loading and dumping. The :mode option controls which
|
209
|
+
other options are in effect. For more details see the {file:Modes.md} page. By
|
210
|
+
default Oj uses the :custom mode which is provides the highest degree of
|
211
|
+
customization.
|
212
|
+
|
213
|
+
### :nan [Symbol]
|
214
|
+
|
215
|
+
How to dump Infinity, -Infinity, and NaN in :null, :strict, and :compat
|
216
|
+
mode. Default is :auto but is ignored in the :compat and :rails modes.
|
217
|
+
|
218
|
+
- `:null` places a null
|
219
|
+
|
220
|
+
- `:huge` places a huge number
|
221
|
+
|
222
|
+
- `:word` places Infinity or NaN
|
223
|
+
|
224
|
+
- `:raise` raises and exception
|
225
|
+
|
226
|
+
- `:auto` uses default for each mode which are `:raise` for `:strict`, `:null` for `:null`, and `:word` for `:compat`.
|
227
|
+
|
228
|
+
### :nilnil [Boolean]
|
229
|
+
|
230
|
+
If true a nil input to load will return nil and not raise an Exception.
|
231
|
+
|
232
|
+
### :object_class
|
233
|
+
|
234
|
+
The class to use when creating a Hash on load instead of the Hash class.
|
235
|
+
|
236
|
+
### :object_nl
|
237
|
+
|
238
|
+
Trailer appended to the end of an object dump. The default is an empty
|
239
|
+
string. Primarily intended for json gem compatibility. Using just indent as an
|
240
|
+
integer gives better performance.
|
241
|
+
|
242
|
+
### :omit_nil [Boolean]
|
243
|
+
|
244
|
+
If true, Hash and Object attributes with nil values are omitted.
|
245
|
+
|
246
|
+
### :quirks_mode [Boolean]
|
247
|
+
|
248
|
+
Allow single JSON values instead of documents, default is true (allow). This
|
249
|
+
can also be used in :compat mode to be backward compatible with older versions
|
250
|
+
of the json gem.
|
251
|
+
|
252
|
+
### :safe
|
253
|
+
|
254
|
+
The JSON gem includes the complete JSON in parse errors with no limit
|
255
|
+
on size. To break from the JSON gem behavior for this case set `:safe`
|
256
|
+
to true.
|
257
|
+
|
258
|
+
### :second_precision [Fixnum]
|
259
|
+
|
260
|
+
The number of digits after the decimal when dumping the seconds of time.
|
261
|
+
|
262
|
+
### :space
|
263
|
+
|
264
|
+
String inserted after the ':' character when dumping a JSON object. The
|
265
|
+
default is an empty string. Primarily intended for json gem
|
266
|
+
compatibility. Using just indent as an integer gives better performance.
|
267
|
+
|
268
|
+
### :space_before
|
269
|
+
|
270
|
+
String inserted before the ':' character when dumping a JSON object. The
|
271
|
+
default is an empty string. Primarily intended for json gem
|
272
|
+
compatibility. Using just indent as an integer gives better performance.
|
273
|
+
|
274
|
+
### :symbol_keys [Boolean]
|
275
|
+
|
276
|
+
Use symbols instead of strings for hash keys.
|
277
|
+
|
278
|
+
### :symbolize_names [Boolean]
|
279
|
+
|
280
|
+
Like :symbol_keys has keys are made into symbols but only when
|
281
|
+
mimicking the JSON gem and then only as the JSON gem honors it so
|
282
|
+
JSON.parse honors the option but JSON.load does not.
|
283
|
+
|
284
|
+
### :trace
|
285
|
+
|
286
|
+
When true dump and load functions are traced by printing beginning and ending
|
287
|
+
of blocks and of specific calls.
|
288
|
+
|
289
|
+
### :time_format [Symbol]
|
290
|
+
|
291
|
+
The :time_format when dumping.
|
292
|
+
|
293
|
+
- `:unix` time is output as a decimal number in seconds since epoch including fractions of a second.
|
294
|
+
|
295
|
+
- `:unix_zone` is similar to the `:unix` format but with the timezone encoded in
|
296
|
+
the exponent of the decimal number of seconds since epoch.
|
297
|
+
|
298
|
+
- `:xmlschema` time is output as a string that follows the XML schema definition.
|
299
|
+
|
300
|
+
- `:ruby` time is output as a string formatted using the Ruby `to_s` conversion.
|
301
|
+
|
302
|
+
### :use_as_json [Boolean]
|
303
|
+
|
304
|
+
Call `as_json()` methods on dump, default is false. The option is ignored in
|
305
|
+
the :compat and :rails modes.
|
306
|
+
|
307
|
+
|
308
|
+
### :use_raw_json [Boolean]
|
309
|
+
|
310
|
+
Call `raw_json()` methods on dump, default is false. The option is
|
311
|
+
accepted in the :compat and :rails modes even though it is not
|
312
|
+
supported by other JSON gems. It provides a means to optimize dump or
|
313
|
+
generate performance. The `raw_json(depth, indent)` method should be
|
314
|
+
called only by Oj. It is not intended for any other use. This is meant
|
315
|
+
to replace the abused `to_json` methods. Calling `Oj.dump` inside the
|
316
|
+
`raw_json` with the object itself when `:use_raw_json` is true will
|
317
|
+
result in an infinite loop.
|
318
|
+
|
319
|
+
### :use_to_hash [Boolean]
|
320
|
+
|
321
|
+
Call `to_hash()` methods on dump, default is false. The option is ignored in
|
322
|
+
the :compat and :rails modes.
|
323
|
+
|
324
|
+
### :use_to_json [Boolean]
|
325
|
+
|
326
|
+
Call `to_json()` methods on dump, default is false. The option is ignored in
|
327
|
+
the :compat and :rails modes.
|