oj 2.10.4 → 2.11.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of oj might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +18 -11
- data/ext/oj/dump.c +59 -11
- data/ext/oj/odd.c +1 -1
- data/ext/oj/oj.c +52 -7
- data/ext/oj/oj.h +4 -2
- data/lib/oj/active_support_helper.rb +17 -0
- data/lib/oj/version.rb +1 -1
- data/test/_test_mimic_rails.rb +21 -3
- data/test/bug.rb +40 -51
- data/test/bug2.rb +10 -0
- data/test/io.rb +48 -0
- data/test/{test_range.rb → mod.rb} +6 -9
- data/test/struct.rb +29 -0
- data/test/test_serializer.rb +59 -0
- data/test/test_various.rb +83 -20
- data/test/write_timebars.rb +31 -0
- data/test/zip.rb +34 -0
- metadata +11 -6
- data/test/perf1.rb +0 -64
- data/test/perf2.rb +0 -76
- data/test/perf_obj_old.rb +0 -213
data/test/bug.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
1
3
|
#!/usr/bin/env ruby
|
2
4
|
# encoding: UTF-8
|
3
5
|
|
@@ -5,60 +7,47 @@ $: << File.dirname(__FILE__)
|
|
5
7
|
|
6
8
|
require 'helper'
|
7
9
|
|
8
|
-
require 'oj'
|
9
|
-
require 'securerandom'
|
10
|
-
|
11
10
|
class Handler
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def
|
17
|
-
|
11
|
+
def initialize
|
12
|
+
@state = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def hash_start
|
16
|
+
@state << {}
|
17
|
+
@state.last
|
18
|
+
end
|
19
|
+
|
20
|
+
def hash_end
|
21
|
+
@state.pop
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash_set(h,k,v)
|
25
|
+
h.store(k,v)
|
18
26
|
end
|
19
|
-
end
|
20
27
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
name = "/tmp/#{SecureRandom.uuid}"
|
25
|
-
`mkfifo #{name}`
|
26
|
-
if fork
|
27
|
-
open(name, 'r+') do |read_io|
|
28
|
-
p "start reading #{read_io.stat.ftype}"
|
29
|
-
Oj.sc_parse(Handler.new, read_io) {|v| p v}
|
30
|
-
p "stop reading"
|
31
|
-
end
|
32
|
-
else
|
33
|
-
open(name, 'w+') do |write_io|
|
34
|
-
p "start writing #{write_io.stat.ftype} autoclose: #{write_io.autoclose?}"
|
35
|
-
write_io.write json
|
36
|
-
write_io.write json
|
37
|
-
p "stop writing"
|
38
|
-
end
|
39
|
-
sleep(1) # make it obvious that there are two threads
|
40
|
-
open(name, 'w+') do |write_io|
|
41
|
-
p "start writing #{write_io.stat.ftype}"
|
42
|
-
write_io.write json
|
43
|
-
write_io.write json
|
44
|
-
p "stop writing"
|
45
|
-
end
|
28
|
+
def array_start
|
29
|
+
@state << []
|
30
|
+
@state.last
|
46
31
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
write_io.write json
|
60
|
-
p "stop writing"
|
61
|
-
write_io.close
|
62
|
-
end
|
32
|
+
|
33
|
+
|
34
|
+
def array_end
|
35
|
+
@state.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
def array_append(a,v)
|
39
|
+
a << v
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_value(v)
|
43
|
+
p v
|
63
44
|
end
|
45
|
+
|
46
|
+
def error(message, line, column); p "ERROR: #{message}" end
|
64
47
|
end
|
48
|
+
|
49
|
+
$handler = Handler.new
|
50
|
+
|
51
|
+
IO.popen("cat tst") { |p| puts Oj.sc_parse($handler, p) }
|
52
|
+
|
53
|
+
#File.open('tst', 'r') { |file| Oj.sc_parse($handler, file) }
|
data/test/bug2.rb
ADDED
data/test/io.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class Handler
|
9
|
+
def initialize
|
10
|
+
@state = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def hash_start
|
14
|
+
@state << {}
|
15
|
+
@state.last
|
16
|
+
end
|
17
|
+
|
18
|
+
def hash_end
|
19
|
+
@state.pop
|
20
|
+
end
|
21
|
+
|
22
|
+
def hash_set(h,k,v)
|
23
|
+
h.store(k,v)
|
24
|
+
end
|
25
|
+
|
26
|
+
def array_start
|
27
|
+
@state << []
|
28
|
+
@state.last
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def array_end
|
33
|
+
@state.pop
|
34
|
+
end
|
35
|
+
|
36
|
+
def array_append(a,v)
|
37
|
+
a << v
|
38
|
+
end
|
39
|
+
|
40
|
+
def error(message, line, column); p "ERROR: #{message}" end
|
41
|
+
end
|
42
|
+
|
43
|
+
handler = Handler.new
|
44
|
+
def handler.add_value(v)
|
45
|
+
p v
|
46
|
+
end
|
47
|
+
|
48
|
+
Oj.sc_parse(handler, StringIO.new('{"a":"b","c":[1,2,{"d":"e"}]}[4,5,6]'))
|
@@ -1,19 +1,16 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
-
$VERBOSE = true
|
5
|
-
|
6
4
|
%w(lib ext test).each do |dir|
|
7
5
|
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
8
6
|
end
|
9
7
|
|
10
|
-
require 'rubygems' if RUBY_VERSION.start_with?('1.8.')
|
11
8
|
require 'oj'
|
12
9
|
|
13
|
-
Oj.mimic_JSON
|
14
|
-
|
15
|
-
#puts Oj.default_options
|
16
|
-
|
17
|
-
range = ("01".."12")
|
18
10
|
|
19
|
-
|
11
|
+
Thread.new do
|
12
|
+
string_io = StringIO.new('{"foo":"bar"}')
|
13
|
+
Oj.load(string_io)
|
14
|
+
string_io.rewind
|
15
|
+
puts string_io.read
|
16
|
+
end.join
|
data/test/struct.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
# Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
|
5
|
+
# required. That can be set in the RUBYOPT environment variable.
|
6
|
+
# export RUBYOPT=-w
|
7
|
+
|
8
|
+
$VERBOSE = true
|
9
|
+
|
10
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
11
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
12
|
+
|
13
|
+
require 'oj'
|
14
|
+
|
15
|
+
A = Struct.new(:a,:b,:c,:d)
|
16
|
+
B = Struct.new(:e,:f)
|
17
|
+
|
18
|
+
obj = [A.new(55, B.new(1, 'X'), B.new(2, 'Y'), 3)]
|
19
|
+
|
20
|
+
s = Oj.dump(obj, :mode => :object)
|
21
|
+
|
22
|
+
100000.times do
|
23
|
+
Oj.load(s, :mode => :object)
|
24
|
+
# ds = Oj.dump(o, :mode => :object)
|
25
|
+
# if ds != s
|
26
|
+
# puts ds
|
27
|
+
# raise "holy crap"
|
28
|
+
# end
|
29
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
|
6
|
+
%w(lib ext test).each do |dir|
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'minitest'
|
11
|
+
require 'minitest/autorun'
|
12
|
+
require 'oj'
|
13
|
+
|
14
|
+
Oj.mimic_JSON
|
15
|
+
|
16
|
+
require 'rails/all'
|
17
|
+
require 'active_model'
|
18
|
+
require 'active_model_serializers'
|
19
|
+
require 'active_support/json'
|
20
|
+
|
21
|
+
#Oj.mimic_JSON
|
22
|
+
|
23
|
+
class Category
|
24
|
+
include ActiveModel::Model
|
25
|
+
include ActiveModel::SerializerSupport
|
26
|
+
|
27
|
+
attr_accessor :id, :name
|
28
|
+
|
29
|
+
def initialize(id, name)
|
30
|
+
@id = id
|
31
|
+
@name = name
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class CategorySerializer < ActiveModel::Serializer
|
36
|
+
attributes :id, :name
|
37
|
+
end
|
38
|
+
|
39
|
+
class MimicRails < Minitest::Test
|
40
|
+
|
41
|
+
def test_dump_object
|
42
|
+
Oj.default_options= {:indent => 0}
|
43
|
+
category = Category.new(1, 'test')
|
44
|
+
serializer = CategorySerializer.new(category)
|
45
|
+
|
46
|
+
json = serializer.to_json()
|
47
|
+
puts "*** serializer.to_json() #{serializer.to_json()}"
|
48
|
+
assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
|
49
|
+
|
50
|
+
json = serializer.as_json()
|
51
|
+
puts "*** serializer.as_json() #{serializer.as_json()}"
|
52
|
+
assert_equal({"category" => {:id => 1, :name => "test"}}, json)
|
53
|
+
|
54
|
+
json = JSON.dump(serializer)
|
55
|
+
puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
|
56
|
+
assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
|
57
|
+
end
|
58
|
+
|
59
|
+
end # MimicRails
|
data/test/test_various.rb
CHANGED
@@ -7,6 +7,9 @@ require 'helper'
|
|
7
7
|
|
8
8
|
class Juice < Minitest::Test
|
9
9
|
|
10
|
+
module TestModule
|
11
|
+
end
|
12
|
+
|
10
13
|
class Jam
|
11
14
|
attr_accessor :x, :y
|
12
15
|
|
@@ -87,57 +90,67 @@ class Juice < Minitest::Test
|
|
87
90
|
Oj.default_options = @default_options
|
88
91
|
end
|
89
92
|
|
90
|
-
|
93
|
+
=begin
|
94
|
+
# Depending on the order the values may have changed. The set_options sets
|
95
|
+
# should cover the function itself.
|
96
|
+
def test_get_options
|
91
97
|
opts = Oj.default_options()
|
92
98
|
assert_equal({ :indent=>0,
|
93
99
|
:second_precision=>9,
|
94
100
|
:circular=>false,
|
101
|
+
:class_cache=>true,
|
95
102
|
:auto_define=>false,
|
96
103
|
:symbol_keys=>false,
|
97
|
-
:class_cache=>true,
|
98
|
-
:escape_mode=>:json,
|
99
|
-
:mode=>:object,
|
100
|
-
:time_format=>:unix,
|
101
104
|
:bigdecimal_as_decimal=>true,
|
102
|
-
:
|
103
|
-
:use_to_json=>false,
|
105
|
+
:use_to_json=>true,
|
104
106
|
:nilnil=>false,
|
105
107
|
:allow_gc=>true,
|
108
|
+
:quirks_mode=>true,
|
109
|
+
:float_precision=>15,
|
110
|
+
:mode=>:object,
|
111
|
+
:escape_mode=>:json,
|
112
|
+
:time_format=>:unix,
|
113
|
+
:bigdecimal_load=>:auto,
|
106
114
|
:create_id=>'json_class'}, opts)
|
107
115
|
end
|
116
|
+
=end
|
108
117
|
|
109
|
-
def
|
110
|
-
orig =
|
118
|
+
def test_set_options
|
119
|
+
orig ={
|
111
120
|
:indent=>0,
|
112
121
|
:second_precision=>9,
|
113
122
|
:circular=>false,
|
123
|
+
:class_cache=>true,
|
114
124
|
:auto_define=>false,
|
115
125
|
:symbol_keys=>false,
|
116
|
-
:class_cache=>true,
|
117
|
-
:escape_mode=>:ascii,
|
118
|
-
:mode=>:object,
|
119
|
-
:time_format=>:unix,
|
120
126
|
:bigdecimal_as_decimal=>true,
|
121
|
-
:bigdecimal_load=>:auto,
|
122
127
|
:use_to_json=>true,
|
123
128
|
:nilnil=>false,
|
124
129
|
:allow_gc=>true,
|
130
|
+
:quirks_mode=>true,
|
131
|
+
:float_precision=>15,
|
132
|
+
:mode=>:object,
|
133
|
+
:escape_mode=>:json,
|
134
|
+
:time_format=>:unix,
|
135
|
+
:bigdecimal_load=>:auto,
|
125
136
|
:create_id=>'json_class'}
|
126
137
|
o2 = {
|
127
138
|
:indent=>4,
|
128
139
|
:second_precision=>7,
|
129
140
|
:circular=>true,
|
141
|
+
:class_cache=>false,
|
130
142
|
:auto_define=>true,
|
131
143
|
:symbol_keys=>true,
|
132
|
-
:class_cache=>false,
|
133
|
-
:escape_mode=>:json,
|
134
|
-
:mode=>:compat,
|
135
|
-
:time_format=>:ruby,
|
136
144
|
:bigdecimal_as_decimal=>false,
|
137
|
-
:bigdecimal_load=>:bigdecimal,
|
138
145
|
:use_to_json=>false,
|
139
146
|
:nilnil=>true,
|
140
147
|
:allow_gc=>false,
|
148
|
+
:quirks_mode=>true,
|
149
|
+
:float_precision=>15,
|
150
|
+
:mode=>:compat,
|
151
|
+
:escape_mode=>:json,
|
152
|
+
:time_format=>:ruby,
|
153
|
+
:bigdecimal_load=>:bigdecimal,
|
141
154
|
:create_id=>nil}
|
142
155
|
o3 = { :indent => 4 }
|
143
156
|
Oj.default_options = o2
|
@@ -166,6 +179,33 @@ class Juice < Minitest::Test
|
|
166
179
|
dump_and_load(1, false)
|
167
180
|
end
|
168
181
|
|
182
|
+
def test_float_dump
|
183
|
+
Oj.default_options = { :float_precision => 16 }
|
184
|
+
assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
|
185
|
+
Oj.default_options = { :float_precision => 5 }
|
186
|
+
assert_equal('1.4055', Oj.dump(1.405460727))
|
187
|
+
Oj.default_options = { :float_precision => 0 }
|
188
|
+
if RUBY_VERSION.start_with?('1.8')
|
189
|
+
assert_equal('1405460727.72387', Oj.dump(1405460727.723866))
|
190
|
+
else
|
191
|
+
assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
|
192
|
+
end
|
193
|
+
Oj.default_options = { :float_precision => 15 }
|
194
|
+
assert_equal('0.56', Oj.dump(0.56))
|
195
|
+
assert_equal('0.5773', Oj.dump(0.5773))
|
196
|
+
assert_equal('0.6768', Oj.dump(0.6768))
|
197
|
+
assert_equal('0.685', Oj.dump(0.685))
|
198
|
+
assert_equal('0.7032', Oj.dump(0.7032))
|
199
|
+
assert_equal('0.7051', Oj.dump(0.7051))
|
200
|
+
assert_equal('0.8274', Oj.dump(0.8274))
|
201
|
+
assert_equal('0.9149', Oj.dump(0.9149))
|
202
|
+
assert_equal('64.4', Oj.dump(64.4))
|
203
|
+
assert_equal('71.6', Oj.dump(71.6))
|
204
|
+
assert_equal('73.4', Oj.dump(73.4))
|
205
|
+
assert_equal('80.6', Oj.dump(80.6))
|
206
|
+
assert_equal('-95.640172', Oj.dump(-95.640172))
|
207
|
+
end
|
208
|
+
|
169
209
|
def test_float
|
170
210
|
mode = Oj.default_options()[:mode]
|
171
211
|
Oj.default_options = {:mode => :object}
|
@@ -486,6 +526,29 @@ class Juice < Minitest::Test
|
|
486
526
|
dump_and_load(Juice, false)
|
487
527
|
end
|
488
528
|
|
529
|
+
# Module
|
530
|
+
def test_module_strict
|
531
|
+
begin
|
532
|
+
Oj.dump(TestModule, :mode => :strict)
|
533
|
+
rescue Exception
|
534
|
+
assert(true)
|
535
|
+
return
|
536
|
+
end
|
537
|
+
assert(false, "*** expected an exception")
|
538
|
+
end
|
539
|
+
def test_module_null
|
540
|
+
json = Oj.dump(TestModule, :mode => :null)
|
541
|
+
assert_equal('null', json)
|
542
|
+
end
|
543
|
+
def test_module_compat
|
544
|
+
json = Oj.dump(TestModule, :mode => :compat)
|
545
|
+
assert_equal(%{"Juice::TestModule"}, json)
|
546
|
+
end
|
547
|
+
def test_module_object
|
548
|
+
Oj.default_options = { :mode => :object }
|
549
|
+
dump_and_load(TestModule, false)
|
550
|
+
end
|
551
|
+
|
489
552
|
# Hash
|
490
553
|
def test_hash
|
491
554
|
Oj.default_options = { :mode => :strict }
|
@@ -880,7 +943,7 @@ class Juice < Minitest::Test
|
|
880
943
|
BigDecimal.send(:define_method, :as_json) do
|
881
944
|
%{this is big}
|
882
945
|
end
|
883
|
-
json = Oj.dump(orig, :mode => :compat)
|
946
|
+
json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => false)
|
884
947
|
bg = Oj.load(json, :mode => :compat)
|
885
948
|
assert_equal("this is big", bg)
|
886
949
|
BigDecimal.send(:remove_method, :as_json) # cleanup
|