oj 2.9.4 → 2.9.5
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 +17 -1
- data/ext/oj/dump.c +2 -1
- data/ext/oj/oj.c +54 -3
- data/ext/oj/parse.c +15 -8
- data/ext/oj/parse.h +1 -0
- data/ext/oj/reader.c +3 -2
- data/ext/oj/scp.c +5 -6
- data/lib/oj/mimic.rb +2 -2
- data/lib/oj/version.rb +1 -1
- data/test/_test_mimic_rails.rb +31 -0
- data/test/helper.rb +27 -0
- data/test/{test_mimic.rb → isolated/shared.rb} +69 -50
- data/test/isolated/test_mimic_after.rb +13 -0
- data/test/isolated/test_mimic_alone.rb +12 -0
- data/test/isolated/test_mimic_before.rb +13 -0
- data/test/isolated/test_mimic_define.rb +28 -0
- data/test/isolated/test_mimic_rails_after.rb +19 -0
- data/test/isolated/test_mimic_rails_before.rb +19 -0
- data/test/test_compat.rb +50 -64
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +13 -13
- data/test/test_file.rb +45 -58
- data/test/test_gc.rb +22 -24
- data/test/test_object.rb +100 -114
- data/test/test_saj.rb +16 -14
- data/test/test_scp.rb +21 -14
- data/test/test_strict.rb +9 -23
- data/test/{tests.rb → test_various.rb} +96 -129
- data/test/test_writer.rb +10 -12
- data/test/zip.rb +34 -0
- metadata +57 -18
- data/test/a.rb +0 -38
- data/test/bug.rb +0 -17
- data/test/debian_test.rb +0 -90
- data/test/e.rb +0 -12
- data/test/foo.rb +0 -24
- data/test/lots.rb +0 -68
- data/test/mj.rb +0 -48
- data/test/perf_str.rb +0 -38
- data/test/perf_strictx.rb +0 -97
- data/test/test_mimic_after.rb +0 -35
- data/test/test_strictx.rb +0 -58
- data/test/write_fake_log.rb +0 -63
- data/test/x.rb +0 -59
@@ -1,125 +1,91 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
-
|
5
|
-
# required. That can be set in the RUBYOPT environment variable.
|
6
|
-
# export RUBYOPT=-w
|
4
|
+
$: << File.dirname(__FILE__)
|
7
5
|
|
8
|
-
|
6
|
+
require 'helper'
|
9
7
|
|
10
|
-
|
11
|
-
$: << File.join(File.dirname(__FILE__), "../ext")
|
8
|
+
class Juice < Minitest::Test
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
require 'date'
|
16
|
-
require 'bigdecimal'
|
17
|
-
require 'oj'
|
10
|
+
class Jam
|
11
|
+
attr_accessor :x, :y
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
return false if h1.size != h2.size
|
24
|
-
h1.keys.each do |k|
|
25
|
-
return false unless h1[k] == h2[k]
|
26
|
-
end
|
27
|
-
true
|
28
|
-
end
|
29
|
-
|
30
|
-
class Jam
|
31
|
-
attr_accessor :x, :y
|
32
|
-
|
33
|
-
def initialize(x, y)
|
34
|
-
@x = x
|
35
|
-
@y = y
|
36
|
-
end
|
13
|
+
def initialize(x, y)
|
14
|
+
@x = x
|
15
|
+
@y = y
|
16
|
+
end
|
37
17
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
18
|
+
def eql?(o)
|
19
|
+
self.class == o.class && @x == o.x && @y == o.y
|
20
|
+
end
|
21
|
+
alias == eql?
|
42
22
|
|
43
|
-
end# Jam
|
23
|
+
end# Jam
|
44
24
|
|
45
|
-
class Jeez < Jam
|
46
|
-
|
47
|
-
|
48
|
-
|
25
|
+
class Jeez < Jam
|
26
|
+
def initialize(x, y)
|
27
|
+
super
|
28
|
+
end
|
49
29
|
|
50
|
-
|
51
|
-
|
52
|
-
|
30
|
+
def to_json()
|
31
|
+
%{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
|
32
|
+
end
|
53
33
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end# Jeez
|
34
|
+
def self.json_create(h)
|
35
|
+
self.new(h['x'], h['y'])
|
36
|
+
end
|
37
|
+
end# Jeez
|
58
38
|
|
59
|
-
# contributed by sauliusg to fix as_json
|
60
|
-
class Orange < Jam
|
61
|
-
|
62
|
-
|
63
|
-
|
39
|
+
# contributed by sauliusg to fix as_json
|
40
|
+
class Orange < Jam
|
41
|
+
def initialize(x, y)
|
42
|
+
super
|
43
|
+
end
|
64
44
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
45
|
+
def as_json()
|
46
|
+
{ :json_class => self.class,
|
47
|
+
:x => @x,
|
48
|
+
:y => @y }
|
49
|
+
end
|
70
50
|
|
71
|
-
|
72
|
-
|
51
|
+
def self.json_create(h)
|
52
|
+
self.new(h['x'], h['y'])
|
53
|
+
end
|
73
54
|
end
|
74
|
-
end
|
75
55
|
|
76
|
-
class Melon < Jam
|
77
|
-
|
78
|
-
|
79
|
-
|
56
|
+
class Melon < Jam
|
57
|
+
def initialize(x, y)
|
58
|
+
super
|
59
|
+
end
|
80
60
|
|
81
|
-
|
82
|
-
|
83
|
-
|
61
|
+
def as_json()
|
62
|
+
"#{x} #{y}"
|
63
|
+
end
|
84
64
|
|
85
|
-
|
86
|
-
|
65
|
+
def self.json_create(h)
|
66
|
+
self.new(h['x'], h['y'])
|
67
|
+
end
|
87
68
|
end
|
88
|
-
end
|
89
69
|
|
90
|
-
class Jazz < Jam
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
end# Jazz
|
70
|
+
class Jazz < Jam
|
71
|
+
def initialize(x, y)
|
72
|
+
super
|
73
|
+
end
|
74
|
+
def to_hash()
|
75
|
+
{ 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
|
76
|
+
end
|
77
|
+
def self.json_create(h)
|
78
|
+
self.new(h['x'], h['y'])
|
79
|
+
end
|
80
|
+
end# Jazz
|
101
81
|
|
102
|
-
|
103
|
-
|
104
|
-
{ 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
|
82
|
+
def setup
|
83
|
+
@default_options = Oj.default_options
|
105
84
|
end
|
106
|
-
end # Range
|
107
85
|
|
108
|
-
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
class RailsLike
|
113
|
-
attr_accessor :x
|
114
|
-
def initialize(x)
|
115
|
-
@x = x
|
116
|
-
end
|
117
|
-
def to_json(options = nil)
|
118
|
-
Oj.dump(self, :mode => :compat)
|
86
|
+
def teardown
|
87
|
+
Oj.default_options = @default_options
|
119
88
|
end
|
120
|
-
end # RailsLike
|
121
|
-
|
122
|
-
class Juice < ::Test::Unit::TestCase
|
123
89
|
|
124
90
|
def test0_get_options
|
125
91
|
opts = Oj.default_options()
|
@@ -225,6 +191,7 @@ class Juice < ::Test::Unit::TestCase
|
|
225
191
|
end
|
226
192
|
|
227
193
|
def test_string_object
|
194
|
+
Oj.default_options = {:mode => :object}
|
228
195
|
dump_and_load('abc', false)
|
229
196
|
dump_and_load(':abc', false)
|
230
197
|
end
|
@@ -252,6 +219,11 @@ class Juice < ::Test::Unit::TestCase
|
|
252
219
|
assert_equal(json, json2)
|
253
220
|
end
|
254
221
|
|
222
|
+
def test_null_char
|
223
|
+
assert_raises(Oj::ParseError) { Oj.load("\"\0\"") }
|
224
|
+
assert_raises(Oj::ParseError) { Oj.load("\"\\\0\"") }
|
225
|
+
end
|
226
|
+
|
255
227
|
def test_array
|
256
228
|
dump_and_load([], false)
|
257
229
|
dump_and_load([true, false], false)
|
@@ -262,7 +234,7 @@ class Juice < ::Test::Unit::TestCase
|
|
262
234
|
def test_array_not_closed
|
263
235
|
begin
|
264
236
|
Oj.load('[')
|
265
|
-
rescue Exception
|
237
|
+
rescue Exception
|
266
238
|
assert(true)
|
267
239
|
return
|
268
240
|
end
|
@@ -271,6 +243,7 @@ class Juice < ::Test::Unit::TestCase
|
|
271
243
|
|
272
244
|
# rails encoding tests
|
273
245
|
def test_does_not_escape_entities_by_default
|
246
|
+
Oj.default_options = { :escape_mode => :ascii } # set in mimic mode
|
274
247
|
# use Oj to create the hash since some Rubies don't deal nicely with unicode.
|
275
248
|
json = %{{"key":"I <3 this\\u2028space"}}
|
276
249
|
hash = Oj.load(json)
|
@@ -543,7 +516,7 @@ class Juice < ::Test::Unit::TestCase
|
|
543
516
|
def test_hash_not_closed
|
544
517
|
begin
|
545
518
|
Oj.load('{')
|
546
|
-
rescue Exception
|
519
|
+
rescue Exception
|
547
520
|
assert(true)
|
548
521
|
return
|
549
522
|
end
|
@@ -570,9 +543,9 @@ class Juice < ::Test::Unit::TestCase
|
|
570
543
|
Oj.default_options = { :mode => :compat, :use_to_json => true }
|
571
544
|
obj = Jeez.new(true, 58)
|
572
545
|
json = Oj.dump(obj, :indent => 2)
|
573
|
-
assert(%{{"json_class":"Jeez","x":true,"y":58}
|
546
|
+
assert(%{{"json_class":"Juice::Jeez","x":true,"y":58}
|
574
547
|
} == json ||
|
575
|
-
%{{"json_class":"Jeez","y":58,"x":true}
|
548
|
+
%{{"json_class":"Juice::Jeez","y":58,"x":true}
|
576
549
|
} == json)
|
577
550
|
dump_and_load(obj, false)
|
578
551
|
Oj.default_options = { :mode => :compat, :use_to_json => false }
|
@@ -580,7 +553,7 @@ class Juice < ::Test::Unit::TestCase
|
|
580
553
|
def test_json_object_create_id
|
581
554
|
Oj.default_options = { :mode => :compat, :create_id => 'kson_class' }
|
582
555
|
expected = Jeez.new(true, 58)
|
583
|
-
json = %{{"kson_class":"Jeez","x":true,"y":58}}
|
556
|
+
json = %{{"kson_class":"Juice::Jeez","x":true,"y":58}}
|
584
557
|
obj = Oj.load(json)
|
585
558
|
assert_equal(expected, obj)
|
586
559
|
Oj.default_options = { :create_id => 'json_class' }
|
@@ -589,13 +562,13 @@ class Juice < ::Test::Unit::TestCase
|
|
589
562
|
obj = Jeez.new(true, 58)
|
590
563
|
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
591
564
|
assert(%{{
|
592
|
-
"^o":"Jeez",
|
565
|
+
"^o":"Juice::Jeez",
|
593
566
|
"x":true,
|
594
567
|
"y":58
|
595
568
|
}
|
596
569
|
} == json ||
|
597
570
|
%{{
|
598
|
-
"^o":"Jeez",
|
571
|
+
"^o":"Juice::Jeez",
|
599
572
|
"y":58,
|
600
573
|
"x":true
|
601
574
|
}
|
@@ -630,13 +603,13 @@ class Juice < ::Test::Unit::TestCase
|
|
630
603
|
obj = Jazz.new(true, 58)
|
631
604
|
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
632
605
|
assert(%{{
|
633
|
-
"^o":"Jazz",
|
606
|
+
"^o":"Juice::Jazz",
|
634
607
|
"x":true,
|
635
608
|
"y":58
|
636
609
|
}
|
637
610
|
} == json ||
|
638
611
|
%{{
|
639
|
-
"^o":"Jazz",
|
612
|
+
"^o":"Juice::Jazz",
|
640
613
|
"y":58,
|
641
614
|
"x":true
|
642
615
|
}
|
@@ -682,13 +655,13 @@ class Juice < ::Test::Unit::TestCase
|
|
682
655
|
obj = Orange.new(true, 58)
|
683
656
|
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
684
657
|
assert(%{{
|
685
|
-
"^o":"Orange",
|
658
|
+
"^o":"Juice::Orange",
|
686
659
|
"x":true,
|
687
660
|
"y":58
|
688
661
|
}
|
689
662
|
} == json ||
|
690
663
|
%{{
|
691
|
-
"^o":"Orange",
|
664
|
+
"^o":"Juice::Orange",
|
692
665
|
"y":58,
|
693
666
|
"x":true
|
694
667
|
}
|
@@ -731,13 +704,13 @@ class Juice < ::Test::Unit::TestCase
|
|
731
704
|
obj = Jam.new(true, 58)
|
732
705
|
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
733
706
|
assert(%{{
|
734
|
-
"^o":"Jam",
|
707
|
+
"^o":"Juice::Jam",
|
735
708
|
"x":true,
|
736
709
|
"y":58
|
737
710
|
}
|
738
711
|
} == json ||
|
739
712
|
%{{
|
740
|
-
"^o":"Jam",
|
713
|
+
"^o":"Juice::Jam",
|
741
714
|
"y":58,
|
742
715
|
"x":true
|
743
716
|
}
|
@@ -750,13 +723,13 @@ class Juice < ::Test::Unit::TestCase
|
|
750
723
|
obj = Jam.new(true, 58)
|
751
724
|
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
752
725
|
assert(%{{
|
753
|
-
"^o":"Jam",
|
726
|
+
"^o":"Juice::Jam",
|
754
727
|
"x":true,
|
755
728
|
"y":58
|
756
729
|
}
|
757
730
|
} == json ||
|
758
731
|
%{{
|
759
|
-
"^o":"Jam",
|
732
|
+
"^o":"Juice::Jam",
|
760
733
|
"y":58,
|
761
734
|
"x":true
|
762
735
|
}
|
@@ -932,6 +905,7 @@ class Juice < ::Test::Unit::TestCase
|
|
932
905
|
end
|
933
906
|
end
|
934
907
|
def test_date_object
|
908
|
+
Oj.default_options = {:mode => :object}
|
935
909
|
dump_and_load(Date.new(2012, 6, 19), false)
|
936
910
|
end
|
937
911
|
|
@@ -958,17 +932,18 @@ class Juice < ::Test::Unit::TestCase
|
|
958
932
|
assert_equal(orig.to_s, x)
|
959
933
|
end
|
960
934
|
def test_datetime_object
|
935
|
+
Oj.default_options = {:mode => :object}
|
961
936
|
dump_and_load(DateTime.new(2012, 6, 19), false)
|
962
937
|
end
|
963
938
|
|
964
939
|
# autodefine Oj::Bag
|
965
940
|
def test_bag
|
966
941
|
json = %{{
|
967
|
-
"^o":"Jem",
|
942
|
+
"^o":"Juice::Jem",
|
968
943
|
"x":true,
|
969
944
|
"y":58 }}
|
970
945
|
obj = Oj.load(json, :mode => :object, :auto_define => true)
|
971
|
-
assert_equal('Jem', obj.class.name)
|
946
|
+
assert_equal('Juice::Jem', obj.class.name)
|
972
947
|
assert_equal(true, obj.x)
|
973
948
|
assert_equal(58, obj.y)
|
974
949
|
end
|
@@ -979,14 +954,14 @@ class Juice < ::Test::Unit::TestCase
|
|
979
954
|
obj.x = obj
|
980
955
|
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
|
981
956
|
assert(%{{
|
982
|
-
"^o":"Jam",
|
957
|
+
"^o":"Juice::Jam",
|
983
958
|
"^i":1,
|
984
959
|
"x":"^r1",
|
985
960
|
"y":58
|
986
961
|
}
|
987
962
|
} == json ||
|
988
963
|
%{{
|
989
|
-
"^o":"Jam",
|
964
|
+
"^o":"Juice::Jam",
|
990
965
|
"^i":1,
|
991
966
|
"y":58,
|
992
967
|
"x":"^r1"
|
@@ -1026,7 +1001,7 @@ class Juice < ::Test::Unit::TestCase
|
|
1026
1001
|
obj.x['b'] = obj
|
1027
1002
|
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
|
1028
1003
|
ha = Oj.load(json, :mode => :strict)
|
1029
|
-
assert_equal({'^o' => 'Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
|
1004
|
+
assert_equal({'^o' => 'Juice::Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
|
1030
1005
|
Oj.load(json, :mode => :object, :circular => true)
|
1031
1006
|
assert_equal(obj.x.__id__, h.__id__)
|
1032
1007
|
assert_equal(h['b'].__id__, obj.__id__)
|
@@ -1048,7 +1023,7 @@ class Juice < ::Test::Unit::TestCase
|
|
1048
1023
|
a = []
|
1049
1024
|
10000.times { a << [a] }
|
1050
1025
|
Oj.dump(a)
|
1051
|
-
rescue Exception
|
1026
|
+
rescue Exception
|
1052
1027
|
assert(true)
|
1053
1028
|
return
|
1054
1029
|
end
|
@@ -1068,7 +1043,7 @@ class Juice < ::Test::Unit::TestCase
|
|
1068
1043
|
|
1069
1044
|
def test_io_file
|
1070
1045
|
src = { 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}
|
1071
|
-
filename = 'open_file_test.json'
|
1046
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
1072
1047
|
File.open(filename, "w") { |f|
|
1073
1048
|
Oj.to_stream(f, src)
|
1074
1049
|
}
|
@@ -1129,7 +1104,7 @@ class Juice < ::Test::Unit::TestCase
|
|
1129
1104
|
def test_nilnil_false
|
1130
1105
|
begin
|
1131
1106
|
Oj.load(nil)
|
1132
|
-
rescue Exception
|
1107
|
+
rescue Exception
|
1133
1108
|
assert(true)
|
1134
1109
|
return
|
1135
1110
|
end
|
@@ -1141,18 +1116,10 @@ class Juice < ::Test::Unit::TestCase
|
|
1141
1116
|
assert_equal(nil, obj)
|
1142
1117
|
end
|
1143
1118
|
|
1144
|
-
# Rails re-call test. Active support recalls the json dumper when the to_json
|
1145
|
-
# method is called. This mimics that and verifies Oj detects it.
|
1146
|
-
def test_rails_like
|
1147
|
-
obj = RailsLike.new(3)
|
1148
|
-
json = Oj.dump(obj, :mode => :compat)
|
1149
|
-
assert_equal('{"x":3}', json)
|
1150
|
-
end
|
1151
|
-
|
1152
1119
|
def dump_and_load(obj, trace=false)
|
1153
1120
|
json = Oj.dump(obj, :indent => 2)
|
1154
1121
|
puts json if trace
|
1155
|
-
loaded = Oj.load(json)
|
1122
|
+
loaded = Oj.load(json)
|
1156
1123
|
assert_equal(obj, loaded)
|
1157
1124
|
loaded
|
1158
1125
|
end
|
data/test/test_writer.rb
CHANGED
@@ -1,21 +1,19 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
-
|
5
|
-
# required. That can be set in the RUBYOPT environment variable.
|
6
|
-
# export RUBYOPT=-w
|
4
|
+
$: << File.dirname(__FILE__)
|
7
5
|
|
8
|
-
|
6
|
+
require 'helper'
|
9
7
|
|
10
|
-
|
11
|
-
$: << File.join(File.dirname(__FILE__), "../ext")
|
8
|
+
class OjWriter < Minitest::Test
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
require 'oj'
|
10
|
+
def setup
|
11
|
+
@default_options = Oj.default_options
|
12
|
+
end
|
17
13
|
|
18
|
-
|
14
|
+
def teardown
|
15
|
+
Oj.default_options = @default_options
|
16
|
+
end
|
19
17
|
|
20
18
|
def test_string_writer_empty_array
|
21
19
|
w = Oj::StringWriter.new(:indent => 0)
|
@@ -249,7 +247,7 @@ class OjWriter < ::Test::Unit::TestCase
|
|
249
247
|
end
|
250
248
|
|
251
249
|
def test_stream_writer_mixed_file
|
252
|
-
filename = '
|
250
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
253
251
|
File.open(filename, "w") do |f|
|
254
252
|
w = Oj::StreamWriter.new(f, :indent => 0)
|
255
253
|
w.push_object()
|