oj 2.18.5 → 3.16.11
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 +5 -5
- data/CHANGELOG.md +1452 -0
- data/README.md +53 -221
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +54 -72
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +61 -63
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +38 -67
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +214 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +194 -110
- data/ext/oj/custom.c +1074 -0
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +1276 -2494
- data/ext/oj/dump.h +110 -0
- data/ext/oj/dump_compat.c +897 -0
- data/ext/oj/dump_leaf.c +162 -0
- data/ext/oj/dump_object.c +710 -0
- data/ext/oj/dump_strict.c +399 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +28 -53
- data/ext/oj/err.h +49 -46
- data/ext/oj/extconf.rb +33 -32
- data/ext/oj/fast.c +1082 -1098
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +919 -0
- data/ext/oj/object.c +545 -625
- data/ext/oj/odd.c +158 -168
- data/ext/oj/odd.h +32 -58
- data/ext/oj/oj.c +1727 -2080
- data/ext/oj/oj.h +334 -259
- data/ext/oj/parse.c +974 -753
- data/ext/oj/parse.h +97 -90
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +103 -0
- data/ext/oj/rails.c +1478 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +136 -163
- data/ext/oj/reader.h +76 -112
- data/ext/oj/resolve.c +45 -94
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +144 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +445 -511
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +82 -143
- data/ext/oj/simd.h +10 -0
- data/ext/oj/sparse.c +749 -644
- data/ext/oj/stream_writer.c +329 -0
- data/ext/oj/strict.c +114 -112
- data/ext/oj/string_writer.c +517 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +55 -0
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +75 -72
- data/ext/oj/val_stack.h +94 -127
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +586 -0
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +8 -1
- data/lib/oj/easy_hash.rb +21 -13
- data/lib/oj/error.rb +10 -12
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +165 -26
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +2 -3
- data/lib/oj.rb +3 -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/InstallOptions.md +20 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +337 -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
- metadata +126 -163
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/_test_active.rb +0 -76
- data/test/_test_active_mimic.rb +0 -96
- data/test/_test_mimic_rails.rb +0 -126
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- 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/files.rb +0 -29
- data/test/foo.rb +0 -24
- data/test/helper.rb +0 -27
- data/test/io.rb +0 -48
- data/test/isolated/shared.rb +0 -310
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/mod.rb +0 -16
- data/test/perf.rb +0 -107
- data/test/perf_compat.rb +0 -128
- data/test/perf_fast.rb +0 -164
- data/test/perf_file.rb +0 -64
- data/test/perf_object.rb +0 -138
- data/test/perf_saj.rb +0 -109
- data/test/perf_scp.rb +0 -151
- data/test/perf_simple.rb +0 -287
- data/test/perf_strict.rb +0 -128
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/sample/change.rb +0 -14
- data/test/sample/dir.rb +0 -19
- data/test/sample/doc.rb +0 -36
- data/test/sample/file.rb +0 -48
- data/test/sample/group.rb +0 -16
- data/test/sample/hasprops.rb +0 -16
- data/test/sample/layer.rb +0 -12
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -10
- data/test/sample/shape.rb +0 -35
- data/test/sample/text.rb +0 -20
- data/test/sample.rb +0 -55
- data/test/sample_json.rb +0 -37
- data/test/struct.rb +0 -29
- data/test/test_compat.rb +0 -398
- data/test/test_debian.rb +0 -53
- data/test/test_fast.rb +0 -458
- data/test/test_file.rb +0 -245
- data/test/test_gc.rb +0 -49
- data/test/test_hash.rb +0 -29
- data/test/test_object.rb +0 -745
- data/test/test_saj.rb +0 -186
- data/test/test_scp.rb +0 -396
- data/test/test_serializer.rb +0 -59
- data/test/test_strict.rb +0 -254
- data/test/test_various.rb +0 -1383
- data/test/test_writer.rb +0 -308
- data/test/write_timebars.rb +0 -31
data/lib/oj/state.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
module JSON
|
2
|
+
module Ext
|
3
|
+
module Generator
|
4
|
+
unless defined?(::JSON::Ext::Generator::State)
|
5
|
+
# This class exists for json gem compatibility only. While it can be
|
6
|
+
# used as the options for other than compatibility a simple Hash is
|
7
|
+
# recommended as it is simpler and performs better. The only bit
|
8
|
+
# missing by not using a state object is the depth availability which
|
9
|
+
# may be the depth during dumping or maybe not since it can be set and
|
10
|
+
# the docs for depth= is the same as max_nesting. Note: Had to make
|
11
|
+
# this a subclass of Object instead of Hash like EashyHash due to
|
12
|
+
# conflicts with the json gem.
|
13
|
+
class State
|
14
|
+
|
15
|
+
def self.from_state(opts)
|
16
|
+
s = self.new()
|
17
|
+
s.clear()
|
18
|
+
s.merge(opts)
|
19
|
+
s
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(opts = {})
|
23
|
+
@attrs = {}
|
24
|
+
|
25
|
+
# Populate with all vars then merge in opts. This class deviates from
|
26
|
+
# the json gem in that any of the options can be set with the opts
|
27
|
+
# argument. The json gem limits the opts use to 7 of the options.
|
28
|
+
@attrs[:indent] = ''
|
29
|
+
@attrs[:space] = ''
|
30
|
+
@attrs[:space_before] = ''
|
31
|
+
@attrs[:array_nl] = ''
|
32
|
+
@attrs[:object_nl] = ''
|
33
|
+
@attrs[:allow_nan] = false
|
34
|
+
@attrs[:buffer_initial_length] = 1024 # completely ignored by Oj
|
35
|
+
@attrs[:depth] = 0
|
36
|
+
@attrs[:max_nesting] = 100
|
37
|
+
@attrs[:check_circular?] = true
|
38
|
+
@attrs[:ascii_only] = false
|
39
|
+
|
40
|
+
@attrs.merge!(opts)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_h()
|
44
|
+
return @attrs.dup
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_hash()
|
48
|
+
return @attrs.dup
|
49
|
+
end
|
50
|
+
|
51
|
+
def allow_nan?()
|
52
|
+
@attrs[:allow_nan]
|
53
|
+
end
|
54
|
+
|
55
|
+
def ascii_only?()
|
56
|
+
@attrs[:ascii_only]
|
57
|
+
end
|
58
|
+
|
59
|
+
def configure(opts)
|
60
|
+
raise TypeError.new('expected a Hash') unless opts.respond_to?(:to_h)
|
61
|
+
|
62
|
+
@attrs.merge!(opts.to_h)
|
63
|
+
end
|
64
|
+
|
65
|
+
def generate(obj)
|
66
|
+
JSON.generate(obj)
|
67
|
+
end
|
68
|
+
|
69
|
+
def merge(opts)
|
70
|
+
@attrs.merge!(opts)
|
71
|
+
end
|
72
|
+
|
73
|
+
# special rule for this.
|
74
|
+
def buffer_initial_length=(len)
|
75
|
+
len = 1024 if 0 >= len
|
76
|
+
@attrs[:buffer_initial_length] = len
|
77
|
+
end
|
78
|
+
|
79
|
+
# Replaces the Object.respond_to?() method.
|
80
|
+
# @param [Symbol] m method symbol
|
81
|
+
# @return [Boolean] true for any method that matches an instance
|
82
|
+
# variable reader, otherwise false.
|
83
|
+
def respond_to?(m, include_all = false)
|
84
|
+
return true if super
|
85
|
+
return true if has_key?(key)
|
86
|
+
return true if has_key?(key.to_s)
|
87
|
+
|
88
|
+
has_key?(key.to_sym)
|
89
|
+
end
|
90
|
+
|
91
|
+
def [](key)
|
92
|
+
key = key.to_sym
|
93
|
+
@attrs.fetch(key, nil)
|
94
|
+
end
|
95
|
+
|
96
|
+
def []=(key, value)
|
97
|
+
key = key.to_sym
|
98
|
+
@attrs[key] = value
|
99
|
+
end
|
100
|
+
|
101
|
+
def clear()
|
102
|
+
@attrs.clear()
|
103
|
+
end
|
104
|
+
|
105
|
+
def has_key?(k)
|
106
|
+
@attrs.has_key?(key.to_sym)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Handles requests for Hash values. Others cause an Exception to be raised.
|
110
|
+
# @param [Symbol|String] m method symbol
|
111
|
+
# @return [Boolean] the value of the specified instance variable.
|
112
|
+
# @raise [ArgumentError] if an argument is given. Zero arguments expected.
|
113
|
+
# @raise [NoMethodError] if the instance variable is not defined.
|
114
|
+
def method_missing(m, *args, &block)
|
115
|
+
if m.to_s.end_with?('=')
|
116
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1 with #{m}) to method #{m}") if args.nil? or 1 != args.length
|
117
|
+
|
118
|
+
m = m.to_s[0..-2]
|
119
|
+
m = m.to_sym
|
120
|
+
return @attrs.store(m, args[0])
|
121
|
+
end
|
122
|
+
if @attrs.has_key?(m.to_sym)
|
123
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 0 with #{m}) to method #{m}") unless args.nil? or args.empty?
|
124
|
+
|
125
|
+
return @attrs[m.to_sym]
|
126
|
+
end
|
127
|
+
return @attrs.send(m, *args, &block)
|
128
|
+
end
|
129
|
+
|
130
|
+
end # State
|
131
|
+
end # defined check
|
132
|
+
end # Generator
|
133
|
+
end # Ext
|
134
|
+
|
135
|
+
end # JSON
|
data/lib/oj/version.rb
CHANGED
data/lib/oj.rb
CHANGED
@@ -1,35 +1,7 @@
|
|
1
|
-
#
|
2
|
-
# optimized JSON handling.
|
3
|
-
#
|
4
|
-
# Oj has several dump or serialization modes which control how Objects are
|
5
|
-
# converted to JSON. These modes are set with the :mode option in either the
|
6
|
-
# default options or as one of the options to the dump() method.
|
7
|
-
#
|
8
|
-
# - :strict mode will only allow the 7 basic JSON types to be serialized. Any other Object
|
9
|
-
# will raise and Exception.
|
10
|
-
#
|
11
|
-
# - :null mode replaces any Object that is not one of the JSON types is replaced by a JSON null.
|
12
|
-
#
|
13
|
-
# - :object mode will dump any Object as a JSON Object with keys that match
|
14
|
-
# the Ruby Object's variable names without the '@' character. This is the
|
15
|
-
# highest performance mode.
|
16
|
-
#
|
17
|
-
# - :compat mode is is the compatible with other systems. It will serialize
|
18
|
-
# any Object but will check to see if the Object implements a to_hash() or
|
19
|
-
# to_json() method. If either exists that method is used for serializing the
|
20
|
-
# Object. The to_hash() is more flexible and produces more consistent output
|
21
|
-
# so it has a preference over the to_json() method. If neither the to_json()
|
22
|
-
# or to_hash() methods exist then the Oj internal Object variable encoding
|
23
|
-
# is used.
|
24
|
-
module Oj
|
25
|
-
end
|
1
|
+
# frozen_string_literal: true
|
26
2
|
|
27
|
-
|
28
|
-
|
29
|
-
# the C extension.
|
30
|
-
require 'bigdecimal'
|
31
|
-
rescue Exception
|
32
|
-
# ignore
|
3
|
+
# Oj module is defined in oj.c.
|
4
|
+
module Oj
|
33
5
|
end
|
34
6
|
|
35
7
|
require 'oj/version'
|
data/pages/Advanced.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Advanced Features
|
2
|
+
|
3
|
+
Optimized JSON (Oj), as the name implies, was written to provide speed optimized
|
4
|
+
JSON handling. It was designed as a faster alternative to Yajl and other
|
5
|
+
common Ruby JSON parsers. So far it has achieved that, and is about 2 times faster
|
6
|
+
than any other Ruby JSON parser, and 3 or more times faster at serializing JSON.
|
7
|
+
|
8
|
+
Oj has several `dump` or serialization modes which control how Ruby `Object`s are
|
9
|
+
converted to JSON. These modes are set with the `:mode` option in either the
|
10
|
+
default options or as one of the options to the `dump` method. In addition to
|
11
|
+
the various options there are also alternative APIs for parsing JSON.
|
12
|
+
|
13
|
+
The fastest alternative parser API is the `Oj::Doc` API. The `Oj::Doc` API takes
|
14
|
+
a completely different approach by opening a JSON document and providing calls
|
15
|
+
to navigate around the JSON while it is open. With this approach, JSON access
|
16
|
+
can be well over 20 times faster than conventional JSON parsing.
|
17
|
+
|
18
|
+
The `Oj::Saj` and `Oj::ScHandler` APIs are callback parsers that
|
19
|
+
walk the JSON document depth first and makes callbacks for each element.
|
20
|
+
Both callback parser are useful when only portions of the JSON are of
|
21
|
+
interest. Performance up to 20 times faster than conventional JSON is
|
22
|
+
possible if only a few elements of the JSON are of interest.
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Compatibility
|
2
|
+
|
3
|
+
**Ruby**
|
4
|
+
|
5
|
+
Oj is compatible with Ruby 2.4+ and RBX.
|
6
|
+
Support for JRuby has been removed as JRuby no longer supports C extensions and
|
7
|
+
there are bugs in the older versions that are not being fixed.
|
8
|
+
|
9
|
+
**Rails**
|
10
|
+
|
11
|
+
Although up until 4.1 Rails uses [multi_json](https://github.com/intridea/multi_json), an [issue in Rails](https://github.com/rails/rails/issues/9212) causes ActiveSupport to fail to make use Oj for JSON handling.
|
12
|
+
There is a
|
13
|
+
[gem to patch this](https://github.com/GoodLife/rails-patch-json-encode) for
|
14
|
+
Rails 3.2 and 4.0. As of the Oj 2.6.0 release the default behavior is to not use
|
15
|
+
the `to_json()` method unless the `:use_to_json` option is set. This provides
|
16
|
+
another work around to the rails older and newer behavior.
|
17
|
+
|
18
|
+
The latest ActiveRecord is able to work with Oj by simply using the line:
|
19
|
+
|
20
|
+
```
|
21
|
+
serialize :metadata, Oj
|
22
|
+
```
|
23
|
+
|
24
|
+
In version Rails 4.1, multi_json has been removed, and this patch is unnecessary and will no longer work.
|
25
|
+
See {file:Rails.md}.
|
data/pages/Custom.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Custom mode
|
2
|
+
|
3
|
+
The `:custom` mode is the most configurable mode and honors almost all
|
4
|
+
options. It provides the most flexibility although it can not be configured to
|
5
|
+
be exactly like any of the other modes. Each mode has some special aspect that
|
6
|
+
makes it unique. For example, the `:object` mode has it's own unique format
|
7
|
+
for object dumping and loading. The `:compat` mode mimic the json gem
|
8
|
+
including methods called for encoding and inconsistencies between
|
9
|
+
`JSON.dump()`, `JSON.generate()`, and `JSON()`.
|
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.
|
14
|
+
|
15
|
+
The ability to create objects from JSON object elements is supported and
|
16
|
+
considers the `:create_additions` option. Special treatment is given to the
|
17
|
+
`:create_id` though. If the `:create_id` is set to `"^o"` then the Oj internal
|
18
|
+
encoding and decoding is used. These are more efficient than calling out to a
|
19
|
+
`to_json` method or `create_json` method on the classes. Those method do not
|
20
|
+
have to exist for the `"^o"` behavior to be utilized. Any other `:create_id`
|
21
|
+
value behaves similar to the json gem by calling `to_json` and `create_json`
|
22
|
+
as appropriate.
|
23
|
+
|
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 escaping 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]`.
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Oj Install Options
|
2
|
+
|
3
|
+
### Enable trace log
|
4
|
+
|
5
|
+
```
|
6
|
+
$ gem install oj -- --enable-trace-log
|
7
|
+
```
|
8
|
+
|
9
|
+
To enable Oj trace feature, it uses `--enable-trace-log` option when installing the gem.
|
10
|
+
Then, the trace logs will be displayed when `:trace` option is set to `true`.
|
11
|
+
|
12
|
+
|
13
|
+
### Enable SIMD instructions
|
14
|
+
|
15
|
+
```
|
16
|
+
$ gem install oj -- --with-sse42
|
17
|
+
```
|
18
|
+
|
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.
|
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.
|