oj 2.17.2 → 2.17.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/ext/oj/oj.c +11 -2
- data/lib/oj/mimic.rb +108 -0
- data/lib/oj/version.rb +1 -1
- data/test/isolated/shared.rb +42 -7
- data/test/isolated/test_mimic_as_json.rb +0 -0
- data/test/isolated/test_mimic_rails_after.rb +2 -0
- data/test/isolated/test_mimic_rails_before.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6db88500e6a8f5b5243b53140952b8956fdad9b
|
4
|
+
data.tar.gz: bd67bf4dd52102f84c728f9349a57a79f28cacd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a85842674b8da8c33f9526c14a675535da412be6f52ad136f6ba48fad5cadf99ec5810ebc076fbf2e27dbda4758b5432dc697a11cdcbff5d7955482dfea92f9c
|
7
|
+
data.tar.gz: d5e3c117f7cea742867e06ca02fdcef961ff6b14c58d9b21979d4df556b780d052bb6de5695ecf9b11d2084091102e752553390bb31d666d6205f39708f52b0b
|
data/README.md
CHANGED
@@ -170,6 +170,11 @@ Oj.default_options = {:mode => :compat }
|
|
170
170
|
|
171
171
|
## Releases
|
172
172
|
|
173
|
+
**Release 2.17.3**
|
174
|
+
|
175
|
+
- Updated mimic_JSON to monkey patch OpenStruct, Range, Rational, Regexp, Struct, Symbol,
|
176
|
+
and Time like the JSON gem does. Also added the JSON.create_id accessor.
|
177
|
+
|
173
178
|
**Release 2.17.2**
|
174
179
|
|
175
180
|
- Worked around a problem with DateTime and ActiveSupport that causes a hang
|
data/ext/oj/oj.c
CHANGED
@@ -1860,7 +1860,7 @@ no_op1(VALUE self, VALUE obj) {
|
|
1860
1860
|
}
|
1861
1861
|
|
1862
1862
|
static VALUE
|
1863
|
-
|
1863
|
+
mimic_set_create_id(VALUE self, VALUE id) {
|
1864
1864
|
Check_Type(id, T_STRING);
|
1865
1865
|
|
1866
1866
|
if (0 != oj_default_options.create_id) {
|
@@ -1880,6 +1880,14 @@ mimic_create_id(VALUE self, VALUE id) {
|
|
1880
1880
|
return id;
|
1881
1881
|
}
|
1882
1882
|
|
1883
|
+
static VALUE
|
1884
|
+
mimic_create_id(VALUE self) {
|
1885
|
+
if (0 != oj_default_options.create_id) {
|
1886
|
+
return oj_encode(rb_str_new_cstr(oj_default_options.create_id));
|
1887
|
+
}
|
1888
|
+
return rb_str_new_cstr(json_class);
|
1889
|
+
}
|
1890
|
+
|
1883
1891
|
static struct _Options mimic_object_to_json_options = {
|
1884
1892
|
0, // indent
|
1885
1893
|
No, // circular
|
@@ -2005,7 +2013,8 @@ define_mimic_json(int argc, VALUE *argv, VALUE self) {
|
|
2005
2013
|
}
|
2006
2014
|
rb_define_module_function(mimic, "parser=", no_op1, 1);
|
2007
2015
|
rb_define_module_function(mimic, "generator=", no_op1, 1);
|
2008
|
-
rb_define_module_function(mimic, "create_id=",
|
2016
|
+
rb_define_module_function(mimic, "create_id=", mimic_set_create_id, 1);
|
2017
|
+
rb_define_module_function(mimic, "create_id", mimic_create_id, 0);
|
2009
2018
|
|
2010
2019
|
rb_define_module_function(mimic, "dump", mimic_dump, -1);
|
2011
2020
|
rb_define_module_function(mimic, "load", mimic_load, -1);
|
data/lib/oj/mimic.rb
CHANGED
@@ -1,4 +1,10 @@
|
|
1
1
|
|
2
|
+
begin
|
3
|
+
require 'ostruct'
|
4
|
+
rescue Exception
|
5
|
+
# ignore
|
6
|
+
end
|
7
|
+
|
2
8
|
module Oj
|
3
9
|
|
4
10
|
def self.mimic_loaded(mimic_paths=[])
|
@@ -14,5 +20,107 @@ module Oj
|
|
14
20
|
end
|
15
21
|
mimic_paths.each { |p| $LOADED_FEATURES << p }
|
16
22
|
$LOADED_FEATURES << 'json' unless $LOADED_FEATURES.include?('json')
|
23
|
+
|
24
|
+
if Object.const_defined?('OpenStruct')
|
25
|
+
OpenStruct.class_eval do
|
26
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
27
|
+
unless defined?(self.as_json)
|
28
|
+
def as_json(*)
|
29
|
+
name = self.class.name.to_s
|
30
|
+
raise JSON::JSONError, "Only named structs are supported!" if 0 == name.length
|
31
|
+
{ JSON.create_id => name, 't' => table }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
def self.json_create(h)
|
35
|
+
new(h['t'])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
Range.class_eval do
|
41
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
42
|
+
unless defined?(self.as_json)
|
43
|
+
def as_json(*)
|
44
|
+
{JSON.create_id => 'Range', 'a' => [first, last, exclude_end?]}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
def self.json_create(h)
|
48
|
+
new(h['a'])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
Rational.class_eval do
|
53
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
54
|
+
unless defined?(self.as_json)
|
55
|
+
def as_json(*)
|
56
|
+
{JSON.create_id => 'Rational', 'n' => numerator, 'd' => denominator }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
def self.json_create(h)
|
60
|
+
Rational(h['n'], h['d'])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Regexp.class_eval do
|
65
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
66
|
+
unless defined?(self.as_json)
|
67
|
+
def as_json(*)
|
68
|
+
{JSON.create_id => 'Regexp', 'o' => options, 's' => source }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
def self.json_create(h)
|
72
|
+
new(h['s'], h['o'])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
Struct.class_eval do
|
77
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
78
|
+
unless defined?(self.as_json)
|
79
|
+
def as_json(*)
|
80
|
+
name = self.class.name.to_s
|
81
|
+
raise JSON::JSONError, "Only named structs are supported!" if 0 == name.length
|
82
|
+
{ JSON.create_id => name, 'v' => values }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
def self.json_create(h)
|
86
|
+
new(h['v'])
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
Symbol.class_eval do
|
91
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
92
|
+
unless defined?(self.as_json)
|
93
|
+
def as_json(*)
|
94
|
+
{JSON.create_id => 'Symbol', 's' => to_s }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
def self.json_create(h)
|
98
|
+
h['s'].to_sym
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
Time.class_eval do
|
103
|
+
# Both the JSON gem and Rails monkey patch as_json. Let them battle it out.
|
104
|
+
unless defined?(self.as_json)
|
105
|
+
def as_json(*)
|
106
|
+
{JSON.create_id => 'Symbol', 's' => to_s }
|
107
|
+
nsecs = [ tv_usec * 1000 ]
|
108
|
+
nsecs << tv_nsec if respond_to?(:tv_nsec)
|
109
|
+
nsecs = nsecs.max
|
110
|
+
{ JSON.create_id => 'Time', 's' => tv_sec, 'n' => nsecs }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
def self.json_create(h)
|
114
|
+
if usec = h.delete('u')
|
115
|
+
h['n'] = usec * 1000
|
116
|
+
end
|
117
|
+
if instance_methods.include?(:tv_nsec)
|
118
|
+
at(h['s'], Rational(h['n'], 1000))
|
119
|
+
else
|
120
|
+
at(h['s'], h['n'] / 1000)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
17
125
|
end
|
18
126
|
end
|
data/lib/oj/version.rb
CHANGED
data/test/isolated/shared.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
+
# The rails tests set this to true. Both Rails and the JSON gem monkey patch the
|
4
|
+
# as_json methods on several base classes. Depending on which one replaces the
|
5
|
+
# method last the behavior will be different. Oj.mimic_JSON abides by the same
|
6
|
+
# conflicting behavior and the tests reflect that.
|
7
|
+
$rails_monkey = false unless defined?($rails_monkey)
|
8
|
+
|
3
9
|
class SharedMimicTest < Minitest::Test
|
4
10
|
class Jam
|
5
11
|
attr_accessor :x, :y
|
@@ -56,37 +62,66 @@ class SharedMimicTest < Minitest::Test
|
|
56
62
|
# dump
|
57
63
|
def test_dump_string
|
58
64
|
json = JSON.dump([1, true, nil, @time])
|
59
|
-
|
65
|
+
if $rails_monkey
|
66
|
+
assert_equal(%{[1,true,null,#{@expected_time_string}]}, json)
|
67
|
+
else
|
68
|
+
assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, json)
|
69
|
+
end
|
60
70
|
end
|
61
71
|
|
62
72
|
def test_dump_with_options
|
63
73
|
Oj.default_options= {:indent => 2} # JSON this will not change anything
|
64
74
|
json = JSON.dump([1, true, nil, @time])
|
65
|
-
|
75
|
+
if $rails_monkey
|
76
|
+
assert_equal(%{[
|
66
77
|
1,
|
67
78
|
true,
|
68
79
|
null,
|
69
80
|
#{@expected_time_string}
|
70
81
|
]
|
71
82
|
}, json)
|
83
|
+
else
|
84
|
+
assert_equal(%{[
|
85
|
+
1,
|
86
|
+
true,
|
87
|
+
null,
|
88
|
+
{
|
89
|
+
"json_class":"Time",
|
90
|
+
"s":1400000000,
|
91
|
+
"n\":0
|
92
|
+
}
|
93
|
+
]
|
94
|
+
}, json)
|
95
|
+
end
|
72
96
|
end
|
73
97
|
|
74
98
|
def test_dump_io
|
75
99
|
s = StringIO.new()
|
76
100
|
json = JSON.dump([1, true, nil, @time], s)
|
77
101
|
assert_equal(s, json)
|
78
|
-
|
102
|
+
if $rails_monkey
|
103
|
+
assert_equal(%{[1,true,null,#{@expected_time_string}]}, s.string)
|
104
|
+
else
|
105
|
+
assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, s.string)
|
106
|
+
end
|
79
107
|
end
|
80
108
|
# TBD options
|
81
109
|
|
82
110
|
def test_dump_struct
|
83
|
-
# anonymous Struct
|
84
|
-
|
111
|
+
# anonymous Struct not supported by json so name it
|
112
|
+
if Object.const_defined?("Struct::Abc")
|
113
|
+
s = Struct::Abc
|
114
|
+
else
|
115
|
+
s = Struct.new("Abc", :a, :b, :c)
|
116
|
+
end
|
85
117
|
o = s.new(1, 'two', [true, false])
|
86
118
|
json = JSON.dump(o)
|
87
|
-
# Rails add the as_json method and changes the behavior.
|
88
119
|
if o.respond_to?(:as_json)
|
89
|
-
|
120
|
+
if $rails_monkey
|
121
|
+
assert_equal(%|{"a":1,"b":"two","c":[true,false]}|, json)
|
122
|
+
else
|
123
|
+
assert_equal(%|{"json_class":"Struct::Abc","v":[1,"two",[true,false]]}|, json)
|
124
|
+
end
|
90
125
|
else
|
91
126
|
j = '"' + o.to_s.gsub('"', '\\"') + '"'
|
92
127
|
assert_equal(j, json)
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.17.
|
4
|
+
version: 2.17.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Ohler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|