oj 2.18.5 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -226
  3. data/ext/oj/circarray.c +0 -25
  4. data/ext/oj/circarray.h +0 -25
  5. data/ext/oj/code.c +227 -0
  6. data/ext/oj/code.h +40 -0
  7. data/ext/oj/compat.c +126 -38
  8. data/ext/oj/custom.c +1097 -0
  9. data/ext/oj/dump.c +658 -2376
  10. data/ext/oj/dump.h +92 -0
  11. data/ext/oj/dump_compat.c +937 -0
  12. data/ext/oj/dump_leaf.c +254 -0
  13. data/ext/oj/dump_object.c +810 -0
  14. data/ext/oj/dump_rails.c +329 -0
  15. data/ext/oj/dump_strict.c +416 -0
  16. data/ext/oj/err.c +0 -25
  17. data/ext/oj/err.h +8 -2
  18. data/ext/oj/fast.c +24 -24
  19. data/ext/oj/mimic_json.c +817 -0
  20. data/ext/oj/mimic_rails.c +806 -0
  21. data/ext/oj/mimic_rails.h +17 -0
  22. data/ext/oj/object.c +18 -72
  23. data/ext/oj/odd.c +0 -25
  24. data/ext/oj/odd.h +2 -27
  25. data/ext/oj/oj.c +655 -1503
  26. data/ext/oj/oj.h +93 -40
  27. data/ext/oj/parse.c +99 -46
  28. data/ext/oj/parse.h +12 -26
  29. data/ext/oj/reader.c +1 -25
  30. data/ext/oj/reader.h +3 -25
  31. data/ext/oj/resolve.c +9 -11
  32. data/ext/oj/resolve.h +2 -2
  33. data/ext/oj/rxclass.c +133 -0
  34. data/ext/oj/rxclass.h +27 -0
  35. data/ext/oj/saj.c +4 -25
  36. data/ext/oj/scp.c +3 -25
  37. data/ext/oj/sparse.c +89 -13
  38. data/ext/oj/stream_writer.c +301 -0
  39. data/ext/oj/strict.c +4 -27
  40. data/ext/oj/string_writer.c +480 -0
  41. data/ext/oj/val_stack.h +6 -2
  42. data/lib/oj.rb +1 -23
  43. data/lib/oj/easy_hash.rb +12 -4
  44. data/lib/oj/json.rb +172 -0
  45. data/lib/oj/mimic.rb +123 -18
  46. data/lib/oj/state.rb +131 -0
  47. data/lib/oj/version.rb +1 -1
  48. data/pages/Advanced.md +22 -0
  49. data/pages/Compatibility.md +25 -0
  50. data/pages/Custom.md +23 -0
  51. data/pages/Encoding.md +65 -0
  52. data/pages/JsonGem.md +79 -0
  53. data/pages/Modes.md +140 -0
  54. data/pages/Options.md +250 -0
  55. data/pages/Rails.md +60 -0
  56. data/pages/Security.md +20 -0
  57. data/test/activesupport4/decoding_test.rb +105 -0
  58. data/test/activesupport4/encoding_test.rb +531 -0
  59. data/test/activesupport4/test_helper.rb +41 -0
  60. data/test/activesupport5/decoding_test.rb +125 -0
  61. data/test/activesupport5/encoding_test.rb +483 -0
  62. data/test/activesupport5/encoding_test_cases.rb +90 -0
  63. data/test/activesupport5/test_helper.rb +50 -0
  64. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  65. data/test/json_gem/json_addition_test.rb +216 -0
  66. data/test/json_gem/json_common_interface_test.rb +143 -0
  67. data/test/json_gem/json_encoding_test.rb +109 -0
  68. data/test/json_gem/json_ext_parser_test.rb +20 -0
  69. data/test/json_gem/json_fixtures_test.rb +35 -0
  70. data/test/json_gem/json_generator_test.rb +383 -0
  71. data/test/json_gem/json_generic_object_test.rb +90 -0
  72. data/test/json_gem/json_parser_test.rb +470 -0
  73. data/test/json_gem/json_string_matching_test.rb +42 -0
  74. data/test/json_gem/test_helper.rb +18 -0
  75. data/test/perf_compat.rb +30 -28
  76. data/test/perf_object.rb +1 -1
  77. data/test/perf_strict.rb +18 -1
  78. data/test/sample.rb +0 -1
  79. data/test/test_compat.rb +169 -93
  80. data/test/test_custom.rb +355 -0
  81. data/test/test_file.rb +0 -8
  82. data/test/test_null.rb +376 -0
  83. data/test/test_object.rb +268 -3
  84. data/test/test_scp.rb +22 -1
  85. data/test/test_strict.rb +160 -4
  86. data/test/test_various.rb +52 -620
  87. data/test/tests.rb +14 -0
  88. data/test/tests_mimic.rb +14 -0
  89. data/test/tests_mimic_addition.rb +7 -0
  90. metadata +89 -47
  91. data/test/activesupport_datetime_test.rb +0 -23
  92. data/test/bug.rb +0 -51
  93. data/test/bug2.rb +0 -10
  94. data/test/bug3.rb +0 -46
  95. data/test/bug_fast.rb +0 -32
  96. data/test/bug_load.rb +0 -24
  97. data/test/crash.rb +0 -111
  98. data/test/curl/curl_oj.rb +0 -46
  99. data/test/curl/get_oj.rb +0 -24
  100. data/test/curl/just_curl.rb +0 -31
  101. data/test/curl/just_oj.rb +0 -51
  102. data/test/example.rb +0 -11
  103. data/test/foo.rb +0 -24
  104. data/test/io.rb +0 -48
  105. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  106. data/test/mod.rb +0 -16
  107. data/test/rails.rb +0 -50
  108. data/test/russian.rb +0 -18
  109. data/test/struct.rb +0 -29
  110. data/test/test_serializer.rb +0 -59
  111. data/test/write_timebars.rb +0 -31
