nano-safe-gem 0.0.1
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 +7 -0
- data/nano-safe-gem.gemspec +12 -0
- data/oj-3.17.3/CHANGELOG.md +1506 -0
- data/oj-3.17.3/LICENSE +21 -0
- data/oj-3.17.3/README.md +103 -0
- data/oj-3.17.3/RELEASE_NOTES.md +61 -0
- data/oj-3.17.3/ext/oj/buf.h +85 -0
- data/oj-3.17.3/ext/oj/cache.c +329 -0
- data/oj-3.17.3/ext/oj/cache.h +22 -0
- data/oj-3.17.3/ext/oj/cache8.c +105 -0
- data/oj-3.17.3/ext/oj/cache8.h +21 -0
- data/oj-3.17.3/ext/oj/circarray.c +64 -0
- data/oj-3.17.3/ext/oj/circarray.h +22 -0
- data/oj-3.17.3/ext/oj/code.c +214 -0
- data/oj-3.17.3/ext/oj/code.h +40 -0
- data/oj-3.17.3/ext/oj/compat.c +239 -0
- data/oj-3.17.3/ext/oj/custom.c +1079 -0
- data/oj-3.17.3/ext/oj/debug.c +126 -0
- data/oj-3.17.3/ext/oj/dump.c +1765 -0
- data/oj-3.17.3/ext/oj/dump.h +112 -0
- data/oj-3.17.3/ext/oj/dump_compat.c +902 -0
- data/oj-3.17.3/ext/oj/dump_leaf.c +162 -0
- data/oj-3.17.3/ext/oj/dump_object.c +715 -0
- data/oj-3.17.3/ext/oj/dump_strict.c +404 -0
- data/oj-3.17.3/ext/oj/encode.h +16 -0
- data/oj-3.17.3/ext/oj/encoder.c +43 -0
- data/oj-3.17.3/ext/oj/err.c +57 -0
- data/oj-3.17.3/ext/oj/err.h +67 -0
- data/oj-3.17.3/ext/oj/extconf.rb +72 -0
- data/oj-3.17.3/ext/oj/fast.c +1730 -0
- data/oj-3.17.3/ext/oj/intern.c +313 -0
- data/oj-3.17.3/ext/oj/intern.h +22 -0
- data/oj-3.17.3/ext/oj/mem.c +318 -0
- data/oj-3.17.3/ext/oj/mem.h +53 -0
- data/oj-3.17.3/ext/oj/mimic_json.c +922 -0
- data/oj-3.17.3/ext/oj/object.c +726 -0
- data/oj-3.17.3/ext/oj/odd.c +245 -0
- data/oj-3.17.3/ext/oj/odd.h +43 -0
- data/oj-3.17.3/ext/oj/oj.c +2332 -0
- data/oj-3.17.3/ext/oj/oj.h +405 -0
- data/oj-3.17.3/ext/oj/parse.c +1367 -0
- data/oj-3.17.3/ext/oj/parse.h +113 -0
- data/oj-3.17.3/ext/oj/parser.c +1676 -0
- data/oj-3.17.3/ext/oj/parser.h +103 -0
- data/oj-3.17.3/ext/oj/rails.c +1480 -0
- data/oj-3.17.3/ext/oj/rails.h +18 -0
- data/oj-3.17.3/ext/oj/reader.c +220 -0
- data/oj-3.17.3/ext/oj/reader.h +137 -0
- data/oj-3.17.3/ext/oj/resolve.c +80 -0
- data/oj-3.17.3/ext/oj/resolve.h +12 -0
- data/oj-3.17.3/ext/oj/rxclass.c +144 -0
- data/oj-3.17.3/ext/oj/rxclass.h +26 -0
- data/oj-3.17.3/ext/oj/safe.c +230 -0
- data/oj-3.17.3/ext/oj/safe.h +79 -0
- data/oj-3.17.3/ext/oj/saj.c +677 -0
- data/oj-3.17.3/ext/oj/saj2.c +584 -0
- data/oj-3.17.3/ext/oj/saj2.h +23 -0
- data/oj-3.17.3/ext/oj/scp.c +185 -0
- data/oj-3.17.3/ext/oj/simd.h +219 -0
- data/oj-3.17.3/ext/oj/sparse.c +943 -0
- data/oj-3.17.3/ext/oj/stream_writer.c +329 -0
- data/oj-3.17.3/ext/oj/strict.c +187 -0
- data/oj-3.17.3/ext/oj/string_writer.c +517 -0
- data/oj-3.17.3/ext/oj/trace.c +72 -0
- data/oj-3.17.3/ext/oj/trace.h +55 -0
- data/oj-3.17.3/ext/oj/usual.c +1231 -0
- data/oj-3.17.3/ext/oj/usual.h +69 -0
- data/oj-3.17.3/ext/oj/util.c +136 -0
- data/oj-3.17.3/ext/oj/util.h +20 -0
- data/oj-3.17.3/ext/oj/val_stack.c +101 -0
- data/oj-3.17.3/ext/oj/val_stack.h +151 -0
- data/oj-3.17.3/ext/oj/validate.c +46 -0
- data/oj-3.17.3/ext/oj/wab.c +586 -0
- data/oj-3.17.3/lib/oj/active_support_helper.rb +39 -0
- data/oj-3.17.3/lib/oj/bag.rb +95 -0
- data/oj-3.17.3/lib/oj/easy_hash.rb +52 -0
- data/oj-3.17.3/lib/oj/error.rb +21 -0
- data/oj-3.17.3/lib/oj/json.rb +188 -0
- data/oj-3.17.3/lib/oj/mimic.rb +301 -0
- data/oj-3.17.3/lib/oj/saj.rb +80 -0
- data/oj-3.17.3/lib/oj/schandler.rb +143 -0
- data/oj-3.17.3/lib/oj/state.rb +135 -0
- data/oj-3.17.3/lib/oj/version.rb +4 -0
- data/oj-3.17.3/lib/oj.rb +15 -0
- data/oj-3.17.3/pages/Advanced.md +22 -0
- data/oj-3.17.3/pages/Compatibility.md +25 -0
- data/oj-3.17.3/pages/Custom.md +23 -0
- data/oj-3.17.3/pages/Encoding.md +65 -0
- data/oj-3.17.3/pages/InstallOptions.md +20 -0
- data/oj-3.17.3/pages/JsonGem.md +94 -0
- data/oj-3.17.3/pages/Modes.md +161 -0
- data/oj-3.17.3/pages/Options.md +337 -0
- data/oj-3.17.3/pages/Parser.md +309 -0
- data/oj-3.17.3/pages/Rails.md +167 -0
- data/oj-3.17.3/pages/Security.md +20 -0
- data/oj-3.17.3/pages/WAB.md +13 -0
- metadata +136 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
module Oj
|
|
2
|
+
# A SAX style parse handler for JSON hence the acronym SAJ for Simple API
|
|
3
|
+
# for JSON. The Oj::Saj handler class can be subclassed and then used with
|
|
4
|
+
# the Oj::Saj key_parse() method or with the more resent
|
|
5
|
+
# Oj::Parser.new(:saj). The Saj methods will then be called as the file is
|
|
6
|
+
# parsed.
|
|
7
|
+
#
|
|
8
|
+
# With Oj::Parser.new(:saj) each method can also include a line and column
|
|
9
|
+
# argument so hash_start(key) could also be hash_start(key, line,
|
|
10
|
+
# column). The error() method is no used with Oj::Parser.new(:saj) so it
|
|
11
|
+
# will never be called.
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
#
|
|
15
|
+
# require 'oj'
|
|
16
|
+
#
|
|
17
|
+
# class MySaj < ::Oj::Saj
|
|
18
|
+
# def initialize()
|
|
19
|
+
# @hash_cnt = 0
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# def hash_start(key)
|
|
23
|
+
# @hash_cnt += 1
|
|
24
|
+
# end
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# cnt = MySaj.new()
|
|
28
|
+
# File.open('any.json', 'r') do |f|
|
|
29
|
+
# Oj.saj_parse(cnt, f)
|
|
30
|
+
# end
|
|
31
|
+
#
|
|
32
|
+
# or
|
|
33
|
+
#
|
|
34
|
+
# p = Oj::Parser.new(:saj)
|
|
35
|
+
# p.handler = MySaj.new()
|
|
36
|
+
# File.open('any.json', 'r') do |f|
|
|
37
|
+
# p.parse(f.read)
|
|
38
|
+
# end
|
|
39
|
+
#
|
|
40
|
+
# To make the desired methods active while parsing the desired method should
|
|
41
|
+
# be made public in the subclasses. If the methods remain private they will
|
|
42
|
+
# not be called during parsing.
|
|
43
|
+
#
|
|
44
|
+
# def hash_start(key); end
|
|
45
|
+
# def hash_end(key); end
|
|
46
|
+
# def array_start(key); end
|
|
47
|
+
# def array_end(key); end
|
|
48
|
+
# def add_value(value, key); end
|
|
49
|
+
# def error(message, line, column); end
|
|
50
|
+
#
|
|
51
|
+
class Saj
|
|
52
|
+
# Create a new instance of the Saj handler class.
|
|
53
|
+
def initialize()
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# To make the desired methods active while parsing the desired method should
|
|
57
|
+
# be made public in the subclasses. If the methods remain private they will
|
|
58
|
+
# not be called during parsing.
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def hash_start(key)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def hash_end(key)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def array_start(key)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def array_end(key)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def add_value(value, key)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def error(message, line, column)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
end # Saj
|
|
80
|
+
end # Oj
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
module Oj
|
|
2
|
+
# A Simple Callback Parser (SCP) for JSON. The Oj::ScHandler class should be
|
|
3
|
+
# subclassed and then used with the Oj.sc_parse() method. The Scp methods will
|
|
4
|
+
# then be called as the file is parsed. The handler does not have to be a
|
|
5
|
+
# subclass of the ScHandler class as long as it responds to the desired
|
|
6
|
+
# methods.
|
|
7
|
+
#
|
|
8
|
+
# @example
|
|
9
|
+
#
|
|
10
|
+
# require 'oj'
|
|
11
|
+
#
|
|
12
|
+
# class MyHandler < ::Oj::ScHandler
|
|
13
|
+
# def hash_start
|
|
14
|
+
# {}
|
|
15
|
+
# end
|
|
16
|
+
#
|
|
17
|
+
# def hash_set(h,k,v)
|
|
18
|
+
# h[k] = v
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# def array_start
|
|
22
|
+
# []
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# def array_append(a,v)
|
|
26
|
+
# a << v
|
|
27
|
+
# end
|
|
28
|
+
#
|
|
29
|
+
# def add_value(v)
|
|
30
|
+
# p v
|
|
31
|
+
# end
|
|
32
|
+
#
|
|
33
|
+
# def error(message, line, column)
|
|
34
|
+
# p "ERROR: #{message}"
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
# File.open('any.json', 'r') do |f|
|
|
39
|
+
# Oj.sc_parse(MyHandler.new, f)
|
|
40
|
+
# end
|
|
41
|
+
#
|
|
42
|
+
# To make the desired methods active while parsing the desired method should
|
|
43
|
+
# be made public in the subclasses. If the methods remain private they will
|
|
44
|
+
# not be called during parsing.
|
|
45
|
+
#
|
|
46
|
+
# def hash_start(); end
|
|
47
|
+
# def hash_end(); end
|
|
48
|
+
# def hash_key(key); end
|
|
49
|
+
# def hash_set(h, key, value); end
|
|
50
|
+
# def array_start(); end
|
|
51
|
+
# def array_end(); end
|
|
52
|
+
# def array_append(a, value); end
|
|
53
|
+
# def add_value(value); end
|
|
54
|
+
#
|
|
55
|
+
# As certain elements of a JSON document are reached during parsing the
|
|
56
|
+
# callbacks are called. The parser helps by keeping track of objects created
|
|
57
|
+
# by the callbacks but does not create those objects itself.
|
|
58
|
+
#
|
|
59
|
+
# hash_start
|
|
60
|
+
#
|
|
61
|
+
# When a JSON object element starts the hash_start() callback is called if
|
|
62
|
+
# public. It should return what ever Ruby Object is to be used as the element
|
|
63
|
+
# that will later be included in the hash_set() callback.
|
|
64
|
+
#
|
|
65
|
+
# hash_end
|
|
66
|
+
#
|
|
67
|
+
# At the end of a JSON object element the hash_end() callback is called if
|
|
68
|
+
# public.
|
|
69
|
+
#
|
|
70
|
+
# hash_key
|
|
71
|
+
#
|
|
72
|
+
# When a hash key is encountered the hash_key() method is called with the
|
|
73
|
+
# parsed hash value key. The return value from the call is then used as the
|
|
74
|
+
# key in the key-value pair that follows.
|
|
75
|
+
#
|
|
76
|
+
# hash_set
|
|
77
|
+
#
|
|
78
|
+
# When a key value pair is encountered during parsing the hash_set() callback
|
|
79
|
+
# is called if public. The first element will be the object returned from the
|
|
80
|
+
# enclosing hash_start() callback. The second argument is the key and the last
|
|
81
|
+
# is the value.
|
|
82
|
+
#
|
|
83
|
+
# array_start
|
|
84
|
+
#
|
|
85
|
+
# When a JSON array element is started the array_start() callback is called if
|
|
86
|
+
# public. It should return what ever Ruby Object is to be used as the element
|
|
87
|
+
# that will later be included in the array_append() callback.
|
|
88
|
+
#
|
|
89
|
+
# array_end
|
|
90
|
+
#
|
|
91
|
+
# At the end of a JSON array element the array_end() callback is called if public.
|
|
92
|
+
#
|
|
93
|
+
# array_append
|
|
94
|
+
#
|
|
95
|
+
# When a element is encountered that is an element of an array the
|
|
96
|
+
# array_append() callback is called if public. The first argument to the
|
|
97
|
+
# callback is the Ruby object returned from the enclosing array_start()
|
|
98
|
+
# callback.
|
|
99
|
+
#
|
|
100
|
+
# add_value
|
|
101
|
+
#
|
|
102
|
+
# The handler is expected to handle multiple JSON elements in one stream,
|
|
103
|
+
# file, or string. When a top level JSON has been read completely the
|
|
104
|
+
# add_value() callback is called. Even if only one element was ready this
|
|
105
|
+
# callback returns the Ruby object that was constructed during the parsing.
|
|
106
|
+
#
|
|
107
|
+
class ScHandler
|
|
108
|
+
# Create a new instance of the ScHandler class.
|
|
109
|
+
def initialize()
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# To make the desired methods active while parsing the desired method should
|
|
113
|
+
# be made public in the subclasses. If the methods remain private they will
|
|
114
|
+
# not be called during parsing.
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def hash_start()
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def hash_end()
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def hash_key(key)
|
|
124
|
+
key
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def hash_set(h, key, value)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def array_start()
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def array_end()
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def add_value(value)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def array_append(a, value)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
end # ScHandler
|
|
143
|
+
end # Oj
|
|
@@ -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/oj-3.17.3/lib/oj.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Oj module is defined in oj.c.
|
|
4
|
+
module Oj
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
require 'oj/version'
|
|
8
|
+
require 'oj/bag'
|
|
9
|
+
require 'oj/easy_hash'
|
|
10
|
+
require 'oj/error'
|
|
11
|
+
require 'oj/mimic'
|
|
12
|
+
require 'oj/saj'
|
|
13
|
+
require 'oj/schandler'
|
|
14
|
+
|
|
15
|
+
require 'oj/oj' # C extension
|
|
@@ -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}.
|
|
@@ -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
|
+
|
|
@@ -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.
|
|
@@ -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.
|