oj 2.18.5 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -226
- data/ext/oj/circarray.c +0 -25
- data/ext/oj/circarray.h +0 -25
- data/ext/oj/code.c +227 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +126 -38
- data/ext/oj/custom.c +1097 -0
- data/ext/oj/dump.c +658 -2376
- data/ext/oj/dump.h +92 -0
- data/ext/oj/dump_compat.c +937 -0
- data/ext/oj/dump_leaf.c +254 -0
- data/ext/oj/dump_object.c +810 -0
- data/ext/oj/dump_rails.c +329 -0
- data/ext/oj/dump_strict.c +416 -0
- data/ext/oj/err.c +0 -25
- data/ext/oj/err.h +8 -2
- data/ext/oj/fast.c +24 -24
- data/ext/oj/mimic_json.c +817 -0
- data/ext/oj/mimic_rails.c +806 -0
- data/ext/oj/mimic_rails.h +17 -0
- data/ext/oj/object.c +18 -72
- data/ext/oj/odd.c +0 -25
- data/ext/oj/odd.h +2 -27
- data/ext/oj/oj.c +655 -1503
- data/ext/oj/oj.h +93 -40
- data/ext/oj/parse.c +99 -46
- data/ext/oj/parse.h +12 -26
- data/ext/oj/reader.c +1 -25
- data/ext/oj/reader.h +3 -25
- data/ext/oj/resolve.c +9 -11
- data/ext/oj/resolve.h +2 -2
- data/ext/oj/rxclass.c +133 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +4 -25
- data/ext/oj/scp.c +3 -25
- data/ext/oj/sparse.c +89 -13
- data/ext/oj/stream_writer.c +301 -0
- data/ext/oj/strict.c +4 -27
- data/ext/oj/string_writer.c +480 -0
- data/ext/oj/val_stack.h +6 -2
- data/lib/oj.rb +1 -23
- data/lib/oj/easy_hash.rb +12 -4
- data/lib/oj/json.rb +172 -0
- data/lib/oj/mimic.rb +123 -18
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +1 -1
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +79 -0
- data/pages/Modes.md +140 -0
- data/pages/Options.md +250 -0
- data/pages/Rails.md +60 -0
- data/pages/Security.md +20 -0
- data/test/activesupport4/decoding_test.rb +105 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/decoding_test.rb +125 -0
- data/test/activesupport5/encoding_test.rb +483 -0
- data/test/activesupport5/encoding_test_cases.rb +90 -0
- data/test/activesupport5/test_helper.rb +50 -0
- data/test/activesupport5/time_zone_test_helpers.rb +24 -0
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +143 -0
- data/test/json_gem/json_encoding_test.rb +109 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +383 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +18 -0
- data/test/perf_compat.rb +30 -28
- data/test/perf_object.rb +1 -1
- data/test/perf_strict.rb +18 -1
- data/test/sample.rb +0 -1
- data/test/test_compat.rb +169 -93
- data/test/test_custom.rb +355 -0
- data/test/test_file.rb +0 -8
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +268 -3
- data/test/test_scp.rb +22 -1
- data/test/test_strict.rb +160 -4
- data/test/test_various.rb +52 -620
- data/test/tests.rb +14 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- metadata +89 -47
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/foo.rb +0 -24
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/test/test_object.rb
CHANGED
@@ -28,6 +28,49 @@ class ObjectJuice < Minitest::Test
|
|
28
28
|
end
|
29
29
|
end # Jeez
|
30
30
|
|
31
|
+
class Jam
|
32
|
+
attr_accessor :x, :y
|
33
|
+
|
34
|
+
def initialize(x, y)
|
35
|
+
@x = x
|
36
|
+
@y = y
|
37
|
+
end
|
38
|
+
|
39
|
+
def eql?(o)
|
40
|
+
self.class == o.class && @x == o.x && @y == o.y
|
41
|
+
end
|
42
|
+
alias == eql?
|
43
|
+
|
44
|
+
end # Jam
|
45
|
+
|
46
|
+
class Jazz < Jam
|
47
|
+
def initialize(x, y)
|
48
|
+
super
|
49
|
+
end
|
50
|
+
def to_hash()
|
51
|
+
{ 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y }
|
52
|
+
end
|
53
|
+
def self.json_create(h)
|
54
|
+
self.new(h['x'], h['y'])
|
55
|
+
end
|
56
|
+
end # Jazz
|
57
|
+
|
58
|
+
class Orange < Jam
|
59
|
+
def initialize(x, y)
|
60
|
+
super
|
61
|
+
end
|
62
|
+
|
63
|
+
def as_json()
|
64
|
+
{ :json_class => self.class,
|
65
|
+
:x => @x,
|
66
|
+
:y => @y }
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.json_create(h)
|
70
|
+
self.new(h['x'], h['y'])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
31
74
|
module One
|
32
75
|
module Two
|
33
76
|
module Three
|
@@ -208,6 +251,12 @@ class ObjectJuice < Minitest::Test
|
|
208
251
|
dump_and_load(2.48e16, false)
|
209
252
|
dump_and_load(2.48e100 * 1.0e10, false)
|
210
253
|
dump_and_load(-2.48e100 * 1.0e10, false)
|
254
|
+
dump_and_load(1/0.0, false)
|
255
|
+
# NaN does not always == NaN
|
256
|
+
json = Oj.dump(0/0.0, :mode => :object)
|
257
|
+
assert_equal('3.3e14159265358979323846', json)
|
258
|
+
loaded = Oj.load(json);
|
259
|
+
assert_equal(true, loaded.nan?)
|
211
260
|
end
|
212
261
|
|
213
262
|
def test_string
|
@@ -220,6 +269,7 @@ class ObjectJuice < Minitest::Test
|
|
220
269
|
def test_symbol
|
221
270
|
dump_and_load(:abc, false)
|
222
271
|
dump_and_load(":abc", false)
|
272
|
+
dump_and_load(':xyz'.to_sym, false)
|
223
273
|
end
|
224
274
|
|
225
275
|
def test_encode
|
@@ -304,6 +354,11 @@ class ObjectJuice < Minitest::Test
|
|
304
354
|
bg = Oj.load(json, :mode => :object, :bigdecimal_load => true)
|
305
355
|
assert_equal(BigDecimal, bg.class)
|
306
356
|
assert_equal(orig, bg)
|
357
|
+
# Infinity is the same for Float and BigDecimal
|
358
|
+
json = Oj.dump(BigDecimal.new('Infinity'), :mode => :object)
|
359
|
+
assert_equal('Infinity', json)
|
360
|
+
json = Oj.dump(BigDecimal.new('-Infinity'), :mode => :object)
|
361
|
+
assert_equal('-Infinity', json)
|
307
362
|
end
|
308
363
|
|
309
364
|
# Stream IO
|
@@ -345,6 +400,22 @@ class ObjectJuice < Minitest::Test
|
|
345
400
|
assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
|
346
401
|
end
|
347
402
|
|
403
|
+
def test_class_object
|
404
|
+
dump_and_load(ObjectJuice, false)
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_module_object
|
408
|
+
dump_and_load(One, false)
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_non_str_hash_object
|
412
|
+
json = Oj.dump({ 1 => true, :sim => nil }, :mode => :object)
|
413
|
+
h = Oj.load(json, :mode => :strict)
|
414
|
+
assert_equal({"^#1" => [1, true], ":sim" => nil}, h)
|
415
|
+
h = Oj.load(json, :mode => :object)
|
416
|
+
assert_equal({ 1 => true, :sim => nil }, h)
|
417
|
+
end
|
418
|
+
|
348
419
|
# comments
|
349
420
|
def test_comment_slash
|
350
421
|
json = %{{
|
@@ -597,7 +668,7 @@ class ObjectJuice < Minitest::Test
|
|
597
668
|
begin
|
598
669
|
Oj.object_load(json)
|
599
670
|
rescue Exception => e
|
600
|
-
assert_equal("
|
671
|
+
assert_equal("ArgumentError", e.class().name)
|
601
672
|
return
|
602
673
|
end
|
603
674
|
assert(false, "*** expected an exception")
|
@@ -645,6 +716,120 @@ class ObjectJuice < Minitest::Test
|
|
645
716
|
assert_equal({ 1 => true, 'nil' => nil, :sim => 4 }, h)
|
646
717
|
end
|
647
718
|
|
719
|
+
def test_json_object_object
|
720
|
+
obj = Jeez.new(true, 58)
|
721
|
+
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
722
|
+
assert(%{{
|
723
|
+
"^o":"ObjectJuice::Jeez",
|
724
|
+
"x":true,
|
725
|
+
"y":58
|
726
|
+
}
|
727
|
+
} == json ||
|
728
|
+
%{{
|
729
|
+
"^o":"ObjectJuice::Jeez",
|
730
|
+
"y":58,
|
731
|
+
"x":true
|
732
|
+
}
|
733
|
+
} == json)
|
734
|
+
obj2 = Oj.load(json, :mode => :object)
|
735
|
+
assert_equal(obj, obj2)
|
736
|
+
end
|
737
|
+
|
738
|
+
def test_to_hash_object_object
|
739
|
+
obj = Jazz.new(true, 58)
|
740
|
+
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
741
|
+
assert(%{{
|
742
|
+
"^o":"ObjectJuice::Jazz",
|
743
|
+
"x":true,
|
744
|
+
"y":58
|
745
|
+
}
|
746
|
+
} == json ||
|
747
|
+
%{{
|
748
|
+
"^o":"ObjectJuice::Jazz",
|
749
|
+
"y":58,
|
750
|
+
"x":true
|
751
|
+
}
|
752
|
+
} == json)
|
753
|
+
obj2 = Oj.load(json, :mode => :object)
|
754
|
+
assert_equal(obj, obj2)
|
755
|
+
end
|
756
|
+
|
757
|
+
def test_as_json_object_object
|
758
|
+
obj = Orange.new(true, 58)
|
759
|
+
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
760
|
+
assert(%{{
|
761
|
+
"^o":"ObjectJuice::Orange",
|
762
|
+
"x":true,
|
763
|
+
"y":58
|
764
|
+
}
|
765
|
+
} == json ||
|
766
|
+
%{{
|
767
|
+
"^o":"ObjectJuice::Orange",
|
768
|
+
"y":58,
|
769
|
+
"x":true
|
770
|
+
}
|
771
|
+
} == json)
|
772
|
+
obj2 = Oj.load(json, :mode => :object)
|
773
|
+
assert_equal(obj, obj2)
|
774
|
+
end
|
775
|
+
|
776
|
+
def test_object_object_no_cache
|
777
|
+
obj = Jam.new(true, 58)
|
778
|
+
json = Oj.dump(obj, :mode => :object, :indent => 2)
|
779
|
+
assert(%{{
|
780
|
+
"^o":"ObjectJuice::Jam",
|
781
|
+
"x":true,
|
782
|
+
"y":58
|
783
|
+
}
|
784
|
+
} == json ||
|
785
|
+
%{{
|
786
|
+
"^o":"ObjectJuice::Jam",
|
787
|
+
"y":58,
|
788
|
+
"x":true
|
789
|
+
}
|
790
|
+
} == json)
|
791
|
+
obj2 = Oj.load(json, :mode => :object, :class_cache => false)
|
792
|
+
assert_equal(obj, obj2)
|
793
|
+
end
|
794
|
+
|
795
|
+
def test_exception
|
796
|
+
err = nil
|
797
|
+
begin
|
798
|
+
raise StandardError.new('A Message')
|
799
|
+
rescue Exception => e
|
800
|
+
err = e
|
801
|
+
end
|
802
|
+
json = Oj.dump(err, :mode => :object, :indent => 2)
|
803
|
+
#puts "*** #{json}"
|
804
|
+
e2 = Oj.load(json, :mode => :strict)
|
805
|
+
assert_equal(err.class.to_s, e2['^o'])
|
806
|
+
assert_equal(err.message, e2['~mesg'])
|
807
|
+
assert_equal(err.backtrace, e2['~bt'])
|
808
|
+
e2 = Oj.load(json, :mode => :object)
|
809
|
+
if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
|
810
|
+
assert_equal(e.class, e2.class);
|
811
|
+
assert_equal(e.message, e2.message);
|
812
|
+
assert_equal(e.backtrace, e2.backtrace);
|
813
|
+
else
|
814
|
+
assert_equal(e, e2);
|
815
|
+
end
|
816
|
+
end
|
817
|
+
|
818
|
+
def test_range_object
|
819
|
+
unless RUBY_VERSION.start_with?('1.8')
|
820
|
+
Oj.default_options = { :mode => :object }
|
821
|
+
json = Oj.dump(1..7, :mode => :object, :indent => 0)
|
822
|
+
if 'rubinius' == $ruby
|
823
|
+
assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
|
824
|
+
else
|
825
|
+
assert_equal(%{{"^u":["Range",1,7,false]}}, json)
|
826
|
+
end
|
827
|
+
dump_and_load(1..7, false)
|
828
|
+
dump_and_load(1..1, false)
|
829
|
+
dump_and_load(1...7, false)
|
830
|
+
end
|
831
|
+
end
|
832
|
+
|
648
833
|
def test_circular_hash
|
649
834
|
h = { 'a' => 7 }
|
650
835
|
h['b'] = h
|
@@ -661,6 +846,30 @@ class ObjectJuice < Minitest::Test
|
|
661
846
|
assert_equal(a2[1].__id__, a2.__id__)
|
662
847
|
end
|
663
848
|
|
849
|
+
def test_circular_array2
|
850
|
+
a = [7]
|
851
|
+
a << a
|
852
|
+
json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true)
|
853
|
+
assert_equal(%{[
|
854
|
+
"^i1",
|
855
|
+
7,
|
856
|
+
"^r1"
|
857
|
+
]
|
858
|
+
}, json)
|
859
|
+
a2 = Oj.load(json, :mode => :object, :circular => true)
|
860
|
+
assert_equal(a2[1].__id__, a2.__id__)
|
861
|
+
end
|
862
|
+
|
863
|
+
def test_circular_hash2
|
864
|
+
h = { 'a' => 7 }
|
865
|
+
h['b'] = h
|
866
|
+
json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
|
867
|
+
ha = Oj.load(json, :mode => :strict)
|
868
|
+
assert_equal({'^i' => 1, 'a' => 7, 'b' => '^r1'}, ha)
|
869
|
+
Oj.load(json, :mode => :object, :circular => true)
|
870
|
+
assert_equal(h['b'].__id__, h.__id__)
|
871
|
+
end
|
872
|
+
|
664
873
|
def test_circular_object
|
665
874
|
obj = Jeez.new(nil, 58)
|
666
875
|
obj.x = obj
|
@@ -669,6 +878,28 @@ class ObjectJuice < Minitest::Test
|
|
669
878
|
assert_equal(obj2.x.__id__, obj2.__id__)
|
670
879
|
end
|
671
880
|
|
881
|
+
def test_circular_object2
|
882
|
+
obj = Jam.new(nil, 58)
|
883
|
+
obj.x = obj
|
884
|
+
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
|
885
|
+
assert(%{{
|
886
|
+
"^o":"ObjectJuice::Jam",
|
887
|
+
"^i":1,
|
888
|
+
"x":"^r1",
|
889
|
+
"y":58
|
890
|
+
}
|
891
|
+
} == json ||
|
892
|
+
%{{
|
893
|
+
"^o":"ObjectJuice::Jam",
|
894
|
+
"^i":1,
|
895
|
+
"y":58,
|
896
|
+
"x":"^r1"
|
897
|
+
}
|
898
|
+
} == json)
|
899
|
+
obj2 = Oj.load(json, :mode => :object, :circular => true)
|
900
|
+
assert_equal(obj2.x.__id__, obj2.__id__)
|
901
|
+
end
|
902
|
+
|
672
903
|
def test_circular
|
673
904
|
h = { 'a' => 7 }
|
674
905
|
obj = Jeez.new(h, 58)
|
@@ -679,15 +910,45 @@ class ObjectJuice < Minitest::Test
|
|
679
910
|
assert_equal(h['b'].__id__, obj.__id__)
|
680
911
|
end
|
681
912
|
|
913
|
+
def test_circular2
|
914
|
+
h = { 'a' => 7 }
|
915
|
+
obj = Jam.new(h, 58)
|
916
|
+
obj.x['b'] = obj
|
917
|
+
json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
|
918
|
+
ha = Oj.load(json, :mode => :strict)
|
919
|
+
assert_equal({'^o' => 'ObjectJuice::Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
|
920
|
+
Oj.load(json, :mode => :object, :circular => true)
|
921
|
+
assert_equal(obj.x.__id__, h.__id__)
|
922
|
+
assert_equal(h['b'].__id__, obj.__id__)
|
923
|
+
end
|
924
|
+
|
925
|
+
def test_omit_nil
|
926
|
+
jam = Jam.new({'a' => 1, 'b' => nil }, nil)
|
927
|
+
|
928
|
+
json = Oj.dump(jam, :omit_nil => true, :mode => :object)
|
929
|
+
assert_equal(%|{"^o":"ObjectJuice::Jam","x":{"a":1}}|, json)
|
930
|
+
end
|
931
|
+
|
682
932
|
def test_odd_date
|
683
933
|
dump_and_load(Date.new(2012, 6, 19), false)
|
684
934
|
end
|
685
|
-
|
935
|
+
|
686
936
|
def test_odd_datetime
|
687
937
|
dump_and_load(DateTime.new(2012, 6, 19, 13, 5, Rational(4, 3)), false)
|
688
938
|
dump_and_load(DateTime.new(2012, 6, 19, 13, 5, Rational(7123456789, 1000000000)), false)
|
689
939
|
end
|
690
940
|
|
941
|
+
def test_bag
|
942
|
+
json = %{{
|
943
|
+
"^o":"ObjectJuice::Jem",
|
944
|
+
"x":true,
|
945
|
+
"y":58 }}
|
946
|
+
obj = Oj.load(json, :mode => :object, :auto_define => true)
|
947
|
+
assert_equal('ObjectJuice::Jem', obj.class.name)
|
948
|
+
assert_equal(true, obj.x)
|
949
|
+
assert_equal(58, obj.y)
|
950
|
+
end
|
951
|
+
|
691
952
|
def test_odd_string
|
692
953
|
Oj.register_odd(Strung, Strung, :create, :to_s, 'safe?')
|
693
954
|
s = Strung.new("Pete", true)
|
@@ -738,7 +999,11 @@ class ObjectJuice < Minitest::Test
|
|
738
999
|
json = Oj.dump(obj, :indent => 2, :mode => :object)
|
739
1000
|
puts json if trace
|
740
1001
|
loaded = Oj.object_load(json);
|
741
|
-
|
1002
|
+
if obj.nil?
|
1003
|
+
assert_nil(loaded)
|
1004
|
+
else
|
1005
|
+
assert_equal(obj, loaded)
|
1006
|
+
end
|
742
1007
|
loaded
|
743
1008
|
end
|
744
1009
|
|
data/test/test_scp.rb
CHANGED
@@ -5,6 +5,7 @@ $: << File.dirname(__FILE__)
|
|
5
5
|
|
6
6
|
require 'helper'
|
7
7
|
require 'socket'
|
8
|
+
require 'stringio'
|
8
9
|
|
9
10
|
$json = %{{
|
10
11
|
"array": [
|
@@ -260,7 +261,27 @@ class ScpTest < Minitest::Test
|
|
260
261
|
def test_double
|
261
262
|
handler = AllHandler.new()
|
262
263
|
json = %{{"one":true,"two":false}{"three":true,"four":false}}
|
263
|
-
Oj.sc_parse(handler, json)
|
264
|
+
Oj.sc_parse(handler, json)
|
265
|
+
assert_equal([[:hash_start],
|
266
|
+
[:hash_key, 'one'],
|
267
|
+
[:hash_set, 'one', true],
|
268
|
+
[:hash_key, 'two'],
|
269
|
+
[:hash_set, 'too', false],
|
270
|
+
[:hash_end],
|
271
|
+
[:add_value, {}],
|
272
|
+
[:hash_start],
|
273
|
+
[:hash_key, 'three'],
|
274
|
+
[:hash_set, 'three', true],
|
275
|
+
[:hash_key, 'four'],
|
276
|
+
[:hash_set, 'four', false],
|
277
|
+
[:hash_end],
|
278
|
+
[:add_value, {}]], handler.calls)
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_double_io
|
282
|
+
handler = AllHandler.new()
|
283
|
+
json = %{{"one":true,"two":false}{"three":true,"four":false}}
|
284
|
+
Oj.sc_parse(handler, StringIO.new(json))
|
264
285
|
assert_equal([[:hash_start],
|
265
286
|
[:hash_key, 'one'],
|
266
287
|
[:hash_set, 'one', true],
|
data/test/test_strict.rb
CHANGED
@@ -2,13 +2,36 @@
|
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
4
|
$: << File.dirname(__FILE__)
|
5
|
+
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
6
|
+
%w(lib ext).each do |dir|
|
7
|
+
$: << File.join($oj_dir, dir)
|
8
|
+
end
|
5
9
|
|
6
|
-
require '
|
10
|
+
require 'minitest'
|
11
|
+
require 'minitest/autorun'
|
12
|
+
require 'stringio'
|
13
|
+
require 'date'
|
14
|
+
require 'bigdecimal'
|
15
|
+
require 'oj'
|
7
16
|
|
8
17
|
class StrictJuice < Minitest::Test
|
9
18
|
|
19
|
+
module TestModule
|
20
|
+
end
|
21
|
+
|
22
|
+
class Jeez
|
23
|
+
attr_accessor :x, :y
|
24
|
+
|
25
|
+
def initialize(x, y)
|
26
|
+
@x = x
|
27
|
+
@y = y
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
10
31
|
def setup
|
11
32
|
@default_options = Oj.default_options
|
33
|
+
# in strict mode other options other than the number formats are not used.
|
34
|
+
Oj.default_options = { :mode => :strict }
|
12
35
|
end
|
13
36
|
|
14
37
|
def teardown
|
@@ -46,6 +69,42 @@ class StrictJuice < Minitest::Test
|
|
46
69
|
dump_and_load(-2.48e100 * 1.0e10, false)
|
47
70
|
end
|
48
71
|
|
72
|
+
def test_nan_dump
|
73
|
+
assert_equal('null', Oj.dump(0/0.0, :nan => :null))
|
74
|
+
assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
|
75
|
+
begin
|
76
|
+
Oj.dump(0/0.0, :nan => :word)
|
77
|
+
rescue Exception
|
78
|
+
assert(true)
|
79
|
+
return
|
80
|
+
end
|
81
|
+
assert(false, "*** expected an exception")
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_infinity_dump
|
85
|
+
assert_equal('null', Oj.dump(1/0.0, :nan => :null))
|
86
|
+
assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :nan => :huge))
|
87
|
+
begin
|
88
|
+
Oj.dump(1/0.0, :nan => :word)
|
89
|
+
rescue Exception
|
90
|
+
assert(true)
|
91
|
+
return
|
92
|
+
end
|
93
|
+
assert(false, "*** expected an exception")
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_neg_infinity_dump
|
97
|
+
assert_equal('null', Oj.dump(-1/0.0, :nan => :null))
|
98
|
+
assert_equal('-3.0e14159265358979323846', Oj.dump(-1/0.0, :nan => :huge))
|
99
|
+
begin
|
100
|
+
Oj.dump(-1/0.0, :nan => :word)
|
101
|
+
rescue Exception
|
102
|
+
assert(true)
|
103
|
+
return
|
104
|
+
end
|
105
|
+
assert(false, "*** expected an exception")
|
106
|
+
end
|
107
|
+
|
49
108
|
def test_string
|
50
109
|
dump_and_load('', false)
|
51
110
|
dump_and_load('abc', false)
|
@@ -96,6 +155,15 @@ class StrictJuice < Minitest::Test
|
|
96
155
|
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
|
97
156
|
end
|
98
157
|
|
158
|
+
def test_deep_nest
|
159
|
+
begin
|
160
|
+
n = 10000
|
161
|
+
Oj.strict_load('[' * n + ']' * n)
|
162
|
+
rescue Exception => e
|
163
|
+
assert(false, e.message)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
99
167
|
# Hash
|
100
168
|
def test_hash
|
101
169
|
dump_and_load({}, false)
|
@@ -132,23 +200,55 @@ class StrictJuice < Minitest::Test
|
|
132
200
|
assert_equal({"a\nb" => true, "c\td" => false}, obj)
|
133
201
|
end
|
134
202
|
|
203
|
+
def test_non_str_hash
|
204
|
+
begin
|
205
|
+
Oj.dump({ 1 => true, 0 => false })
|
206
|
+
rescue Exception
|
207
|
+
assert(true)
|
208
|
+
return
|
209
|
+
end
|
210
|
+
assert(false, "*** expected an exception")
|
211
|
+
end
|
212
|
+
|
135
213
|
def test_bignum_object
|
136
214
|
dump_and_load(7 ** 55, false)
|
137
215
|
end
|
138
216
|
|
139
217
|
# BigDecimal
|
140
218
|
def test_bigdecimal_strict
|
219
|
+
Oj.default_options = { :bigdecimal_load => true}
|
141
220
|
dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
|
142
221
|
end
|
143
222
|
|
144
223
|
def test_bigdecimal_load
|
145
224
|
orig = BigDecimal.new('80.51')
|
146
|
-
json = Oj.dump(orig, :mode => :
|
147
|
-
bg = Oj.load(json, :mode => :
|
225
|
+
json = Oj.dump(orig, :mode => :strict, :bigdecimal_as_decimal => true)
|
226
|
+
bg = Oj.load(json, :mode => :strict, :bigdecimal_load => true)
|
148
227
|
assert_equal(BigDecimal, bg.class)
|
149
228
|
assert_equal(orig, bg)
|
150
229
|
end
|
151
230
|
|
231
|
+
def test_json_object
|
232
|
+
obj = Jeez.new(true, 58)
|
233
|
+
begin
|
234
|
+
Oj.dump(obj)
|
235
|
+
rescue Exception
|
236
|
+
assert(true)
|
237
|
+
return
|
238
|
+
end
|
239
|
+
assert(false, "*** expected an exception")
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_range
|
243
|
+
begin
|
244
|
+
Oj.dump(1..7)
|
245
|
+
rescue Exception
|
246
|
+
assert(true)
|
247
|
+
return
|
248
|
+
end
|
249
|
+
assert(false, "*** expected an exception")
|
250
|
+
end
|
251
|
+
|
152
252
|
# Stream IO
|
153
253
|
def test_io_string
|
154
254
|
json = %{{
|
@@ -176,6 +276,42 @@ class StrictJuice < Minitest::Test
|
|
176
276
|
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
|
177
277
|
end
|
178
278
|
|
279
|
+
def test_symbol
|
280
|
+
json = Oj.dump(:abc)
|
281
|
+
assert_equal('"abc"', json)
|
282
|
+
end
|
283
|
+
|
284
|
+
def test_time
|
285
|
+
t = Time.local(2012, 1, 5, 23, 58, 7)
|
286
|
+
begin
|
287
|
+
Oj.dump(t)
|
288
|
+
rescue Exception
|
289
|
+
assert(true)
|
290
|
+
return
|
291
|
+
end
|
292
|
+
assert(false, "*** expected an exception")
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_class
|
296
|
+
begin
|
297
|
+
Oj.dump(StrictJuice)
|
298
|
+
rescue Exception
|
299
|
+
assert(true)
|
300
|
+
return
|
301
|
+
end
|
302
|
+
assert(false, "*** expected an exception")
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_module
|
306
|
+
begin
|
307
|
+
Oj.dump(TestModule)
|
308
|
+
rescue Exception
|
309
|
+
assert(true)
|
310
|
+
return
|
311
|
+
end
|
312
|
+
assert(false, "*** expected an exception")
|
313
|
+
end
|
314
|
+
|
179
315
|
# symbol_keys option
|
180
316
|
def test_symbol_keys
|
181
317
|
json = %{{
|
@@ -243,11 +379,31 @@ class StrictJuice < Minitest::Test
|
|
243
379
|
assert_equal([{ 'x' => 1 }, { 'y' => 2 }], results)
|
244
380
|
end
|
245
381
|
|
382
|
+
def test_circular_hash
|
383
|
+
h = { 'a' => 7 }
|
384
|
+
h['b'] = h
|
385
|
+
json = Oj.dump(h, :indent => 2, :circular => true)
|
386
|
+
assert_equal(%|{
|
387
|
+
"a":7,
|
388
|
+
"b":null
|
389
|
+
}
|
390
|
+
|, json)
|
391
|
+
end
|
392
|
+
|
393
|
+
def test_omit_nil
|
394
|
+
json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true)
|
395
|
+
assert_equal(%|{"x":{"a":1}}|, json)
|
396
|
+
end
|
397
|
+
|
246
398
|
def dump_and_load(obj, trace=false)
|
247
399
|
json = Oj.dump(obj, :indent => 2)
|
248
400
|
puts json if trace
|
249
401
|
loaded = Oj.strict_load(json);
|
250
|
-
|
402
|
+
if obj.nil?
|
403
|
+
assert_nil(loaded)
|
404
|
+
else
|
405
|
+
assert_equal(obj, loaded)
|
406
|
+
end
|
251
407
|
loaded
|
252
408
|
end
|
253
409
|
|