@@ -132,6 +132,7 @@ class Juice < Minitest::Test
132
132
  :symbol_keys=>true,
133
133
  :bigdecimal_as_decimal=>false,
134
134
  :use_to_json=>false,
135
+ :use_to_hash=>false,
135
136
  :use_as_json=>false,
136
137
  :nilnil=>true,
137
138
  :empty_string=>true,
@@ -151,8 +152,12 @@ class Juice < Minitest::Test
151
152
  :nan=>:huge,
152
153
  :hash_class=>Hash,
153
154
  :omit_nil=>false,
155
+ :allow_nan=>true,
156
+ :array_class=>Array,
154
157
  }
155
158
  Oj.default_options = alt
159
+ #keys = alt.keys
160
+ #Oj.default_options.keys.each { |k| puts k unless keys.include? k}
156
161
  opts = Oj.default_options()
157
162
  assert_equal(alt, opts);
158
163
 
@@ -193,14 +198,16 @@ class Juice < Minitest::Test
193
198
 
194
199
  n = Oj.load('1000.0000123456789')
195
200
  assert_equal(BigDecimal, n.class)
196
- assert_equal('0.10000000123456789E4', n.to_s)
201
+ assert_equal('0.10000000123456789E4', n.to_s.upcase)
197
202
 
198
203
  n = Oj.load('-0.000012345678901234567')
199
204
  assert_equal(BigDecimal, n.class)
200
- assert_equal('-0.12345678901234567E-4', n.to_s)
205
+ assert_equal('-0.12345678901234567E-4', n.to_s.upcase)
201
206
 
202
207
  end
203
208
 
209
+ =begin
210
+ # TBD move to custom
204
211
  def test_float_dump
205
212
  Oj.default_options = { :float_precision => 16 }
206
213
  assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
@@ -227,45 +234,7 @@ class Juice < Minitest::Test
227
234
  assert_equal('80.6', Oj.dump(80.6))
228
235
  assert_equal('-95.640172', Oj.dump(-95.640172))
229
236
  end
230
-
231
- def test_nan_dump
232
- assert_equal('null', Oj.dump(0/0.0, :mode => :strict, :nan => :null))
233
- assert_equal('NaN', Oj.dump(0/0.0, :mode => :strict, :nan => :word))
234
- assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :mode => :strict, :nan => :huge))
235
- end
236
-
237
- def test_infinity_dump
238
- assert_equal('null', Oj.dump(1/0.0, :mode => :strict, :nan => :null))
239
- assert_equal('Infinity', Oj.dump(1/0.0, :mode => :strict, :nan => :word))
240
- assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :mode => :strict, :nan => :huge))
241
- end
242
-
243
- def test_neg_infinity_dump
244
- assert_equal('null', Oj.dump(-1/0.0, :mode => :strict, :nan => :null))
245
- assert_equal('-Infinity', Oj.dump(-1/0.0, :mode => :strict, :nan => :word))
246
- assert_equal('-3.0e14159265358979323846', Oj.dump(-1/0.0, :mode => :strict, :nan => :huge))
247
- end
248
-
249
- def test_float
250
- mode = Oj.default_options()[:mode]
251
- Oj.default_options = {:mode => :object}
252
- dump_and_load(0.0, false)
253
- dump_and_load(12345.6789, false)
254
- dump_and_load(70.35, false)
255
- dump_and_load(-54321.012, false)
256
- dump_and_load(1.7775, false)
257
- dump_and_load(2.5024, false)
258
- dump_and_load(2.48e16, false)
259
- dump_and_load(2.48e100 * 1.0e10, false)
260
- dump_and_load(-2.48e100 * 1.0e10, false)
261
- dump_and_load(1/0.0, false)
262
- # NaN does not always == NaN
263
- json = Oj.dump(0/0.0)
264
- assert_equal('3.3e14159265358979323846', json)
265
- loaded = Oj.load(json);
266
- assert_equal(true, loaded.nan?)
267
- Oj.default_options = {:mode => mode}
268
- end
237
+ =end
269
238
 
270
239
  def test_string
271
240
  dump_and_load('', false)
@@ -275,12 +244,6 @@ class Juice < Minitest::Test
275
244
  assert_equal("a\u0000a", dump_and_load("a\u0000a", false))
276
245
  end
277
246
 
278
- def test_string_object
279
- Oj.default_options = {:mode => :object}
280
- dump_and_load('abc', false)
281
- dump_and_load(':abc', false)
282
- end
283
-
284
247
  def test_encode
285
248
  opts = Oj.default_options
286
249
  Oj.default_options = { :ascii_only => false }
@@ -375,8 +338,8 @@ class Juice < Minitest::Test
375
338
  {"b":2}
376
339
  }
377
340
  results = []
378
- Oj.load(json) { |x| results << x }
379
- assert_equal([{"a"=>1}, [1,2], [3,4], {"b"=>2}], results)
341
+ Oj.load(json, :mode => :strict) { |x, start, len| results << [x, start, len] }
342
+ assert_equal([[{"a"=>1}, 0, 7], [[1,2], 7, 6], [[3,4], 13, 5], [{"b"=>2}, 18, 8]], results)
380
343
  end
381
344
 
382
345
  def test_multiple_json_no_callback
@@ -433,86 +396,60 @@ class Juice < Minitest::Test
433
396
  end
434
397
 
435
398
  # Symbol
