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
data/lib/oj/schandler.rb
ADDED
|
@@ -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
|
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
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/time'
|
|
4
|
+
|
|
5
|
+
module Oj
|
|
6
|
+
# Exists only to handle the ActiveSupport::TimeWithZone.
|
|
7
|
+
class ActiveSupportHelper
|
|
8
|
+
def self.createTimeWithZone(utc, zone)
|
|
9
|
+
ActiveSupport::TimeWithZone.new(utc - utc.gmt_offset, ActiveSupport::TimeZone[zone])
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
Oj.register_odd(ActiveSupport::TimeWithZone, Oj::ActiveSupportHelper, :createTimeWithZone, :utc, 'time_zone.name')
|
|
15
|
+
|
|
16
|
+
# This is a hack to work around an oddness with DateTime and the ActiveSupport
|
|
17
|
+
# that causes a hang when some methods are called from C. Hour, min(ute),
|
|
18
|
+
# sec(ond) and other methods are special but they can be called from C until
|
|
19
|
+
# activesupport/time is required. After that they can not be even though
|
|
20
|
+
# resond_to? returns true. By defining methods to call super the problem goes
|
|
21
|
+
# away. There is obviously some magic going on under the covers that I don't
|
|
22
|
+
# understand.
|
|
23
|
+
class DateTime
|
|
24
|
+
def hour()
|
|
25
|
+
super
|
|
26
|
+
end
|
|
27
|
+
def min()
|
|
28
|
+
super
|
|
29
|
+
end
|
|
30
|
+
def sec()
|
|
31
|
+
super
|
|
32
|
+
end
|
|
33
|
+
def sec_fraction()
|
|
34
|
+
super
|
|
35
|
+
end
|
|
36
|
+
def offset()
|
|
37
|
+
super
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oj
|
|
4
|
+
|
|
5
|
+
# A generic class that is used only for storing attributes. It is the base
|
|
6
|
+
# Class for auto-generated classes in the storage system. Instance variables
|
|
7
|
+
# are added using the instance_variable_set() method. All instance variables
|
|
8
|
+
# can be accessed using the variable name (without the @ prefix). No setters
|
|
9
|
+
# are provided as the Class is intended for reading only.
|
|
10
|
+
class Bag
|
|
11
|
+
|
|
12
|
+
# The initializer can take multiple arguments in the form of key values
|
|
13
|
+
# where the key is the variable name and the value is the variable
|
|
14
|
+
# value. This is intended for testing purposes only.
|
|
15
|
+
# @example Oj::Bag.new(:@x => 42, :@y => 57)
|
|
16
|
+
# @param [Hash] args instance variable symbols and their values
|
|
17
|
+
def initialize(args = {})
|
|
18
|
+
args.each do |k, v|
|
|
19
|
+
self.instance_variable_set(k, v)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Replaces the Object.respond_to?() method.
|
|
24
|
+
# @param [Symbol] m method symbol
|
|
25
|
+
# @return [Boolean] true for any method that matches an instance
|
|
26
|
+
# variable reader, otherwise false.
|
|
27
|
+
def respond_to?(m)
|
|
28
|
+
return true if super
|
|
29
|
+
|
|
30
|
+
instance_variables.include?(:"@#{m}")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Handles requests for variable values. Others cause an Exception to be
|
|
34
|
+
# raised.
|
|
35
|
+
# @param [Symbol] m method symbol
|
|
36
|
+
# @return [Boolean] the value of the specified instance variable.
|
|
37
|
+
# @raise [ArgumentError] if an argument is given. Zero arguments expected.
|
|
38
|
+
# @raise [NoMethodError] if the instance variable is not defined.
|
|
39
|
+
def method_missing(m, *args, &block)
|
|
40
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 0) to method #{m}") unless args.nil? or args.empty?
|
|
41
|
+
|
|
42
|
+
at_m = :"@#{m}"
|
|
43
|
+
raise NoMethodError.new("undefined method #{m}", m) unless instance_variable_defined?(at_m)
|
|
44
|
+
|
|
45
|
+
instance_variable_get(at_m)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Replaces eql?() with something more reasonable for this Class.
|
|
49
|
+
# @param [Object] other Object to compare self to
|
|
50
|
+
# @return [Boolean] true if each variable and value are the same, otherwise false.
|
|
51
|
+
def eql?(other)
|
|
52
|
+
return false if (other.nil? or self.class != other.class)
|
|
53
|
+
|
|
54
|
+
ova = other.instance_variables
|
|
55
|
+
iv = instance_variables
|
|
56
|
+
return false if ova.size != iv.size
|
|
57
|
+
|
|
58
|
+
iv.all? { |vid| instance_variable_get(vid) != other.instance_variable_get(vid) }
|
|
59
|
+
end
|
|
60
|
+
alias == eql?
|
|
61
|
+
|
|
62
|
+
# Define a new class based on the Oj::Bag class. This is used internally in
|
|
63
|
+
# the Oj module and is available to service wrappers that receive XML
|
|
64
|
+
# requests that include Objects of Classes not defined in the storage
|
|
65
|
+
# process.
|
|
66
|
+
# @param [String] classname Class name or symbol that includes Module names.
|
|
67
|
+
# @return [Object] an instance of the specified Class.
|
|
68
|
+
# @raise [NameError] if the classname is invalid.
|
|
69
|
+
def self.define_class(classname)
|
|
70
|
+
classname = classname.to_s unless classname.is_a?(String)
|
|
71
|
+
tokens = classname.split('::').map(&:to_sym)
|
|
72
|
+
raise NameError.new("Invalid classname '#{classname}") if tokens.empty?
|
|
73
|
+
|
|
74
|
+
m = Object
|
|
75
|
+
tokens[0..-2].each do |sym|
|
|
76
|
+
if m.const_defined?(sym)
|
|
77
|
+
m = m.const_get(sym)
|
|
78
|
+
else
|
|
79
|
+
c = Module.new
|
|
80
|
+
m.const_set(sym, c)
|
|
81
|
+
m = c
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
sym = tokens[-1]
|
|
85
|
+
if m.const_defined?(sym)
|
|
86
|
+
c = m.const_get(sym)
|
|
87
|
+
else
|
|
88
|
+
c = Class.new(Oj::Bag)
|
|
89
|
+
m.const_set(sym, c)
|
|
90
|
+
end
|
|
91
|
+
c
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
end # Bag
|
|
95
|
+
end # Oj
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module Oj
|
|
2
|
+
|
|
3
|
+
# A Hash subclass that normalizes the hash keys to allow lookup by the
|
|
4
|
+
# key.to_s or key.to_sym. It also supports looking up hash values by methods
|
|
5
|
+
# that match the keys.
|
|
6
|
+
class EasyHash < Hash
|
|
7
|
+
|
|
8
|
+
# Replaces the Object.respond_to?() method.
|
|
9
|
+
# @param [Symbol] m method symbol
|
|
10
|
+
# @param [Boolean] include_all whether to include private and protected methods in the search
|
|
11
|
+
# @return [Boolean] true for any method that matches an instance
|
|
12
|
+
# variable reader, otherwise false.
|
|
13
|
+
def respond_to?(m, include_all = false)
|
|
14
|
+
return true if super
|
|
15
|
+
return true if has_key?(m)
|
|
16
|
+
return true if has_key?(m.to_s)
|
|
17
|
+
|
|
18
|
+
has_key?(m.to_sym)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def [](key)
|
|
22
|
+
return fetch(key, nil) if has_key?(key)
|
|
23
|
+
return fetch(key.to_s, nil) if has_key?(key.to_s)
|
|
24
|
+
|
|
25
|
+
fetch(key.to_sym, nil)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Handles requests for Hash values. Others cause an Exception to be raised.
|
|
29
|
+
# @param [Symbol|String] m method symbol
|
|
30
|
+
# @return [Boolean] the value of the specified instance variable.
|
|
31
|
+
# @raise [ArgumentError] if an argument is given. Zero arguments expected.
|
|
32
|
+
# @raise [NoMethodError] if the instance variable is not defined.
|
|
33
|
+
def method_missing(m, *args, &block)
|
|
34
|
+
if m.to_s.end_with?('=')
|
|
35
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 1 with #{m}) to method #{m}") if args.nil? or 1 != args.length
|
|
36
|
+
|
|
37
|
+
m = m[0..-2]
|
|
38
|
+
return store(m.to_s, args[0]) if has_key?(m.to_s)
|
|
39
|
+
return store(m.to_sym, args[0]) if has_key?(m.to_sym)
|
|
40
|
+
|
|
41
|
+
return store(m, args[0])
|
|
42
|
+
else
|
|
43
|
+
raise ArgumentError.new("wrong number of arguments (#{args.size} for 0 with #{m}) to method #{m}") unless args.nil? or args.empty?
|
|
44
|
+
return fetch(m, nil) if has_key?(m)
|
|
45
|
+
return fetch(m.to_s, nil) if has_key?(m.to_s)
|
|
46
|
+
return fetch(m.to_sym, nil) if has_key?(m.to_sym)
|
|
47
|
+
end
|
|
48
|
+
raise NoMethodError.new("undefined method #{m}", m)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end # EasyHash
|
|
52
|
+
end # Oj
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Oj
|
|
2
|
+
|
|
3
|
+
# Inherit Error class from StandardError.
|
|
4
|
+
Error = Class.new(StandardError)
|
|
5
|
+
|
|
6
|
+
# Following classes inherit from the Error class.
|
|
7
|
+
# -----------------------------------------------
|
|
8
|
+
|
|
9
|
+
# An Exception that is raised as a result of a parse error while parsing a JSON document.
|
|
10
|
+
ParseError = Class.new(Error)
|
|
11
|
+
|
|
12
|
+
# An Exception that is raised as a result of a path being too deep.
|
|
13
|
+
DepthError = Class.new(Error)
|
|
14
|
+
|
|
15
|
+
# An Exception that is raised if a file fails to load.
|
|
16
|
+
LoadError = Class.new(Error)
|
|
17
|
+
|
|
18
|
+
# An Exception that is raised if there is a conflict with mimicking JSON
|
|
19
|
+
MimicError = Class.new(Error)
|
|
20
|
+
|
|
21
|
+
end # Oj
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
require 'ostruct'
|
|
2
|
+
require 'oj_windows/state'
|
|
3
|
+
|
|
4
|
+
if defined?(JSON::PRETTY_STATE_PROTOTYPE)
|
|
5
|
+
# There are enough people that try to use both the json gen and oj in mimic
|
|
6
|
+
# mode so don't display the warning.
|
|
7
|
+
|
|
8
|
+
# warn "WARNING: oj_windows/json is a compatability shim used by Oj. Requiring the file explicitly is not recommended."
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
unless defined?(JSON::PRETTY_STATE_PROTOTYPE)
|
|
12
|
+
module JSON
|
|
13
|
+
NaN = 0.0/0.0 unless defined?(::JSON::NaN)
|
|
14
|
+
Infinity = 1.0/0.0 unless defined?(::JSON::Infinity)
|
|
15
|
+
MinusInfinity = -1.0/0.0 unless defined?(::JSON::MinusInfinity)
|
|
16
|
+
# Taken from the unit test. Note that items like check_circular? are not
|
|
17
|
+
# present.
|
|
18
|
+
PRETTY_STATE_PROTOTYPE = Ext::Generator::State.from_state({
|
|
19
|
+
:allow_nan => false,
|
|
20
|
+
:array_nl => "\n",
|
|
21
|
+
:ascii_only => false,
|
|
22
|
+
:buffer_initial_length => 1024,
|
|
23
|
+
:depth => 0,
|
|
24
|
+
:indent => " ",
|
|
25
|
+
:max_nesting => 100,
|
|
26
|
+
:object_nl => "\n",
|
|
27
|
+
:space => " ",
|
|
28
|
+
:space_before => "",
|
|
29
|
+
}) unless defined?(::JSON::PRETTY_STATE_PROTOTYPE)
|
|
30
|
+
SAFE_STATE_PROTOTYPE = Ext::Generator::State.from_state({
|
|
31
|
+
:allow_nan => false,
|
|
32
|
+
:array_nl => "",
|
|
33
|
+
:ascii_only => false,
|
|
34
|
+
:buffer_initial_length => 1024,
|
|
35
|
+
:depth => 0,
|
|
36
|
+
:indent => "",
|
|
37
|
+
:max_nesting => 100,
|
|
38
|
+
:object_nl => "",
|
|
39
|
+
:space => "",
|
|
40
|
+
:space_before => "",
|
|
41
|
+
}) unless defined?(::JSON::SAFE_STATE_PROTOTYPE)
|
|
42
|
+
FAST_STATE_PROTOTYPE = Ext::Generator::State.from_state({
|
|
43
|
+
:allow_nan => false,
|
|
44
|
+
:array_nl => "",
|
|
45
|
+
:ascii_only => false,
|
|
46
|
+
:buffer_initial_length => 1024,
|
|
47
|
+
:depth => 0,
|
|
48
|
+
:indent => "",
|
|
49
|
+
:max_nesting => 0,
|
|
50
|
+
:object_nl => "",
|
|
51
|
+
:space => "",
|
|
52
|
+
:space_before => "",
|
|
53
|
+
}) unless defined?(::JSON::FAST_STATE_PROTOTYPE)
|
|
54
|
+
|
|
55
|
+
def self.dump_default_options
|
|
56
|
+
Oj::MimicDumpOption.new
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.dump_default_options=(h)
|
|
60
|
+
m = Oj::MimicDumpOption.new
|
|
61
|
+
h.each do |k, v|
|
|
62
|
+
m[k] = v
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.parser=(p)
|
|
67
|
+
@@parser = p
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def self.parser()
|
|
71
|
+
@@parser
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.generator=(g)
|
|
75
|
+
@@generator = g
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.generator()
|
|
79
|
+
@@generator
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
module Ext
|
|
83
|
+
class Parser
|
|
84
|
+
def initialize(src)
|
|
85
|
+
raise TypeError.new("already initialized") unless @source.nil?
|
|
86
|
+
|
|
87
|
+
@source = src
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def source()
|
|
91
|
+
raise TypeError.new("already initialized") if @source.nil?
|
|
92
|
+
|
|
93
|
+
@source
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def parse()
|
|
97
|
+
raise TypeError.new("already initialized") if @source.nil?
|
|
98
|
+
|
|
99
|
+
JSON.parse(@source)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
end # Parser
|
|
103
|
+
end # Ext
|
|
104
|
+
|
|
105
|
+
State = ::JSON::Ext::Generator::State unless defined?(::JSON::State)
|
|
106
|
+
|
|
107
|
+
begin
|
|
108
|
+
send(:remove_const, :Parser)
|
|
109
|
+
rescue
|
|
110
|
+
# ignore and move on
|
|
111
|
+
end
|
|
112
|
+
Parser = ::JSON::Ext::Parser unless defined?(::JSON::Parser)
|
|
113
|
+
self.parser = ::JSON::Ext::Parser
|
|
114
|
+
self.generator = ::JSON::Ext::Generator
|
|
115
|
+
|
|
116
|
+
# Taken directly from the json gem. Shamelessly copied. It is similar in
|
|
117
|
+
# some ways to the Oj::Bag class or the Oj::EasyHash class.
|
|
118
|
+
class GenericObject < OpenStruct
|
|
119
|
+
class << self
|
|
120
|
+
alias [] new
|
|
121
|
+
|
|
122
|
+
def json_creatable?
|
|
123
|
+
@json_creatable
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
attr_writer :json_creatable
|
|
127
|
+
|
|
128
|
+
def json_create(data)
|
|
129
|
+
data = data.dup
|
|
130
|
+
data.delete JSON.create_id
|
|
131
|
+
self[data]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def from_hash(object)
|
|
135
|
+
case
|
|
136
|
+
when object.respond_to?(:to_hash)
|
|
137
|
+
result = new
|
|
138
|
+
object.to_hash.each do |key, value|
|
|
139
|
+
result[key] = from_hash(value)
|
|
140
|
+
end
|
|
141
|
+
result
|
|
142
|
+
when object.respond_to?(:to_ary)
|
|
143
|
+
object.to_ary.map { |a| from_hash(a) }
|
|
144
|
+
else
|
|
145
|
+
object
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def load(source, proc = nil, opts = {})
|
|
150
|
+
result = ::JSON.load(source, proc, opts.merge(:object_class => self))
|
|
151
|
+
result.nil? ? new : result
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def dump(obj, *args)
|
|
155
|
+
::JSON.dump(obj, *args)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
end # self
|
|
159
|
+
|
|
160
|
+
self.json_creatable = false
|
|
161
|
+
|
|
162
|
+
def to_hash
|
|
163
|
+
table
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def [](name)
|
|
167
|
+
__send__(name)
|
|
168
|
+
end unless method_defined?(:[])
|
|
169
|
+
|
|
170
|
+
def []=(name, value)
|
|
171
|
+
__send__("#{name}=", value)
|
|
172
|
+
end unless method_defined?(:[]=)
|
|
173
|
+
|
|
174
|
+
def |(other)
|
|
175
|
+
self.class[other.to_hash.merge(to_hash)]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def as_json(*)
|
|
179
|
+
{ JSON.create_id => self.class.name }.merge to_hash
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def to_json(*a)
|
|
183
|
+
as_json.to_json(*a)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
end # JSON
|
|
188
|
+
end
|