forked from ohler55/oj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_object.rb
executable file
·1032 lines (899 loc) · 25.4 KB
/
test_object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# encoding: utf-8
$: << File.dirname(__FILE__)
require 'helper'
class ObjectJuice < Minitest::Test
class Jeez
attr_accessor :x, :y, :_z
def initialize(x, y)
@x = x
@y = y
@_z = x.to_s
end
def eql?(o)
self.class == o.class && @x == o.x && @y == o.y
end
alias == eql?
def to_json(*a)
%{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
end
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
def initialize()
end
def eql?(o)
self.class == o.class
end
alias == eql?
def to_hash()
{'json_class' => "#{self.class.name}"}
end
def to_json(*a)
%{{"json_class":"#{self.class.name}"}}
end
def self.json_create(h)
self.new()
end
end # Deep
end # Three
end # Two
class Stuck2 < Struct.new(:a, :b)
end
end # One
class Stuck < Struct.new(:a, :b)
end
class Strung < String
def initialize(str, safe)
super(str)
@safe = safe
end
def safe?()
@safe
end
def self.create(str, safe)
new(str, safe)
end
def eql?(o)
super && self.class == o.class && @safe == o.safe?
end
alias == eql?
def inspect()
return super + '(' + @safe + ')'
end
end
class AutoStrung < String
attr_accessor :safe
def initialize(str, safe)
super(str)
@safe = safe
end
def eql?(o)
self.class == o.class && super(o) && @safe == o.safe
end
alias == eql?
end
class AutoArray < Array
attr_accessor :safe
def initialize(a, safe)
super(a)
@safe = safe
end
def eql?(o)
self.class == o.class && super(o) && @safe == o.safe
end
alias == eql?
end
class AutoHash < Hash
attr_accessor :safe
def initialize(h, safe)
super(h)
@safe = safe
end
def eql?(o)
self.class == o.class && super(o) && @safe == o.safe
end
alias == eql?
end
class Raw
attr_accessor :json
def initialize(j)
@json = j
end
def to_json(*a)
@json
end
def self.create(h)
h
end
end # Raw
module Ichi
module Ni
def self.direct(h)
h
end
module San
class Shi
attr_accessor :hash
def initialize(h)
@hash = h
end
def dump()
@hash
end
end # Shi
end # San
end # Ni
end # Ichi
def setup
@default_options = Oj.default_options
end
def teardown
Oj.default_options = @default_options
#=begin
if '3.1.0' <= RUBY_VERSION && !(RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/)
#Oj::debug_odd("teardown before GC.verify_compaction_references")
GC.verify_compaction_references(double_heap: true, toward: :empty)
#Oj::debug_odd("teardown after GC.verify_compaction_references")
end
#=end
end
def test_nil
dump_and_load(nil, false)
end
def test_true
dump_and_load(true, false)
end
def test_false
dump_and_load(false, false)
end
def test_fixnum
dump_and_load(0, false)
dump_and_load(12345, false)
dump_and_load(-54321, false)
dump_and_load(1, false)
end
def test_float
dump_and_load(0.0, false)
dump_and_load(12345.6789, false)
dump_and_load(70.35, false)
dump_and_load(-54321.012, false)
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)
dump_and_load("abc\ndef", false)
dump_and_load("a\u0041", false)
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 }
dump_and_load("ぴーたー", false)
Oj.default_options = { :ascii_only => true }
json = Oj.dump("ぴーたー")
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
dump_and_load("ぴーたー", false)
Oj.default_options = opts
end
def test_unicode
# hits the 3 normal ranges and one extended surrogate pair
json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
obj = Oj.load(json)
json2 = Oj.dump(obj, :ascii_only => true)
assert_equal(json, json2)
end
def test_array
dump_and_load([], false)
dump_and_load([true, false], false)
dump_and_load(['a', 1, nil], false)
dump_and_load([[nil]], false)
dump_and_load([[nil], 58], false)
end
def test_array_deep
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
end
# Hash
def test_hash
dump_and_load({}, false)
dump_and_load({ 'true' => true, 'false' => false}, false)
dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
end
def test_hash_deep
dump_and_load({'1' => {
'2' => {
'3' => {
'4' => {
'5' => {
'6' => {
'7' => {
'8' => {
'9' => {
'10' => {
'11' => {
'12' => {
'13' => {
'14' => {
'15' => {
'16' => {
'17' => {
'18' => {
'19' => {
'20' => {}}}}}}}}}}}}}}}}}}}}}, false)
end
def test_hash_escaped_key
json = %{{"a\nb":true,"c\td":false}}
obj = Oj.object_load(json)
assert_equal({"a\nb" => true, "c\td" => false}, obj)
end
def test_bignum_object
dump_and_load(7 ** 55, false)
end
# BigDecimal
def test_bigdecimal_object
dump_and_load(BigDecimal('3.14159265358979323846'), false)
end
def test_bigdecimal_load
orig = BigDecimal('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('Infinity'), :mode => :object)
assert_equal('Infinity', json)
json = Oj.dump(BigDecimal('-Infinity'), :mode => :object)
assert_equal('-Infinity', json)
end
# Stream IO
def test_io_string
json = %{{
"x":true,
"y":58,
"z": [1,2,3]
}
}
input = StringIO.new(json)
obj = Oj.object_load(input)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_io_file
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
File.open(filename, 'w') { |f| f.write(%{{
"x":true,
"y":58,
"z": [1,2,3]
}
}) }
f = File.new(filename)
obj = Oj.object_load(f)
f.close()
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
# symbol_keys option
def test_symbol_keys
json = %{{
"x":true,
"y":58,
"z": [1,2,3]
}
}
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,
"z": [1,2,
3 // six
]}
}
obj = Oj.object_load(json)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_comment_c
json = %{{
"x"/*one*/:/*two*/true,
"y":58,
"z": [1,2,3]}
}
obj = Oj.object_load(json)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_comment
json = %{{
"x"/*one*/:/*two*/true,//three
"y":58/*four*/,
"z": [1,2/*five*/,
3 // six
]
}
}
obj = Oj.object_load(json)
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
end
def test_json_module_object
obj = One::Two::Three::Deep.new()
dump_and_load(obj, false)
end
def test_xml_time
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :xmlschema)
#puts "*** json for test_xml_time '#{json}'"
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_xml_time_utc
t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :xmlschema)
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_ruby_time
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :ruby)
#puts "*** json for test_ruby_time '#{json}'"
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_ruby_time_12345
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, 12345/60*60)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :ruby)
#puts "*** json for test_ruby_time '#{json}'"
loaded = Oj.object_load(json);
#puts "*** loaded: #{loaded}"
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_ruby_time_utc
t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :ruby)
#puts json
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_time_early
# Windows does not support dates before 1970.
return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
t = Time.new(1954, 1, 5, 21, 37, 7.123456789, -8 * 3600)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
#puts json
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_time_unix_zone
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, -8 * 3600)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
#puts json
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_time_unix_zone_12345
t = Time.new(2015, 1, 5, 21, 37, 7.123456789, 12345)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
#puts json
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_time_unix_zone_utc
t = Time.utc(2015, 1, 5, 21, 37, 7.123456789)
# The fractional seconds are not always recreated exactly which causes a
# mismatch so instead the seconds, nsecs, and gmt_offset are checked
# separately along with utc.
json = Oj.dump(t, :mode => :object, :time_format => :unix_zone)
#puts json
loaded = Oj.object_load(json);
assert_equal(t.tv_sec, loaded.tv_sec)
if t.respond_to?(:tv_nsec)
assert_equal(t.tv_nsec, loaded.tv_nsec)
else
assert_equal(t.tv_usec, loaded.tv_usec)
end
assert_equal(t.utc?, loaded.utc?)
assert_equal(t.utc_offset, loaded.utc_offset)
end
def test_json_object
obj = Jeez.new(true, 58)
dump_and_load(obj, false)
end
def test_json_object_create_deep
obj = One::Two::Three::Deep.new()
dump_and_load(obj, false)
end
def test_json_object_bad
json = %{{"^o":"Junk","x":true}}
begin
Oj.object_load(json)
rescue Exception => e
assert_equal("ArgumentError", e.class().name)
return
end
assert(false, "*** expected an exception")
end
def test_json_object_not_hat_hash
json = %{{"^#x":[1,2]}}
h = Oj.object_load(json)
assert_equal({1 => 2}, h);
json = %{{"~#x":[1,2]}}
h = Oj.object_load(json)
assert_equal({'~#x' => [1,2]}, h);
end
def test_json_struct
obj = Stuck.new(false, 7)
dump_and_load(obj, false)
end
def test_json_struct2
obj = One::Stuck2.new(false, 7)
dump_and_load(obj, false)
end
def test_json_anonymous_struct
s = Struct.new(:x, :y)
obj = s.new(1, 2)
json = Oj.dump(obj, :indent => 2, :mode => :object)
#puts json
loaded = Oj.object_load(json);
assert_equal(obj.members, loaded.members)
assert_equal(obj.values, loaded.values)
end
def test_json_non_str_hash
obj = { 59 => "young", false => true }
dump_and_load(obj, false)
end
def test_mixed_hash_object
Oj.default_options = { :mode => :object }
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, ignore_under: true)
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_ignore
obj = Jeez.new(true, 58)
json = Oj.dump({ 'a' => 7, 'b' => obj }, :mode => :object, :indent => 2, :ignore => [ Jeez ])
assert_equal(%|{
"a":7
}
|, json)
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 '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
class SubX < Exception
def initialize
super("sub")
@xyz = 123
end
end
def test_exception_subclass
err = nil
begin
raise SubX.new
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)
assert_equal(e, e2);
end
def test_range_object
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
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)
assert_equal(h2['b'].__id__, h2.__id__)
end
def test_json_object_missing_fields
json = %{{ "^u": [ "ObjectJuice::Stuck",1]}}
obj = Oj.load(json, mode: :object)
assert_nil(obj['b'])
end
def test_circular_array
a = [7]
a << a
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_array3
a = ['^r1']
json = Oj.dump(a, mode: :object, circular: true)
assert_equal(%{["^i1","\\u005er1"]}, json)
a2 = Oj.load(json, mode: :object, circular: true)
assert_equal(a, a2)
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)
Oj.register_odd(Date, Date, :jd, :jd)
json = Oj.dump(Date.new(2015, 3, 7), :mode => :object)
assert_equal(%|{"^O":"Date","jd":2457089}|, json)
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
def test_odd_raw
Oj.register_odd_raw(Raw, Raw, :create, :to_json)
json = Oj.dump(Raw.new(%|{"a":1}|), :mode => :object)
assert_equal(%|{"^O":"ObjectJuice::Raw","to_json":{"a":1}}|, json)
h = Oj.load(json, :mode => :object)
assert_equal({'a' => 1}, h)
end
def test_odd_mod
Oj.register_odd(Ichi::Ni, Ichi::Ni, :direct, :dump)
json = Oj.dump(Ichi::Ni::San::Shi.new({'a' => 1}), :mode => :object)
assert_equal(%|{"^O":"ObjectJuice::Ichi::Ni::San::Shi","dump":{"a":1}}|, json)
h = Oj.load(json, :mode => :object)
assert_equal({'a' => 1}, h)