436
- def test_symbol_strict
437
- begin
438
- Oj.dump(:abc, :mode => :strict)
439
- rescue Exception
440
- assert(true)
441
- return
442
- end
443
- assert(false, "*** expected an exception")
444
- end
445
399
  def test_symbol_null
446
400
  json = Oj.dump(:abc, :mode => :null)
447
- assert_equal('null', json)
448
- end
449
- def test_symbol_compat
450
- json = Oj.dump(:abc, :mode => :compat)
451
401
  assert_equal('"abc"', json)
452
402
  end
453
- def test_symbol_object
454
- Oj.default_options = { :mode => :object }
455
- #dump_and_load(''.to_sym, false)
456
- dump_and_load(:abc, false)
457
- dump_and_load(':xyz'.to_sym, false)
458
- end
459
403
 
460
404
  # Time
461
- def test_time_strict
462
- t = Time.local(2012, 1, 5, 23, 58, 7)
463
- begin
464
- Oj.dump(t, :mode => :strict)
465
- rescue Exception
466
- assert(true)
467
- return
468
- end
469
- assert(false, "*** expected an exception")
470
- end
471
405
  def test_time_null
472
406
  t = Time.local(2012, 1, 5, 23, 58, 7)
473
407
  json = Oj.dump(t, :mode => :null)
474
408
  assert_equal('null', json)
475
409
  end
476
- def test_unix_time_compat
410
+
411
+ =begin
412
+ # TBD make thse tests for cusom mode
413
+ def test_unix_time_custom
477
414
  t = Time.xmlschema("2012-01-05T23:58:07.123456000+09:00")
478
415
  #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
479
- json = Oj.dump(t, :mode => :compat, :time_format => :unix)
416
+ json = Oj.dump(t, :mode => :custom, :time_format => :unix)
480
417
  assert_equal(%{1325775487.123456000}, json)
481
418
  end
482
- def test_unix_time_compat_precision
419
+ def test_unix_time_custom_precision
483
420
  t = Time.xmlschema("2012-01-05T23:58:07.123456789+09:00")
484
421
  #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
485
- json = Oj.dump(t, :mode => :compat, :second_precision => 5, :time_format => :unix)
422
+ json = Oj.dump(t, :mode => :custom, :second_precision => 5, :time_format => :unix)
486
423
  assert_equal(%{1325775487.12346}, json)
487
424
  t = Time.xmlschema("2012-01-05T23:58:07.999600+09:00")
488
- json = Oj.dump(t, :mode => :compat, :second_precision => 3, :time_format => :unix)
425
+ json = Oj.dump(t, :mode => :custom, :second_precision => 3, :time_format => :unix)
489
426
  assert_equal(%{1325775488.000}, json)
490
427
  end
491
- def test_unix_time_compat_early
428
+ def test_unix_time_custom_early
492
429
  t = Time.xmlschema("1954-01-05T00:00:00.123456789+00:00")
493
- json = Oj.dump(t, :mode => :compat, :second_precision => 5, :time_format => :unix)
430
+ json = Oj.dump(t, :mode => :custom, :second_precision => 5, :time_format => :unix)
494
431
  assert_equal(%{-504575999.87654}, json)
495
432
  end
496
- def test_unix_time_compat_1970
433
+ def test_unix_time_custom_1970
497
434
  t = Time.xmlschema("1970-01-01T00:00:00.123456789+00:00")
498
- json = Oj.dump(t, :mode => :compat, :second_precision => 5, :time_format => :unix)
435
+ json = Oj.dump(t, :mode => :custom, :second_precision => 5, :time_format => :unix)
499
436
  assert_equal(%{0.12346}, json)
500
437
  end
501
- def test_ruby_time_compat
438
+ def test_ruby_time_custom
502
439
  t = Time.xmlschema("2012-01-05T23:58:07.123456000+09:00")
503
- json = Oj.dump(t, :mode => :compat, :time_format => :ruby)
440
+ json = Oj.dump(t, :mode => :custom, :time_format => :ruby)
504
441
  #assert_equal(%{"2012-01-05 23:58:07 +0900"}, json)
505
442
  assert_equal(%{"#{t.to_s}"}, json)
506
443
  end
507
- def test_xml_time_compat
444
+ def test_xml_time_custom
508
445
  begin
509
446
  t = Time.new(2012, 1, 5, 23, 58, 7.123456000, 34200)
510
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
447
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema)
511
448
  assert_equal(%{"2012-01-05T23:58:07.123456000+09:30"}, json)
512
449
  rescue Exception
513
450
  # some Rubies (1.8.7) do not allow the timezome to be set
514
451
  t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
515
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
452
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema)
516
453
  tz = t.utc_offset
517
454
  # Ruby does not handle a %+02d properly so...
518
455
  sign = '+'
@@ -523,15 +460,15 @@ class Juice < Minitest::Test
523
460
  assert_equal(%{"2012-01-05T23:58:07.123456000%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
524
461
  end
525
462
  end
526
- def test_xml_time_compat_no_secs
463
+ def test_xml_time_custom_no_secs
527
464
  begin
528
465
  t = Time.new(2012, 1, 5, 23, 58, 7.0, 34200)
529
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
466
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema)
530
467
  assert_equal(%{"2012-01-05T23:58:07+09:30"}, json)
531
468
  rescue Exception
532
469
  # some Rubies (1.8.7) do not allow the timezome to be set
533
470
  t = Time.local(2012, 1, 5, 23, 58, 7, 0)
534
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
471
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema)
535
472
  tz = t.utc_offset
536
473
  # Ruby does not handle a %+02d properly so...
537
474
  sign = '+'
