test/test_object.rb in oj-2.18.5 vs test/test_object.rb in oj-3.0.0

- old
+ new

@@ -26,10 +26,53 @@ def self.json_create(h) self.new(h['x'], h['y']) end end # Jeez + class Jam + attr_accessor :x, :y + + def initialize(x, y) + @x = x + @y = y + end + + def eql?(o) + self.class == o.class && @x == o.x && @y == o.y + end + alias == eql? + + end # Jam + + class Jazz < Jam + def initialize(x, y) + super + end + def to_hash() + { 'json_class' => self.class.to_s, 'x' => @x, 'y' => @y } + end + def self.json_create(h) + self.new(h['x'], h['y']) + end + end # Jazz + + class Orange < Jam + def initialize(x, y) + super + end + + def as_json() + { :json_class => self.class, + :x => @x, + :y => @y } + end + + def self.json_create(h) + self.new(h['x'], h['y']) + end + end + module One module Two module Three class Deep @@ -206,10 +249,16 @@ dump_and_load(1.7775, false) dump_and_load(2.5024, false) dump_and_load(2.48e16, false) dump_and_load(2.48e100 * 1.0e10, false) dump_and_load(-2.48e100 * 1.0e10, false) + dump_and_load(1/0.0, false) + # NaN does not always == NaN + json = Oj.dump(0/0.0, :mode => :object) + assert_equal('3.3e14159265358979323846', json) + loaded = Oj.load(json); + assert_equal(true, loaded.nan?) end def test_string dump_and_load('', false) dump_and_load('abc', false) @@ -218,10 +267,11 @@ end def test_symbol dump_and_load(:abc, false) dump_and_load(":abc", false) + dump_and_load(':xyz'.to_sym, false) end def test_encode opts = Oj.default_options Oj.default_options = { :ascii_only => false } @@ -302,10 +352,15 @@ orig = BigDecimal.new('80.51') json = Oj.dump(orig, :mode => :object, :bigdecimal_as_decimal => true) bg = Oj.load(json, :mode => :object, :bigdecimal_load => true) assert_equal(BigDecimal, bg.class) assert_equal(orig, bg) + # Infinity is the same for Float and BigDecimal + json = Oj.dump(BigDecimal.new('Infinity'), :mode => :object) + assert_equal('Infinity', json) + json = Oj.dump(BigDecimal.new('-Infinity'), :mode => :object) + assert_equal('-Infinity', json) end # Stream IO def test_io_string json = %{{ @@ -343,10 +398,26 @@ } obj = Oj.object_load(json, :symbol_keys => true) assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj) end + def test_class_object + dump_and_load(ObjectJuice, false) + end + + def test_module_object + dump_and_load(One, false) + end + + def test_non_str_hash_object + json = Oj.dump({ 1 => true, :sim => nil }, :mode => :object) + h = Oj.load(json, :mode => :strict) + assert_equal({"^#1" => [1, true], ":sim" => nil}, h) + h = Oj.load(json, :mode => :object) + assert_equal({ 1 => true, :sim => nil }, h) + end + # comments def test_comment_slash json = %{{ "x":true,//three "y":58, @@ -595,11 +666,11 @@ def test_json_object_bad json = %{{"^o":"Junk","x":true}} begin Oj.object_load(json) rescue Exception => e - assert_equal("Oj::ParseError", e.class().name) + assert_equal("ArgumentError", e.class().name) return end assert(false, "*** expected an exception") end @@ -643,10 +714,124 @@ json = Oj.dump({ 1 => true, 'nil' => nil, :sim => 4 }) h = Oj.object_load(json) assert_equal({ 1 => true, 'nil' => nil, :sim => 4 }, h) end + def test_json_object_object + obj = Jeez.new(true, 58) + json = Oj.dump(obj, :mode => :object, :indent => 2) + assert(%{{ + "^o":"ObjectJuice::Jeez", + "x":true, + "y":58 +} +} == json || +%{{ + "^o":"ObjectJuice::Jeez", + "y":58, + "x":true +} +} == json) + obj2 = Oj.load(json, :mode => :object) + assert_equal(obj, obj2) + end + + def test_to_hash_object_object + obj = Jazz.new(true, 58) + json = Oj.dump(obj, :mode => :object, :indent => 2) + assert(%{{ + "^o":"ObjectJuice::Jazz", + "x":true, + "y":58 +} +} == json || +%{{ + "^o":"ObjectJuice::Jazz", + "y":58, + "x":true +} +} == json) + obj2 = Oj.load(json, :mode => :object) + assert_equal(obj, obj2) + end + + def test_as_json_object_object + obj = Orange.new(true, 58) + json = Oj.dump(obj, :mode => :object, :indent => 2) + assert(%{{ + "^o":"ObjectJuice::Orange", + "x":true, + "y":58 +} +} == json || +%{{ + "^o":"ObjectJuice::Orange", + "y":58, + "x":true +} +} == json) + obj2 = Oj.load(json, :mode => :object) + assert_equal(obj, obj2) + end + + def test_object_object_no_cache + obj = Jam.new(true, 58) + json = Oj.dump(obj, :mode => :object, :indent => 2) + assert(%{{ + "^o":"ObjectJuice::Jam", + "x":true, + "y":58 +} +} == json || +%{{ + "^o":"ObjectJuice::Jam", + "y":58, + "x":true +} +} == json) + obj2 = Oj.load(json, :mode => :object, :class_cache => false) + assert_equal(obj, obj2) + end + + def test_exception + err = nil + begin + raise StandardError.new('A Message') + rescue Exception => e + err = e + end + json = Oj.dump(err, :mode => :object, :indent => 2) + #puts "*** #{json}" + e2 = Oj.load(json, :mode => :strict) + assert_equal(err.class.to_s, e2['^o']) + assert_equal(err.message, e2['~mesg']) + assert_equal(err.backtrace, e2['~bt']) + e2 = Oj.load(json, :mode => :object) + if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby + assert_equal(e.class, e2.class); + assert_equal(e.message, e2.message); + assert_equal(e.backtrace, e2.backtrace); + else + assert_equal(e, e2); + end + end + + def test_range_object + unless RUBY_VERSION.start_with?('1.8') + Oj.default_options = { :mode => :object } + json = Oj.dump(1..7, :mode => :object, :indent => 0) + if 'rubinius' == $ruby + assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json) + else + assert_equal(%{{"^u":["Range",1,7,false]}}, json) + end + dump_and_load(1..7, false) + dump_and_load(1..1, false) + dump_and_load(1...7, false) + end + end + def test_circular_hash h = { 'a' => 7 } h['b'] = h json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true) h2 = Oj.object_load(json, :circular => true) @@ -659,37 +844,113 @@ json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true) a2 = Oj.object_load(json, :circular => true) assert_equal(a2[1].__id__, a2.__id__) end + def test_circular_array2 + a = [7] + a << a + json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true) + assert_equal(%{[ + "^i1", + 7, + "^r1" +] +}, json) + a2 = Oj.load(json, :mode => :object, :circular => true) + assert_equal(a2[1].__id__, a2.__id__) + end + + def test_circular_hash2 + h = { 'a' => 7 } + h['b'] = h + json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true) + ha = Oj.load(json, :mode => :strict) + assert_equal({'^i' => 1, 'a' => 7, 'b' => '^r1'}, ha) + Oj.load(json, :mode => :object, :circular => true) + assert_equal(h['b'].__id__, h.__id__) + end + def test_circular_object obj = Jeez.new(nil, 58) obj.x = obj json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true) obj2 = Oj.object_load(json, :circular => true) assert_equal(obj2.x.__id__, obj2.__id__) end + def test_circular_object2 + obj = Jam.new(nil, 58) + obj.x = obj + json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true) + assert(%{{ + "^o":"ObjectJuice::Jam", + "^i":1, + "x":"^r1", + "y":58 +} +} == json || +%{{ + "^o":"ObjectJuice::Jam", + "^i":1, + "y":58, + "x":"^r1" +} +} == json) + obj2 = Oj.load(json, :mode => :object, :circular => true) + assert_equal(obj2.x.__id__, obj2.__id__) + end + def test_circular h = { 'a' => 7 } obj = Jeez.new(h, 58) obj.x['b'] = obj json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true) Oj.object_load(json, :circular => true) assert_equal(obj.x.__id__, h.__id__) assert_equal(h['b'].__id__, obj.__id__) end + def test_circular2 + h = { 'a' => 7 } + obj = Jam.new(h, 58) + obj.x['b'] = obj + json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true) + ha = Oj.load(json, :mode => :strict) + assert_equal({'^o' => 'ObjectJuice::Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha) + Oj.load(json, :mode => :object, :circular => true) + assert_equal(obj.x.__id__, h.__id__) + assert_equal(h['b'].__id__, obj.__id__) + end + + def test_omit_nil + jam = Jam.new({'a' => 1, 'b' => nil }, nil) + + json = Oj.dump(jam, :omit_nil => true, :mode => :object) + assert_equal(%|{"^o":"ObjectJuice::Jam","x":{"a":1}}|, json) + end + def test_odd_date dump_and_load(Date.new(2012, 6, 19), false) end - + def test_odd_datetime dump_and_load(DateTime.new(2012, 6, 19, 13, 5, Rational(4, 3)), false) dump_and_load(DateTime.new(2012, 6, 19, 13, 5, Rational(7123456789, 1000000000)), false) end + def test_bag + json = %{{ + "^o":"ObjectJuice::Jem", + "x":true, + "y":58 }} + obj = Oj.load(json, :mode => :object, :auto_define => true) + assert_equal('ObjectJuice::Jem', obj.class.name) + assert_equal(true, obj.x) + assert_equal(58, obj.y) + end + def test_odd_string Oj.register_odd(Strung, Strung, :create, :to_s, 'safe?') s = Strung.new("Pete", true) dump_and_load(s, false) end @@ -736,10 +997,14 @@ def dump_and_load(obj, trace=false) json = Oj.dump(obj, :indent => 2, :mode => :object) puts json if trace loaded = Oj.object_load(json); - assert_equal(obj, loaded) + if obj.nil? + assert_nil(loaded) + else + assert_equal(obj, loaded) + end loaded end end