oj_windows 3.16.15
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/CHANGELOG.md +44 -0
- data/LICENSE +21 -0
- data/README.md +164 -0
- data/ext/oj_windows/buf.h +85 -0
- data/ext/oj_windows/cache.c +339 -0
- data/ext/oj_windows/cache.h +22 -0
- data/ext/oj_windows/cache8.c +105 -0
- data/ext/oj_windows/cache8.h +21 -0
- data/ext/oj_windows/circarray.c +64 -0
- data/ext/oj_windows/circarray.h +22 -0
- data/ext/oj_windows/code.c +214 -0
- data/ext/oj_windows/code.h +40 -0
- data/ext/oj_windows/compat.c +239 -0
- data/ext/oj_windows/custom.c +1074 -0
- data/ext/oj_windows/debug.c +126 -0
- data/ext/oj_windows/dump.c +1556 -0
- data/ext/oj_windows/dump.h +110 -0
- data/ext/oj_windows/dump_compat.c +901 -0
- data/ext/oj_windows/dump_leaf.c +162 -0
- data/ext/oj_windows/dump_object.c +710 -0
- data/ext/oj_windows/dump_strict.c +405 -0
- data/ext/oj_windows/encode.h +16 -0
- data/ext/oj_windows/err.c +57 -0
- data/ext/oj_windows/err.h +67 -0
- data/ext/oj_windows/extconf.rb +77 -0
- data/ext/oj_windows/fast.c +1710 -0
- data/ext/oj_windows/intern.c +325 -0
- data/ext/oj_windows/intern.h +22 -0
- data/ext/oj_windows/mem.c +320 -0
- data/ext/oj_windows/mem.h +53 -0
- data/ext/oj_windows/mimic_json.c +919 -0
- data/ext/oj_windows/object.c +726 -0
- data/ext/oj_windows/odd.c +245 -0
- data/ext/oj_windows/odd.h +43 -0
- data/ext/oj_windows/oj.c +2097 -0
- data/ext/oj_windows/oj.h +420 -0
- data/ext/oj_windows/parse.c +1317 -0
- data/ext/oj_windows/parse.h +113 -0
- data/ext/oj_windows/parser.c +1600 -0
- data/ext/oj_windows/parser.h +103 -0
- data/ext/oj_windows/rails.c +1484 -0
- data/ext/oj_windows/rails.h +18 -0
- data/ext/oj_windows/reader.c +222 -0
- data/ext/oj_windows/reader.h +137 -0
- data/ext/oj_windows/resolve.c +80 -0
- data/ext/oj_windows/resolve.h +12 -0
- data/ext/oj_windows/rxclass.c +144 -0
- data/ext/oj_windows/rxclass.h +26 -0
- data/ext/oj_windows/saj.c +675 -0
- data/ext/oj_windows/saj2.c +584 -0
- data/ext/oj_windows/saj2.h +23 -0
- data/ext/oj_windows/scp.c +187 -0
- data/ext/oj_windows/simd.h +47 -0
- data/ext/oj_windows/sparse.c +946 -0
- data/ext/oj_windows/stream_writer.c +329 -0
- data/ext/oj_windows/strict.c +189 -0
- data/ext/oj_windows/string_writer.c +517 -0
- data/ext/oj_windows/trace.c +72 -0
- data/ext/oj_windows/trace.h +55 -0
- data/ext/oj_windows/usual.c +1218 -0
- data/ext/oj_windows/usual.h +69 -0
- data/ext/oj_windows/util.c +136 -0
- data/ext/oj_windows/util.h +20 -0
- data/ext/oj_windows/val_stack.c +101 -0
- data/ext/oj_windows/val_stack.h +151 -0
- data/ext/oj_windows/validate.c +46 -0
- data/ext/oj_windows/wab.c +584 -0
- data/lib/oj/active_support_helper.rb +39 -0
- data/lib/oj/bag.rb +95 -0
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/error.rb +21 -0
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +301 -0
- data/lib/oj/saj.rb +80 -0
- data/lib/oj/schandler.rb +143 -0
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +4 -0
- data/lib/oj_windows/active_support_helper.rb +39 -0
- data/lib/oj_windows/bag.rb +95 -0
- data/lib/oj_windows/easy_hash.rb +52 -0
- data/lib/oj_windows/error.rb +21 -0
- data/lib/oj_windows/json.rb +188 -0
- data/lib/oj_windows/mimic.rb +301 -0
- data/lib/oj_windows/saj.rb +80 -0
- data/lib/oj_windows/schandler.rb +143 -0
- data/lib/oj_windows/state.rb +135 -0
- data/lib/oj_windows/version.rb +4 -0
- data/lib/oj_windows.rb +15 -0
- data/pages/Advanced.md +38 -0
- data/pages/Compatibility.md +49 -0
- data/pages/Custom.md +37 -0
- data/pages/Encoding.md +61 -0
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +60 -0
- data/pages/Modes.md +94 -0
- data/pages/Options.md +339 -0
- data/pages/Parser.md +134 -0
- data/pages/Rails.md +85 -0
- data/pages/Security.md +43 -0
- data/pages/WAB.md +12 -0
- metadata +242 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
# frozen_string_literal: false
|
|
2
|
+
|
|
3
|
+
require 'bigdecimal'
|
|
4
|
+
require 'ostruct'
|
|
5
|
+
|
|
6
|
+
module Oj
|
|
7
|
+
|
|
8
|
+
##
|
|
9
|
+
# Custom mode can be used to emulate the compat mode with some minor
|
|
10
|
+
# differences. These are the options that setup the custom mode to be like
|
|
11
|
+
# the compat mode.
|
|
12
|
+
CUSTOM_MIMIC_JSON_OPTIONS = {
|
|
13
|
+
allow_gc: true,
|
|
14
|
+
allow_invalid_unicode: false,
|
|
15
|
+
allow_nan: false,
|
|
16
|
+
array_class: nil,
|
|
17
|
+
array_nl: nil,
|
|
18
|
+
auto_define: false,
|
|
19
|
+
bigdecimal_as_decimal: false,
|
|
20
|
+
bigdecimal_load: :auto,
|
|
21
|
+
circular: false,
|
|
22
|
+
class_cache: false,
|
|
23
|
+
cache_keys: true,
|
|
24
|
+
cache_str: 5,
|
|
25
|
+
create_additions: false,
|
|
26
|
+
create_id: "json_class",
|
|
27
|
+
empty_string: false,
|
|
28
|
+
escape_mode: :unicode_xss,
|
|
29
|
+
float_precision: 0,
|
|
30
|
+
hash_class: nil,
|
|
31
|
+
ignore: nil,
|
|
32
|
+
ignore_under: false,
|
|
33
|
+
indent: 0,
|
|
34
|
+
integer_range: nil,
|
|
35
|
+
mode: :custom,
|
|
36
|
+
nan: :raise,
|
|
37
|
+
nilnil: false,
|
|
38
|
+
object_nl: nil,
|
|
39
|
+
omit_nil: false,
|
|
40
|
+
quirks_mode: true,
|
|
41
|
+
safe: false,
|
|
42
|
+
second_precision: 3,
|
|
43
|
+
space: nil,
|
|
44
|
+
space_before: nil,
|
|
45
|
+
symbol_keys: false,
|
|
46
|
+
time_format: :ruby,
|
|
47
|
+
trace: false,
|
|
48
|
+
use_as_json: false,
|
|
49
|
+
use_raw_json: false,
|
|
50
|
+
use_to_hash: false,
|
|
51
|
+
use_to_json: true,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
# A bit hack-ish but does the trick. The JSON.dump_default_options is a Hash
|
|
55
|
+
# but in mimic we use a C struct to store defaults. This class creates a view
|
|
56
|
+
# onto that struct.
|
|
57
|
+
class MimicDumpOption < Hash
|
|
58
|
+
def initialize()
|
|
59
|
+
oo = Oj.default_options
|
|
60
|
+
self.store(:max_nesting, false)
|
|
61
|
+
self.store(:allow_nan, true)
|
|
62
|
+
self.store(:quirks_mode, oo[:quirks_mode])
|
|
63
|
+
self.store(:ascii_only, (:ascii == oo[:escape_mode]))
|
|
64
|
+
|
|
65
|
+
super
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def []=(key, value)
|
|
69
|
+
case key
|
|
70
|
+
when :quirks_mode
|
|
71
|
+
Oj.default_options = {:quirks_mode => value}
|
|
72
|
+
when :ascii_only
|
|
73
|
+
Oj.default_options = {:ascii_only => value}
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Loads mimic-ed JSON paths. Used by Oj.mimic_JSON().
|
|
79
|
+
# @param mimic_paths [Array] additional paths to add to the Ruby loaded features.
|
|
80
|
+
def self.mimic_loaded(mimic_paths=[])
|
|
81
|
+
$LOAD_PATH.each do |d|
|
|
82
|
+
next unless File.exist?(d)
|
|
83
|
+
|
|
84
|
+
jfile = File.join(d, 'json.rb')
|
|
85
|
+
$LOADED_FEATURES << jfile unless $LOADED_FEATURES.include?(jfile) if File.exist?(jfile)
|
|
86
|
+
|
|
87
|
+
Dir.glob(File.join(d, 'json', '**', '*.rb')).each do |file|
|
|
88
|
+
# allow json/add/xxx to be loaded. User can override with Oj.add_to_json(xxx).
|
|
89
|
+
$LOADED_FEATURES << file unless $LOADED_FEATURES.include?(file) unless file.include?('add')
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
mimic_paths.each { |p| $LOADED_FEATURES << p }
|
|
93
|
+
$LOADED_FEATURES << 'json' unless $LOADED_FEATURES.include?('json')
|
|
94
|
+
|
|
95
|
+
require 'oj_windows/json'
|
|
96
|
+
|
|
97
|
+
if Object.const_defined?('OpenStruct')
|
|
98
|
+
OpenStruct.class_eval do
|
|
99
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
100
|
+
unless defined?(self.as_json)
|
|
101
|
+
def as_json(*)
|
|
102
|
+
name = self.class.name.to_s
|
|
103
|
+
raise JSON::JSONError, "Only named structs are supported!" if 0 == name.length
|
|
104
|
+
|
|
105
|
+
{ JSON.create_id => name, 't' => table }
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
def self.json_create(h)
|
|
109
|
+
new(h['t'] || h[:t])
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
BigDecimal.class_eval do
|
|
115
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
116
|
+
unless defined?(self.as_json)
|
|
117
|
+
def as_json(*)
|
|
118
|
+
{JSON.create_id => 'BigDecimal', 'b' => _dump }
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
def self.json_create(h)
|
|
122
|
+
BigDecimal._load(h['b'])
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
Complex.class_eval do
|
|
127
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
128
|
+
unless defined?(self.as_json)
|
|
129
|
+
def as_json(*)
|
|
130
|
+
{JSON.create_id => 'Complex', 'r' => real, 'i' => imag }
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
def self.json_create(h)
|
|
134
|
+
Complex(h['r'], h['i'])
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
DateTime.class_eval do
|
|
139
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
140
|
+
unless defined?(self.as_json)
|
|
141
|
+
def as_json(*)
|
|
142
|
+
{ JSON.create_id => 'DateTime',
|
|
143
|
+
'y' => year,
|
|
144
|
+
'm' => month,
|
|
145
|
+
'd' => day,
|
|
146
|
+
'H' => hour,
|
|
147
|
+
'M' => min,
|
|
148
|
+
'S' => sec,
|
|
149
|
+
'of' => offset.to_s,
|
|
150
|
+
'sg' => start }
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
def self.json_create(h)
|
|
154
|
+
# offset is a rational as a string
|
|
155
|
+
as, bs = h['of'].split('/')
|
|
156
|
+
a = as.to_i
|
|
157
|
+
b = bs.to_i
|
|
158
|
+
if 0 == b
|
|
159
|
+
off = a
|
|
160
|
+
else
|
|
161
|
+
off = Rational(a, b)
|
|
162
|
+
end
|
|
163
|
+
civil(h['y'], h['m'], h['d'], h['H'], h['M'], h['S'], off, h['sg'])
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
Date.class_eval do
|
|
168
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
169
|
+
unless defined?(self.as_json)
|
|
170
|
+
def as_json(*)
|
|
171
|
+
{ JSON.create_id => 'Date', 'y' => year, 'm' => month, 'd' => day, 'sg' => start }
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
def self.json_create(h)
|
|
175
|
+
civil(h['y'], h['m'], h['d'], h['sg'])
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
Exception.class_eval do
|
|
180
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
181
|
+
unless defined?(self.as_json)
|
|
182
|
+
def as_json(*)
|
|
183
|
+
{JSON.create_id => self.class.name, 'm' => message, 'b' => backtrace }
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
def self.json_create(h)
|
|
187
|
+
e = new(h['m'])
|
|
188
|
+
e.set_backtrace(h['b'])
|
|
189
|
+
e
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
Range.class_eval do
|
|
194
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
195
|
+
unless defined?(self.as_json)
|
|
196
|
+
def as_json(*)
|
|
197
|
+
{JSON.create_id => 'Range', 'a' => [first, last, exclude_end?]}
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
def self.json_create(h)
|
|
201
|
+
new(*h['a'])
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
Rational.class_eval do
|
|
206
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
207
|
+
unless defined?(self.as_json)
|
|
208
|
+
def as_json(*)
|
|
209
|
+
{JSON.create_id => 'Rational', 'n' => numerator, 'd' => denominator }
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
def self.json_create(h)
|
|
213
|
+
Rational(h['n'], h['d'])
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
Regexp.class_eval do
|
|
218
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
219
|
+
unless defined?(self.as_json)
|
|
220
|
+
def as_json(*)
|
|
221
|
+
{JSON.create_id => 'Regexp', 'o' => options, 's' => source }
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
def self.json_create(h)
|
|
225
|
+
new(h['s'], h['o'])
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
Struct.class_eval do
|
|
230
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
231
|
+
unless defined?(self.as_json)
|
|
232
|
+
def as_json(*)
|
|
233
|
+
name = self.class.name.to_s
|
|
234
|
+
raise JSON::JSONError, "Only named structs are supported!" if 0 == name.length
|
|
235
|
+
|
|
236
|
+
{ JSON.create_id => name, 'v' => values }
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
def self.json_create(h)
|
|
240
|
+
new(*h['v'])
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
Symbol.class_eval do
|
|
245
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
246
|
+
unless defined?(self.as_json)
|
|
247
|
+
def as_json(*)
|
|
248
|
+
{JSON.create_id => 'Symbol', 's' => to_s }
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
def self.json_create(h)
|
|
252
|
+
h['s'].to_sym
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
Time.class_eval do
|
|
257
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
|
258
|
+
unless defined?(self.as_json)
|
|
259
|
+
def as_json(*)
|
|
260
|
+
nsecs = [ tv_usec * 1000 ]
|
|
261
|
+
nsecs << tv_nsec if respond_to?(:tv_nsec)
|
|
262
|
+
nsecs = nsecs.max
|
|
263
|
+
{ JSON.create_id => 'Time', 's' => tv_sec, 'n' => nsecs }
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
def self.json_create(h)
|
|
267
|
+
if (usec = h.delete('u'))
|
|
268
|
+
h['n'] = usec * 1000
|
|
269
|
+
end
|
|
270
|
+
if instance_methods.include?(:tv_nsec)
|
|
271
|
+
at(h['s'], Rational(h['n'], 1000))
|
|
272
|
+
else
|
|
273
|
+
at(h['s'], h['n'] / 1000)
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
end # self.mimic_loaded
|
|
278
|
+
|
|
279
|
+
end # Oj
|
|
280
|
+
|
|
281
|
+
# More monkey patches.
|
|
282
|
+
class String
|
|
283
|
+
def to_json_raw_object
|
|
284
|
+
{
|
|
285
|
+
JSON.create_id => self.class.name,
|
|
286
|
+
'raw' => self.bytes
|
|
287
|
+
}
|
|
288
|
+
end
|
|
289
|
+
def to_json_raw(*)
|
|
290
|
+
to_json_raw_object().to_json()
|
|
291
|
+
end
|
|
292
|
+
def self.json_create(obj)
|
|
293
|
+
s = ''
|
|
294
|
+
s.encode!(Encoding::ASCII_8BIT) if s.respond_to?(:encode!)
|
|
295
|
+
raw = obj['raw']
|
|
296
|
+
if raw.is_a? Array
|
|
297
|
+
raw.each { |v| s << v }
|
|
298
|
+
end
|
|
299
|
+
s
|
|
300
|
+
end
|
|
301
|
+
end
|
|
@@ -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/lib/oj_windows.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_windows/version'
|
|
8
|
+
require 'oj_windows/bag'
|
|
9
|
+
require 'oj_windows/easy_hash'
|
|
10
|
+
require 'oj_windows/error'
|
|
11
|
+
require 'oj_windows/mimic'
|
|
12
|
+
require 'oj_windows/saj'
|
|
13
|
+
require 'oj_windows/schandler'
|
|
14
|
+
|
|
15
|
+
require 'oj_windows/oj_windows' # C extension
|