@@ -542,15 +479,15 @@ class Juice < Minitest::Test
542
479
  assert_equal(%{"2012-01-05T23:58:07%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
543
480
  end
544
481
  end
545
- def test_xml_time_compat_precision
482
+ def test_xml_time_custom_precision
546
483
  begin
547
484
  t = Time.new(2012, 1, 5, 23, 58, 7.123456789, 32400)
548
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 5)
485
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema, :second_precision => 5)
549
486
  assert_equal(%{"2012-01-05T23:58:07.12346+09:00"}, json)
550
487
  rescue Exception
551
488
  # some Rubies (1.8.7) do not allow the timezome to be set
552
489
  t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
553
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 5)
490
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema, :second_precision => 5)
554
491
  tz = t.utc_offset
555
492
  # Ruby does not handle a %+02d properly so...
556
493
  sign = '+'
@@ -561,15 +498,15 @@ class Juice < Minitest::Test
561
498
  assert_equal(%{"2012-01-05T23:58:07.12346%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
562
499
  end
563
500
  end
564
- def test_xml_time_compat_precision_round
501
+ def test_xml_time_custom_precision_round
565
502
  begin
566
- t = Time.new(2012, 1, 5, 23, 58, 7.9996, 32400)
567
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 3)
503
+ t = Time.new(2012, 1, 5, 23, 58, 7.99996, 32400)
504
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema, :second_precision => 4)
568
505
  assert_equal(%{"2012-01-05T23:58:08+09:00"}, json)
569
506
  rescue Exception
570
507
  # some Rubies (1.8.7) do not allow the timezome to be set
571
508
  t = Time.local(2012, 1, 5, 23, 58, 7, 999600)
572
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 3)
509
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema, :second_precision => 3)
573
510
  tz = t.utc_offset
574
511
  # Ruby does not handle a %+02d properly so...
575
512
  sign = '+'
@@ -580,82 +517,34 @@ class Juice < Minitest::Test
580
517
  assert_equal(%{"2012-01-05T23:58:08%s%02d:%02d"} % [sign, tz / 3600, tz / 60 % 60], json)
581
518
  end
582
519
  end
583
- def test_xml_time_compat_zulu
520
+ def test_xml_time_custom_zulu
584
521
  begin
585
522
  t = Time.new(2012, 1, 5, 23, 58, 7.0, 0)
586
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
523
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema)
587
524
  assert_equal(%{"2012-01-05T23:58:07Z"}, json)
588
525
  rescue Exception
589
526
  # some Rubies (1.8.7) do not allow the timezome to be set
590
527
  t = Time.utc(2012, 1, 5, 23, 58, 7, 0)
591
- json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema)
528
+ json = Oj.dump(t, :mode => :custom, :time_format => :xmlschema)
592
529
  #tz = t.utc_offset
593
530
  assert_equal(%{"2012-01-05T23:58:07Z"}, json)
594
531
  end
595
532
  end
533
+ =end
596
534
 
597
535
  # Class
598
- def test_class_strict
599
- begin
600
- Oj.dump(Juice, :mode => :strict)
601
- rescue Exception
602
- assert(true)
603
- return
604
- end
605
- assert(false, "*** expected an exception")
606
- end
607
536
  def test_class_null
608
537
  json = Oj.dump(Juice, :mode => :null)
609
538
  assert_equal('null', json)
610
539
  end
611
- def test_class_compat
612
- json = Oj.dump(Juice, :mode => :compat)
613
- assert_equal(%{"Juice"}, json)
614
- end
615
- def test_class_object
616
- Oj.default_options = { :mode => :object }
617
- dump_and_load(Juice, false)
618
- end
619
540
 
620
541
  # Module
621
- def test_module_strict
622
- begin
623
- Oj.dump(TestModule, :mode => :strict)
624
- rescue Exception
625
- assert(true)
626
- return
627
- end
628
- assert(false, "*** expected an exception")
629
- end
630
542
  def test_module_null
631
543
  json = Oj.dump(TestModule, :mode => :null)
632
544
  assert_equal('null', json)
633
545
  end
634
- def test_module_compat
635
- json = Oj.dump(TestModule, :mode => :compat)
636
- assert_equal(%{"Juice::TestModule"}, json)
637
- end
638
- def test_module_object
639
- Oj.default_options = { :mode => :object }
640
- dump_and_load(TestModule, false)
641
- end
642
546
 
643
547
  # Hash
644
- def test_hash
645
- Oj.default_options = { :mode => :strict }
646
- dump_and_load({}, false)
647
- dump_and_load({ 'true' => true, 'false' => false}, false)
648
- dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
649
- end
650
- def test_non_str_hash_strict
651
- begin
652
- Oj.dump({ 1 => true, 0 => false }, :mode => :strict)
653
- rescue Exception
654
- assert(true)
655
- return
656
- end
657
- assert(false, "*** expected an exception")
658
- end
659
548
  def test_non_str_hash_null
660
549
  begin
661
550
  Oj.dump({ 1 => true, 0 => false }, :mode => :null)
@@ -665,27 +554,7 @@ class Juice < Minitest::Test
665
554
  end
666
555
  assert(false, "*** expected an exception")
667
556
  end
668
- def test_non_str_hash_compat
669
- json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
670
- h = Oj.load(json, :mode => :strict)
671
- assert_equal({ "1" => true, "0" => false }, h)
672
- end
673
- def test_non_str_hash_object
674
- Oj.default_options = { :mode => :object }
675
- json = Oj.dump({ 1 => true, :sim => nil })
676
- h = Oj.load(json, :mode => :strict)
677
- assert_equal({"^#1" => [1, true], ":sim" => nil}, h)
678
- h = Oj.load(json)
679
- assert_equal({ 1 => true, :sim => nil }, h)
680
- end
681
- def test_mixed_hash_object
682
- Oj.default_options = { :mode => :object }
683
- json = Oj.dump({ 1 => true, 'nil' => nil, :sim => 4 })
684
- h = Oj.load(json, :mode => :strict)
685
- assert_equal({"^#1" => [1, true], "nil" => nil, ":sim" => 4}, h)
686
- h = Oj.load(json)
687
- assert_equal({ 1 => true, 'nil' => nil, :sim => 4 }, h)
688
- end
557
+
689
558
  def test_hash_not_closed
690
559
  begin
691
560
  Oj.load('{')
@@ -697,297 +566,44 @@ class Juice < Minitest::Test
697
566
  end
698
567
 
699
568
  # Object with to_json()
700
- def test_json_object_strict
701
- obj = Jeez.new(true, 58)
702
- begin
703
- Oj.dump(obj, :mode => :strict)
704
- rescue Exception
705
- assert(true)
706
- return
707
- end
708
- assert(false, "*** expected an exception")
709
- end
710
569
  def test_json_object_null
711
570
  obj = Jeez.new(true, 58)
712
571
  json = Oj.dump(obj, :mode => :null)
713
572
  assert_equal('null', json)
714
573
  end
715
- def test_json_object_compat
716
- Oj.default_options = { :mode => :compat, :use_to_json => true }
717
- obj = Jeez.new(true, 58)
718
- json = Oj.dump(obj, :indent => 2)
719
- assert(%{{"json_class":"Juice::Jeez","x":true,"y":58}
720
- } == json ||
721
- %{{"json_class":"Juice::Jeez","y":58,"x":true}
722
- } == json)
723
- dump_and_load(obj, false)
724
- Oj.default_options = { :mode => :compat, :use_to_json => false }
725
- end
726
- def test_json_object_create_id
727
- Oj.default_options = { :mode => :compat, :create_id => 'kson_class' }
728
- expected = Jeez.new(true, 58)
729
- json = %{{"kson_class":"Juice::Jeez","x":true,"y":58}}
730
- obj = Oj.load(json)
731
- assert_equal(expected, obj)
732
- Oj.default_options = { :create_id => 'json_class' }
733
- end
734
- def test_json_object_object
735
- obj = Jeez.new(true, 58)
736
- json = Oj.dump(obj, :mode => :object, :indent => 2)
737
- assert(%{{
738
- "^o":"Juice::Jeez",
739
- "x":true,
740
- "y":58
741
- }
742
- } == json ||
743
- %{{
744
- "^o":"Juice::Jeez",
745
- "y":58,
746
- "x":true
747
- }
748
- } == json)
749
- obj2 = Oj.load(json, :mode => :object)
750
- assert_equal(obj, obj2)
751
- end
752
574
 
753
575
  # Object with to_hash()
754
- def test_to_hash_object_strict
755
- obj = Jazz.new(true, 58)
756
- begin
757
- Oj.dump(obj, :mode => :strict)
758
- rescue Exception
759
- assert(true)
760
- return
761
- end
762
- assert(false, "*** expected an exception")
763
- end
764
576
  def test_to_hash_object_null
765
577
  obj = Jazz.new(true, 58)
766
578
  json = Oj.dump(obj, :mode => :null)
767
579
  assert_equal('null', json)
768
580
  end
769
- def test_to_hash_object_compat
770
- Oj.default_options = { :use_to_json => true }
771
- obj = Jazz.new(true, 58)
772
- json = Oj.dump(obj, :mode => :compat, :indent => 2)
773
- h = Oj.load(json, :mode => :strict)
774
- assert_equal(obj.to_hash, h)
775
- end
776
- def test_to_hash_object_object
777
- obj = Jazz.new(true, 58)
778
- json = Oj.dump(obj, :mode => :object, :indent => 2)
779
- assert(%{{
780
- "^o":"Juice::Jazz",
781
- "x":true,
782
- "y":58
783
- }
784
- } == json ||
785
- %{{
786
- "^o":"Juice::Jazz",
787
- "y":58,
788
- "x":true
789
- }
790
- } == json)
791
- obj2 = Oj.load(json, :mode => :object)
792
- assert_equal(obj, obj2)
793
- end
794
581
 
795
582
  # Object with as_json() # contributed by sauliusg
796
- def test_as_json_object_strict
797
- obj = Orange.new(true, 58)
798
- begin
799
- Oj.dump(obj, :mode => :strict)
800
- rescue Exception
801
- assert(true)
802
- return
803
- end
804
- assert(false, "*** expected an exception")
805
- end
806
-
807
583
  def test_as_json_object_null
808
584
  obj = Orange.new(true, 58)
809
585
  json = Oj.dump(obj, :mode => :null)
810
586
  assert_equal('null', json)
811
587
  end
812
588
 
813
- def test_as_json_object_compat_hash
814
- Oj.default_options = { :mode => :compat, :use_as_json => true }
815
- obj = Orange.new(true, 58)
816
- json = Oj.dump(obj, :indent => 2)
817
- assert(!json.nil?)
818
- dump_and_load(obj, false)
819
- end
820
-
821
- def test_as_json_object_compat_non_hash
822
- Oj.default_options = { :mode => :compat, :use_as_json => true }
823
- obj = Melon.new(true, 58)
824
- json = Oj.dump(obj, :indent => 2)
825
- assert_equal(%{"true 58"}, json)
826
- end
827
-
828
- def test_as_json_object_object
829
- obj = Orange.new(true, 58)
830
- json = Oj.dump(obj, :mode => :object, :indent => 2)
831
- assert(%{{
832
- "^o":"Juice::Orange",
833
- "x":true,
834
- "y":58
835
- }
836
- } == json ||
837
- %{{
838
- "^o":"Juice::Orange",
839
- "y":58,
840
- "x":true
841
- }
842
- } == json)
843
- obj2 = Oj.load(json, :mode => :object)
844
- assert_equal(obj, obj2)
845
- end
846
-
847
589
  # Object without to_json() or to_hash()
848
- def test_object_strict
849
- obj = Jam.new(true, 58)
850
- begin
851
- Oj.dump(obj, :mode => :strict)
852
- rescue Exception
853
- assert(true)
854
- return
855
- end
856
- assert(false, "*** expected an exception")
857
- end
858
590
  def test_object_null
859
591
  obj = Jam.new(true, 58)
860
592
  json = Oj.dump(obj, :mode => :null)
861
593
  assert_equal('null', json)
862
594
  end
863
- def test_object_compat
864
- obj = Jam.new(true, 58)
865
- json = Oj.dump(obj, :mode => :compat, :indent => 2)
866
- assert(%{{
867
- "x":true,
868
- "y":58
869
- }
870
- } == json ||
871
- %{{
872
- "y":58,
873
- "x":true
874
- }
875
- } == json)
876
- end
877
- def test_object_object
878
- obj = Jam.new(true, 58)
879
- json = Oj.dump(obj, :mode => :object, :indent => 2)
880
- assert(%{{
881
- "^o":"Juice::Jam",
882
- "x":true,
883
- "y":58
884
- }
885
- } == json ||
886
- %{{
887
- "^o":"Juice::Jam",
888
- "y":58,
889
- "x":true
890
- }
891
- } == json)
892
- obj2 = Oj.load(json, :mode => :object)
893
- assert_equal(obj, obj2)
894
- end
895
-
896
- def test_object_object_no_cache
897
- obj = Jam.new(true, 58)
898
- json = Oj.dump(obj, :mode => :object, :indent => 2)
899
- assert(%{{
900
- "^o":"Juice::Jam",
901
- "x":true,
902
- "y":58
903
- }
904
- } == json ||
905
- %{{
906
- "^o":"Juice::Jam",
907
- "y":58,
908
- "x":true
909
- }
910
- } == json)
911
- obj2 = Oj.load(json, :mode => :object, :class_cache => false)
912
- assert_equal(obj, obj2)
913
- end
914
-
915
- # Exception
916
- def test_exception
917
- err = nil
918
- begin
919
- raise StandardError.new('A Message')
920
- rescue Exception => e
921
- err = e
922
- end
923
- json = Oj.dump(err, :mode => :object, :indent => 2)
924
- #puts "*** #{json}"
925
- e2 = Oj.load(json, :mode => :strict)
926
- assert_equal(err.class.to_s, e2['^o'])
927
- assert_equal(err.message, e2['~mesg'])
928
- assert_equal(err.backtrace, e2['~bt'])
929
- e2 = Oj.load(json, :mode => :object)
930
- if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
931
- assert_equal(e.class, e2.class);
932
- assert_equal(e.message, e2.message);
933
- assert_equal(e.backtrace, e2.backtrace);
934
- else
935
- assert_equal(e, e2);
936
- end
937
- end
938
595
 
939
596
  # Range
940
- def test_range_strict
941
- begin
942
- Oj.dump(1..7, :mode => :strict)
943
- rescue Exception
944
- assert(true)
945
- return
946
- end
947
- assert(false, "*** expected an exception")
948
- end
949
597
  def test_range_null
950
598
  json = Oj.dump(1..7, :mode => :null)
951
599
  assert_equal('null', json)
952
600
  end
953
- def test_range_compat
954
- Oj.default_options = { :use_to_json => true }
955
- json = Oj.dump(1..7, :mode => :compat)
956
- h = Oj.load(json, :mode => :strict)
957
- assert_equal({'begin' => 1, 'end' => 7, 'exclude_end' => false}, h)
958
- json = Oj.dump(1...7, :mode => :compat)
959
- h = Oj.load(json, :mode => :strict)
960
- assert_equal({'begin' => 1, 'end' => 7, 'exclude_end' => true}, h)
961
- end
962
- def test_range_object
963
- unless RUBY_VERSION.start_with?('1.8')
964
- Oj.default_options = { :mode => :object }
965
- json = Oj.dump(1..7, :mode => :object, :indent => 0)
966
- if 'rubinius' == $ruby
967
- assert(%{{"^O":"Range","begin":1,"end":7,"exclude_end?":false}} == json)
968
- else
969
- assert_equal(%{{"^u":["Range",1,7,false]}}, json)
970
- end
971
- dump_and_load(1..7, false)
972
- dump_and_load(1..1, false)
973
- dump_and_load(1...7, false)
974
- end
975
- end
976
601
 
977
602
  # BigNum
978
- def test_bignum_strict
979
- json = Oj.dump(7 ** 55, :mode => :strict)
980
- assert_equal('30226801971775055948247051683954096612865741943', json)
981
- end
982
603
  def test_bignum_null
983
604
  json = Oj.dump(7 ** 55, :mode => :null)
984
605
  assert_equal('30226801971775055948247051683954096612865741943', json)
985
606
  end
986
- def test_bignum_compat
987
- json = Oj.dump(7 ** 55, :mode => :compat)
988
- b = Oj.load(json, :mode => :strict)
989
- assert_equal(30226801971775055948247051683954096612865741943, b)
990
- end
991
607
 
992
608
  def test_bignum_object
993
609
  dump_and_load(7 ** 55, false)
@@ -995,136 +611,31 @@ class Juice < Minitest::Test
995
611
  end
996
612
 
997
613
  # BigDecimal
998
- def test_bigdecimal_strict
999
- mode = Oj.default_options[:mode]
1000
- Oj.default_options = {:mode => :strict}
1001
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
1002
- Oj.default_options = {:mode => mode}
1003
- end
1004
614
  def test_bigdecimal_null
1005
615
  mode = Oj.default_options[:mode]
1006
616
  Oj.default_options = {:mode => :null}
1007
617
  dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
1008
618
  Oj.default_options = {:mode => mode}
1009
619
  end
1010
- def test_bigdecimal_compat
1011
- orig = BigDecimal.new('80.51')
1012
- json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => false)
1013
- bg = Oj.load(json, :mode => :compat)
1014
- assert_equal(orig.to_s, bg)
1015
- orig = BigDecimal.new('3.14159265358979323846')
1016
- json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => false)
1017
- bg = Oj.load(json, :mode => :compat)
1018
- assert_equal(orig.to_s, bg)
1019
- end
1020
- def test_bigdecimal_load
1021
- orig = BigDecimal.new('80.51')
1022
- json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => true)
1023
- bg = Oj.load(json, :mode => :compat, :bigdecimal_load => true)
1024
- assert_equal(BigDecimal, bg.class)
1025
- assert_equal(orig, bg)
1026
- end
1027
- def test_float_load
1028
- orig = BigDecimal.new('80.51')
1029
- json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => true)
1030
- bg = Oj.load(json, :mode => :compat, :bigdecimal_load => :float)
1031
- assert_equal(Float, bg.class)
1032
- assert_equal(orig.to_f, bg)
1033
- end
1034
- def test_bigdecimal_compat_as_json
1035
- Oj.default_options = { :use_as_json => true }
1036
- orig = BigDecimal.new('80.51')
1037
- BigDecimal.send(:define_method, :as_json) do
1038
- %{this is big}
1039
- end
1040
- json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => false)
1041
- bg = Oj.load(json, :mode => :compat)
1042
- assert_equal("this is big", bg)
1043
- BigDecimal.send(:remove_method, :as_json) # cleanup
1044
- end
1045
- def test_bigdecimal_object
1046
- mode = Oj.default_options[:mode]
1047
- Oj.default_options = {:mode => :object}
1048
- dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
1049
- Oj.default_options = {:mode => mode}
1050
- # Infinity is the same for Float and BigDecimal
1051
- json = Oj.dump(BigDecimal.new('Infinity'), :mode => :object)
1052
- assert_equal('Infinity', json)
1053
- json = Oj.dump(BigDecimal.new('-Infinity'), :mode => :object)
1054
- assert_equal('-Infinity', json)
1055
- end
1056
620
 
1057
621
  def test_infinity
1058
622
  n = Oj.load('Infinity', :mode => :object)
1059
623
  assert_equal(BigDecimal.new('Infinity').to_f, n);
1060
- begin
1061
- Oj.load('Infinity', :mode => :strict)
1062
- fail()
1063
- rescue Oj::ParseError
1064
- assert(true)
1065
- end
1066
624
  x = Oj.load('Infinity', :mode => :compat)
1067
625
  assert_equal('Infinity', x.to_s)
1068
-
1069
626
  end
1070
627
 
1071
628
  # Date
1072
- def test_date_strict
1073
- begin
1074
- Oj.dump(Date.new(2012, 6, 19), :mode => :strict)
1075
- rescue Exception
1076
- assert(true)
1077
- return
1078
- end
1079
- assert(false, "*** expected an exception")
1080
- end
1081
629
  def test_date_null
1082
630
  json = Oj.dump(Date.new(2012, 6, 19), :mode => :null)
1083
631
  assert_equal('null', json)
1084
632
  end
1085
- def test_date_compat
1086
- orig = Date.new(2012, 6, 19)
1087
- json = Oj.dump(orig, :mode => :compat)
1088
- x = Oj.load(json, :mode => :compat)
1089
- # Some Rubies implement Date as data and some as a real Object. Either are
1090
- # okay for the test.
1091
- if x.is_a?(String)
1092
- assert_equal(orig.to_s, x)
1093
- else # better be a Hash
1094
- assert_equal({"year" => orig.year, "month" => orig.month, "day" => orig.day, "start" => orig.start}, x)
1095
- end
1096
- end
1097
- def test_date_object
1098
- Oj.default_options = {:mode => :object}
1099
- dump_and_load(Date.new(2012, 6, 19), false)
1100
- end
1101
633
 
1102
634
  # DateTime
1103
- def test_datetime_strict
1104
- begin
1105
- Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :strict)
1106
- rescue Exception
1107
- assert(true)
1108
- return
1109
- end
1110
- assert(false, "*** expected an exception")
1111
- end
1112
635
  def test_datetime_null
1113
636
  json = Oj.dump(DateTime.new(2012, 6, 19, 20, 19, 27), :mode => :null)
1114
637
  assert_equal('null', json)
1115
638
  end
1116
- def test_datetime_compat
1117
- orig = DateTime.new(2012, 6, 19, 20, 19, 27)
1118
- json = Oj.dump(orig, :mode => :compat)
1119
- x = Oj.load(json, :mode => :compat)
1120
- # Some Rubies implement Date as data and some as a real Object. Either are
1121
- # okay for the test.
1122
- assert_equal(orig.to_s, x)
1123
- end
1124
- def test_datetime_object
1125
- Oj.default_options = {:mode => :object}
1126
- dump_and_load(DateTime.new(2012, 6, 19), false)
1127
- end
1128
639
 
1129
640
  # autodefine Oj::Bag
1130
641
  def test_bag
@@ -1138,75 +649,7 @@ class Juice < Minitest::Test
1138
649
  assert_equal(58, obj.y)
1139
650
  end
1140
651
 
1141
- # Circular
1142
- def test_circular_object
1143
- obj = Jam.new(nil, 58)
1144
- obj.x = obj
1145
- json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
1146
- assert(%{{
1147
- "^o":"Juice::Jam",
1148
- "^i":1,
1149
- "x":"^r1",
1150
- "y":58
1151
- }
1152
- } == json ||
1153
- %{{
1154
- "^o":"Juice::Jam",
1155
- "^i":1,
1156
- "y":58,
1157
- "x":"^r1"
1158
- }
1159
- } == json)
1160
- obj2 = Oj.load(json, :mode => :object, :circular => true)
1161
- assert_equal(obj2.x.__id__, obj2.__id__)
1162
- end
1163
-
1164
- def test_circular_hash
1165
- h = { 'a' => 7 }
1166
- h['b'] = h
1167
- json = Oj.dump(h, :mode => :object, :indent => 2, :circular => true)
1168
- ha = Oj.load(json, :mode => :strict)
1169
- assert_equal({'^i' => 1, 'a' => 7, 'b' => '^r1'}, ha)
1170
- Oj.load(json, :mode => :object, :circular => true)
1171
- assert_equal(h['b'].__id__, h.__id__)
1172
- end
1173
-
1174
- def test_circular_array
1175
- a = [7]
1176
- a << a
1177
- json = Oj.dump(a, :mode => :object, :indent => 2, :circular => true)
1178
- assert_equal(%{[
1179
- "^i1",
1180
- 7,
1181
- "^r1"
1182
- ]
1183
- }, json)
1184
- a2 = Oj.load(json, :mode => :object, :circular => true)
1185
- assert_equal(a2[1].__id__, a2.__id__)
1186
- end
1187
-
1188
- def test_circular
1189
- h = { 'a' => 7 }
1190
- obj = Jam.new(h, 58)
1191
- obj.x['b'] = obj
1192
- json = Oj.dump(obj, :mode => :object, :indent => 2, :circular => true)
1193
- ha = Oj.load(json, :mode => :strict)
1194
- assert_equal({'^o' => 'Juice::Jam', '^i' => 1, 'x' => { '^i' => 2, 'a' => 7, 'b' => '^r1' }, 'y' => 58 }, ha)
1195
- Oj.load(json, :mode => :object, :circular => true)
1196
- assert_equal(obj.x.__id__, h.__id__)
1197
- assert_equal(h['b'].__id__, obj.__id__)
1198
- end
1199
-
1200
652
  # Stream Deeply Nested
1201
- def test_deep_nest
1202
- begin
1203
- n = 10000
1204
- Oj.strict_load('[' * n + ']' * n)
1205
- rescue Exception => e
1206
- assert(false, e.message)
1207
- end
1208
- end
1209
-
1210
653
  def test_deep_nest_dump
1211
654
  begin
1212
655
  a = []
@@ -1242,18 +685,6 @@ class Juice < Minitest::Test
1242
685
  assert_equal(src, obj)
1243
686
  end
1244
687
 
1245
- # symbol_keys option
1246
- def test_symbol_keys
1247
- json = %{{
1248
- "x":true,
1249
- "y":58,
1250
- "z": [1,2,3]
1251
- }
1252
- }
1253
- obj = Oj.load(json, :mode => :strict, :symbol_keys => true)
1254
- assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
1255
- end
1256
-
1257
688
  # comments
1258
689
  def test_comment_slash
1259
690
  json = %{{
@@ -1293,7 +724,7 @@ class Juice < Minitest::Test
1293
724
 
1294
725
  def test_nilnil_false
1295
726
  begin
1296
- Oj.load(nil)
727
+ Oj.load(nil, :nilnil => false)
1297
728
  rescue Exception
1298
729
  assert(true)
1299
730
  return
@@ -1303,7 +734,7 @@ class Juice < Minitest::Test
1303
734
 
1304
735
  def test_nilnil_true
1305
736
  obj = Oj.load(nil, :nilnil => true)
1306
- assert_equal(nil, obj)
737
+ assert_nil(obj)
1307
738
  end
1308
739
 
1309
740
  def test_empty_string_true
@@ -1323,7 +754,7 @@ class Juice < Minitest::Test
1323
754
 
1324
755
  def test_quirks_null_mode
1325
756
  assert_raises(Oj::ParseError) { Oj.load("null", :quirks_mode => false) }
1326
- assert_equal(nil, Oj.load("null", :quirks_mode => true))
757
+ assert_nil(Oj.load("null", :quirks_mode => true))
1327
758
  end
1328
759
 
1329
760
  def test_quirks_bool_mode
@@ -1362,9 +793,6 @@ class Juice < Minitest::Test
1362
793
  json = Oj.dump(jam, :omit_nil => true, :mode => :object)
1363
794
  assert_equal(%|{"^o":"Juice::Jam","x":{"a":1}}|, json)
1364
795
 
1365
- json = Oj.dump(jam, :omit_nil => true, :mode => :compat)
1366
- assert_equal(%|{"x":{"a":1}}|, json)
1367
-
1368
796
  json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true, :mode => :strict)
1369
797
  assert_equal(%|{"x":{"a":1}}|, json)
1370
798
 
@@ -1376,7 +804,11 @@ class Juice < Minitest::Test
1376
804
  json = Oj.dump(obj, :indent => 2)
1377
805
  puts json if trace
1378
806
  loaded = Oj.load(json)
1379
- assert_equal(obj, loaded)
807
+ if obj.nil?
808
+ assert_nil(loaded)
809
+ else
810
+ assert_equal(obj, loaded)
811
+ end
1380
812
  loaded
1381
813
  end
1382
814