oj 3.9.1 → 3.16.11

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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +21 -6
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +50 -68
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +60 -62
  9. data/ext/oj/cache8.h +9 -36
  10. data/ext/oj/circarray.c +38 -42
  11. data/ext/oj/circarray.h +12 -13
  12. data/ext/oj/code.c +158 -179
  13. data/ext/oj/code.h +20 -22
  14. data/ext/oj/compat.c +145 -205
  15. data/ext/oj/custom.c +740 -880
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1145 -844
  18. data/ext/oj/dump.h +71 -57
  19. data/ext/oj/dump_compat.c +575 -655
  20. data/ext/oj/dump_leaf.c +96 -186
  21. data/ext/oj/dump_object.c +533 -660
  22. data/ext/oj/dump_strict.c +306 -340
  23. data/ext/oj/encode.h +4 -33
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -28
  26. data/ext/oj/err.h +39 -42
  27. data/ext/oj/extconf.rb +28 -7
  28. data/ext/oj/fast.c +1052 -1113
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +471 -430
  34. data/ext/oj/object.c +532 -580
  35. data/ext/oj/odd.c +156 -142
  36. data/ext/oj/odd.h +25 -26
  37. data/ext/oj/oj.c +1346 -961
  38. data/ext/oj/oj.h +307 -290
  39. data/ext/oj/parse.c +954 -858
  40. data/ext/oj/parse.h +74 -72
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +819 -836
  44. data/ext/oj/rails.h +8 -11
  45. data/ext/oj/reader.c +136 -147
  46. data/ext/oj/reader.h +69 -83
  47. data/ext/oj/resolve.c +41 -63
  48. data/ext/oj/resolve.h +4 -6
  49. data/ext/oj/rxclass.c +69 -72
  50. data/ext/oj/rxclass.h +12 -13
  51. data/ext/oj/saj.c +440 -485
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +79 -118
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +739 -709
  57. data/ext/oj/stream_writer.c +141 -175
  58. data/ext/oj/strict.c +103 -128
  59. data/ext/oj/string_writer.c +244 -261
  60. data/ext/oj/trace.c +34 -41
  61. data/ext/oj/trace.h +42 -15
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +107 -107
  65. data/ext/oj/util.h +4 -3
  66. data/ext/oj/val_stack.c +61 -78
  67. data/ext/oj/val_stack.h +80 -114
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +316 -361
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +9 -9
  73. data/lib/oj/error.rb +1 -2
  74. data/lib/oj/json.rb +162 -150
  75. data/lib/oj/mimic.rb +54 -20
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +12 -8
  79. data/lib/oj/version.rb +1 -2
  80. data/lib/oj.rb +2 -8
  81. data/pages/Compatibility.md +1 -1
  82. data/pages/Encoding.md +1 -1
  83. data/pages/InstallOptions.md +20 -0
  84. data/pages/JsonGem.md +15 -0
  85. data/pages/Modes.md +9 -3
  86. data/pages/Options.md +62 -12
  87. data/pages/Parser.md +309 -0
  88. data/pages/Rails.md +73 -22
  89. metadata +68 -192
  90. data/ext/oj/hash.c +0 -163
  91. data/ext/oj/hash.h +0 -46
  92. data/ext/oj/hash_test.c +0 -512
  93. data/test/_test_active.rb +0 -76
  94. data/test/_test_active_mimic.rb +0 -96
  95. data/test/_test_mimic_rails.rb +0 -126
  96. data/test/activerecord/result_test.rb +0 -27
  97. data/test/activesupport4/decoding_test.rb +0 -108
  98. data/test/activesupport4/encoding_test.rb +0 -531
  99. data/test/activesupport4/test_helper.rb +0 -41
  100. data/test/activesupport5/decoding_test.rb +0 -125
  101. data/test/activesupport5/encoding_test.rb +0 -485
  102. data/test/activesupport5/encoding_test_cases.rb +0 -90
  103. data/test/activesupport5/test_helper.rb +0 -50
  104. data/test/activesupport5/time_zone_test_helpers.rb +0 -24
  105. data/test/bar.rb +0 -25
  106. data/test/files.rb +0 -29
  107. data/test/foo.rb +0 -21
  108. data/test/helper.rb +0 -26
  109. data/test/isolated/shared.rb +0 -308
  110. data/test/isolated/test_mimic_after.rb +0 -13
  111. data/test/isolated/test_mimic_alone.rb +0 -12
  112. data/test/isolated/test_mimic_as_json.rb +0 -45
  113. data/test/isolated/test_mimic_before.rb +0 -13
  114. data/test/isolated/test_mimic_define.rb +0 -28
  115. data/test/isolated/test_mimic_rails_after.rb +0 -22
  116. data/test/isolated/test_mimic_rails_before.rb +0 -21
  117. data/test/isolated/test_mimic_redefine.rb +0 -15
  118. data/test/json_gem/json_addition_test.rb +0 -216
  119. data/test/json_gem/json_common_interface_test.rb +0 -148
  120. data/test/json_gem/json_encoding_test.rb +0 -107
  121. data/test/json_gem/json_ext_parser_test.rb +0 -20
  122. data/test/json_gem/json_fixtures_test.rb +0 -35
  123. data/test/json_gem/json_generator_test.rb +0 -383
  124. data/test/json_gem/json_generic_object_test.rb +0 -90
  125. data/test/json_gem/json_parser_test.rb +0 -470
  126. data/test/json_gem/json_string_matching_test.rb +0 -42
  127. data/test/json_gem/test_helper.rb +0 -18
  128. data/test/perf.rb +0 -107
  129. data/test/perf_compat.rb +0 -130
  130. data/test/perf_fast.rb +0 -164
  131. data/test/perf_file.rb +0 -64
  132. data/test/perf_object.rb +0 -138
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -151
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -145
  137. data/test/perf_wab.rb +0 -131
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -54
  151. data/test/sample_json.rb +0 -37
  152. data/test/test_compat.rb +0 -509
  153. data/test/test_custom.rb +0 -503
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -470
  156. data/test/test_file.rb +0 -239
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_integer_range.rb +0 -73
  160. data/test/test_null.rb +0 -376
  161. data/test/test_object.rb +0 -1018
  162. data/test/test_saj.rb +0 -186
  163. data/test/test_scp.rb +0 -433
  164. data/test/test_strict.rb +0 -410
  165. data/test/test_various.rb +0 -741
  166. data/test/test_wab.rb +0 -307
  167. data/test/test_writer.rb +0 -380
  168. data/test/tests.rb +0 -24
  169. data/test/tests_mimic.rb +0 -14
  170. data/test/tests_mimic_addition.rb +0 -7
  171. data/test/zoo.rb +0 -13
@@ -1,90 +0,0 @@
1
- require 'bigdecimal'
2
- require 'date'
3
- require 'time'
4
-
5
- module JSONTest
6
- class Foo
7
- def initialize(a, b)
8
- @a, @b = a, b
9
- end
10
- end
11
-
12
- class Hashlike
13
- def to_hash
14
- { :foo => "hello", :bar => "world" }
15
- end
16
- end
17
-
18
- class Custom
19
- def initialize(serialized)
20
- @serialized = serialized
21
- end
22
-
23
- def as_json(options = nil)
24
- @serialized
25
- end
26
- end
27
-
28
- class MyStruct < Struct.new(:name, :value)
29
- def initialize(*)
30
- @unused = "unused instance variable"
31
- super
32
- end
33
- end
34
-
35
- module EncodingTestCases
36
- TrueTests = [[ true, %(true) ]]
37
- FalseTests = [[ false, %(false) ]]
38
- NilTests = [[ nil, %(null) ]]
39
- NumericTests = [[ 1, %(1) ],
40
- [ 2.5, %(2.5) ],
41
- [ 0.0/0.0, %(null) ],
42
- [ 1.0/0.0, %(null) ],
43
- [ -1.0/0.0, %(null) ],
44
- [ BigDecimal('0.0')/BigDecimal('0.0'), %(null) ],
45
- [ BigDecimal('2.5'), %("#{BigDecimal('2.5')}") ]]
46
-
47
- StringTests = [[ 'this is the <string>', %("this is the \\u003cstring\\u003e")],
48
- [ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ],
49
- [ 'http://test.host/posts/1', %("http://test.host/posts/1")],
50
- [ "Control characters: \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\u2028\u2029",
51
- %("Control characters: \\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\\u2028\\u2029") ]]
52
-
53
- ArrayTests = [[ ['a', 'b', 'c'], %([\"a\",\"b\",\"c\"]) ],
54
- [ [1, 'a', :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
55
-
56
- HashTests = [[ {foo: "bar"}, %({\"foo\":\"bar\"}) ],
57
- [ {1 => 1, 2 => 'a', 3 => :b, 4 => nil, 5 => false}, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]]
58
-
59
- RangeTests = [[ 1..2, %("1..2")],
60
- [ 1...2, %("1...2")],
61
- [ 1.5..2.5, %("1.5..2.5")]]
62
-
63
- SymbolTests = [[ :a, %("a") ],
64
- [ :this, %("this") ],
65
- [ :"a b", %("a b") ]]
66
-
67
- ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
68
- HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
69
- StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
70
- [ MyStruct.new(nil, nil), %({\"name\":null,\"value\":null}) ]]
71
- CustomTests = [[ Custom.new("custom"), '"custom"' ],
72
- [ Custom.new(nil), 'null' ],
73
- [ Custom.new(:a), '"a"' ],
74
- [ Custom.new([ :foo, "bar" ]), '["foo","bar"]' ],
75
- [ Custom.new({ :foo => "hello", :bar => "world" }), '{"bar":"world","foo":"hello"}' ],
76
- [ Custom.new(Hashlike.new), '{"bar":"world","foo":"hello"}' ],
77
- [ Custom.new(Custom.new(Custom.new(:a))), '"a"' ]]
78
-
79
- RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']]
80
-
81
- DateTests = [[ Date.new(2005,2,1), %("2005/02/01") ]]
82
- TimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]
83
- DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]
84
-
85
- StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]]
86
- StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10.000Z") ]]
87
- StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10.000+00:00") ]]
88
- StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]]
89
- end
90
- end
@@ -1,50 +0,0 @@
1
- require 'active_support/testing/assertions'
2
- require 'active_support/testing/deprecation'
3
- require 'active_support/testing/declarative'
4
- require 'minitest/autorun'
5
-
6
- module ActiveSupport
7
- class TestCase < ::Minitest::Test
8
- # Skips the current run on Rubinius using Minitest::Assertions#skip
9
- private def rubinius_skip(message = "")
10
- skip message if RUBY_ENGINE == "rbx"
11
- end
12
- # Skips the current run on JRuby using Minitest::Assertions#skip
13
- private def jruby_skip(message = "")
14
- skip message if defined?(JRUBY_VERSION)
15
- end
16
-
17
- Assertion = Minitest::Assertion
18
-
19
- alias_method :method_name, :name
20
-
21
- include ActiveSupport::Testing::Assertions
22
- include ActiveSupport::Testing::Deprecation
23
- extend ActiveSupport::Testing::Declarative
24
-
25
- # test/unit backwards compatibility methods
26
- alias :assert_raise :assert_raises
27
- alias :assert_not_empty :refute_empty
28
- alias :assert_not_equal :refute_equal
29
- alias :assert_not_in_delta :refute_in_delta
30
- alias :assert_not_in_epsilon :refute_in_epsilon
31
- alias :assert_not_includes :refute_includes
32
- alias :assert_not_instance_of :refute_instance_of
33
- alias :assert_not_kind_of :refute_kind_of
34
- alias :assert_no_match :refute_match
35
- alias :assert_not_nil :refute_nil
36
- alias :assert_not_operator :refute_operator
37
- alias :assert_not_predicate :refute_predicate
38
- alias :assert_not_respond_to :refute_respond_to
39
- alias :assert_not_same :refute_same
40
-
41
- # Fails if the block raises an exception.
42
- #
43
- # assert_nothing_raised do
44
- # ...
45
- # end
46
- def assert_nothing_raised(*args)
47
- yield
48
- end
49
- end
50
- end
@@ -1,24 +0,0 @@
1
- module TimeZoneTestHelpers
2
- def with_tz_default(tz = nil)
3
- old_tz = Time.zone
4
- Time.zone = tz
5
- yield
6
- ensure
7
- Time.zone = old_tz
8
- end
9
-
10
- def with_env_tz(new_tz = 'US/Eastern')
11
- old_tz, ENV['TZ'] = ENV['TZ'], new_tz
12
- yield
13
- ensure
14
- old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
15
- end
16
-
17
- def with_preserve_timezone(value)
18
- old_preserve_tz = ActiveSupport.to_time_preserves_timezone
19
- ActiveSupport.to_time_preserves_timezone = value
20
- yield
21
- ensure
22
- ActiveSupport.to_time_preserves_timezone = old_preserve_tz
23
- end
24
- end
data/test/bar.rb DELETED
@@ -1,25 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
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
9
-
10
- require 'oj'
11
-
12
- #Oj.load_file(ARGV[0], mode: :strict) { |obj|
13
- # puts Oj.dump(obj, indent: 2)
14
- #}
15
-
16
- data = open('invalid_unicode.data').read
17
-
18
- puts data
19
-
20
- puts Oj.dump(data)
21
-
22
- Oj.mimic_JSON
23
- puts Oj.dump(data, escape_mode: :json)
24
-
25
- puts Oj.default_options
data/test/files.rb DELETED
@@ -1,29 +0,0 @@
1
- #!/usr/bin/env ruby -wW2
2
-
3
- if $0 == __FILE__
4
- $: << '.'
5
- $: << '..'
6
- $: << '../lib'
7
- $: << '../ext'
8
- end
9
-
10
- require 'pp'
11
- require 'sample/file'
12
- require 'sample/dir'
13
-
14
- def files(dir)
15
- d = ::Sample::Dir.new(dir)
16
- Dir.new(dir).each do |fn|
17
- next if fn.start_with?('.')
18
- filename = File.join(dir, fn)
19
- #filename = '.' == dir ? fn : File.join(dir, fn)
20
- if File.directory?(filename)
21
- d << files(filename)
22
- else
23
- d << ::Sample::File.new(filename)
24
- end
25
- end
26
- #pp d
27
- d
28
- end
29
-
data/test/foo.rb DELETED
@@ -1,21 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
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
9
-
10
- require 'oj'
11
- require 'active_support'
12
- require 'active_support/json'
13
- #require 'tracer'
14
-
15
- Oj::Rails.set_encoder()
16
- Oj::Rails.set_decoder()
17
- Oj::Rails.optimize()
18
-
19
- #Oj::mimic_JSON
20
-
21
- puts JSON.parse('{"a":1}', symbolize_names: true)
data/test/helper.rb DELETED
@@ -1,26 +0,0 @@
1
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
2
- # required. That can be set in the RUBYOPT environment variable.
3
- # export RUBYOPT=-w
4
-
5
- $VERBOSE = true
6
-
7
- %w(lib ext test).each do |dir|
8
- $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
9
- end
10
-
11
- require 'minitest'
12
- require 'minitest/autorun'
13
- require 'stringio'
14
- require 'date'
15
- require 'bigdecimal'
16
- require 'pp'
17
- require 'oj'
18
-
19
- $ruby = RUBY_DESCRIPTION.split(' ')[0]
20
- $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
21
-
22
- class Range
23
- def to_hash()
24
- { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
25
- end
26
- end
@@ -1,308 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- # The rails tests set this to true. Both Rails and the JSON gem monkey patch the
4
- # as_json methods on several base classes. Depending on which one replaces the
5
- # method last the behavior will be different. Oj.mimic_JSON abides by the same
6
- # conflicting behavior and the tests reflect that.
7
- $rails_monkey = false unless defined?($rails_monkey)
8
-
9
- class SharedMimicTest < Minitest::Test
10
- class Jam
11
- attr_accessor :x, :y
12
-
13
- def initialize(x, y)
14
- @x = x
15
- @y = y
16
- end
17
-
18
- def eql?(o)
19
- self.class == o.class && @x == o.x && @y == o.y
20
- end
21
- alias == eql?
22
-
23
- def as_json()
24
- {"json_class" => self.class.to_s,"x" => @x,"y" => @y}
25
- end
26
-
27
- def self.json_create(h)
28
- self.new(h['x'], h['y'])
29
- end
30
-
31
- end # Jam
32
-
33
- def setup
34
- @default_options = Oj.default_options
35
- @time = Time.at(1400000000).utc
36
- @expected_time_string =
37
- if defined?(Rails)
38
- %{"2014-05-13T16:53:20.000Z"}
39
- else
40
- %{"2014-05-13 16:53:20 UTC"}
41
- end
42
- end
43
-
44
- def teardown
45
- Oj.default_options = @default_options
46
- end
47
-
48
- # exception
49
- def test_exception
50
- begin
51
- JSON.parse("{")
52
- puts "Failed"
53
- rescue JSON::ParserError
54
- assert(true)
55
- rescue Exception
56
- assert(false, 'Expected a JSON::ParserError')
57
- end
58
- end
59
-
60
- # dump
61
- def test_dump_string
62
- json = JSON.dump([1, true, nil, @time])
63
- if $rails_monkey
64
- assert_equal(%{[1,true,null,#{@expected_time_string}]}, json)
65
- else
66
- assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, json)
67
- end
68
- end
69
-
70
- def test_dump_with_options
71
- Oj.default_options= {:indent => 2} # JSON this will not change anything
72
- json = JSON.dump([1, true, nil, @time])
73
- if $rails_monkey
74
- assert_equal(%{[
75
- 1,
76
- true,
77
- null,
78
- #{@expected_time_string}
79
- ]
80
- }, json)
81
- else
82
- assert_equal(%{[
83
- 1,
84
- true,
85
- null,
86
- {
87
- "json_class":"Time",
88
- "s":1400000000,
89
- "n\":0
90
- }
91
- ]
92
- }, json)
93
- end
94
- end
95
-
96
- def test_dump_io
97
- s = StringIO.new()
98
- json = JSON.dump([1, true, nil, @time], s)
99
- assert_equal(s, json)
100
- if $rails_monkey
101
- assert_equal(%{[1,true,null,#{@expected_time_string}]}, s.string)
102
- else
103
- assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, s.string)
104
- end
105
- end
106
- # TBD options
107
-
108
- def test_dump_struct
109
- # anonymous Struct not supported by json so name it
110
- if Object.const_defined?("Struct::Abc")
111
- s = Struct::Abc
112
- else
113
- s = Struct.new("Abc", :a, :b, :c)
114
- end
115
- o = s.new(1, 'two', [true, false])
116
- json = JSON.dump(o)
117
- if o.respond_to?(:as_json)
118
- if $rails_monkey
119
- assert_equal(%|{"a":1,"b":"two","c":[true,false]}|, json)
120
- else
121
- assert_equal(%|{"json_class":"Struct::Abc","v":[1,"two",[true,false]]}|, json)
122
- end
123
- else
124
- j = '"' + o.to_s.gsub('"', '\\"') + '"'
125
- assert_equal(j, json)
126
- end
127
- end
128
-
129
- # load
130
- def test_load_string
131
- json = %{{"a":1,"b":[true,false]}}
132
- obj = JSON.load(json)
133
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
134
- end
135
-
136
- def test_load_io
137
- json = %{{"a":1,"b":[true,false]}}
138
- obj = JSON.load(StringIO.new(json))
139
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
140
- end
141
-
142
- def test_load_proc
143
- Oj.mimic_JSON
144
- children = []
145
- json = %{{"a":1,"b":[true,false]}}
146
- if 'rubinius' == $ruby
147
- obj = JSON.load(json) {|x| children << x }
148
- else
149
- p = Proc.new {|x| children << x }
150
- obj = JSON.load(json, p)
151
- end
152
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
153
- assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
154
- [true, false, [true, false], 1, { 'a' => 1, 'b' => [true, false]}] == children,
155
- "children don't match")
156
- end
157
-
158
- def test_parse_with_quirks_mode
159
- json = %{null}
160
- assert_equal(nil, JSON.parse(json, :quirks_mode => true))
161
- assert_raises(JSON::ParserError) { JSON.parse(json, :quirks_mode => false) }
162
- assert_raises(JSON::ParserError) { JSON.parse(json) }
163
- end
164
-
165
- def test_parse_with_empty_string
166
- Oj.mimic_JSON
167
- assert_raises(JSON::ParserError) { JSON.parse(' ') }
168
- assert_raises(JSON::ParserError) { JSON.parse("\t\t\n ") }
169
- end
170
-
171
- # []
172
- def test_bracket_load
173
- json = %{{"a":1,"b":[true,false]}}
174
- obj = JSON[json]
175
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
176
- end
177
-
178
- def test_bracket_dump
179
- json = JSON[[1, true, nil]]
180
- assert_equal(%{[1,true,null]}, json)
181
- end
182
-
183
- # generate
184
- def test_generate
185
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
186
- assert(%{{"a":1,"b":[true,false]}} == json ||
187
- %{{"b":[true,false],"a":1}} == json)
188
- end
189
- def test_generate_options
190
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]},
191
- :indent => "--",
192
- :array_nl => "\n",
193
- :object_nl => "#\n",
194
- :space => "*",
195
- :space_before => "~")
196
- assert(%{{#
197
- --"a"~:*1,#
198
- --"b"~:*[
199
- ----true,
200
- ----false
201
- --]#
202
- }} == json ||
203
- %{{#
204
- --"b"~:*[
205
- ----true,
206
- ----false
207
- --],#
208
- --"a"~:*1#
209
- }} == json)
210
-
211
- end
212
-
213
- # fast_generate
214
- def test_fast_generate
215
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
216
- assert(%{{"a":1,"b":[true,false]}} == json ||
217
- %{{"b":[true,false],"a":1}} == json)
218
- end
219
-
220
- # pretty_generate
221
- def test_pretty_generate
222
- json = JSON.pretty_generate({ 'a' => 1, 'b' => [true, false]})
223
- assert(%{{
224
- "a": 1,
225
- "b": [
226
- true,
227
- false
228
- ]
229
- }} == json ||
230
- %{{
231
- "b": [
232
- true,
233
- false
234
- ],
235
- "a": 1
236
- }} == json)
237
- end
238
-
239
- # parse
240
- def test_parse
241
- json = %{{"a":1,"b":[true,false]}}
242
- obj = JSON.parse(json)
243
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
244
- end
245
- def test_parse_sym_names
246
- json = %{{"a":1,"b":[true,false]}}
247
- obj = JSON.parse(json, :symbolize_names => true)
248
- assert_equal({ :a => 1, :b => [true, false]}, obj)
249
- end
250
- def test_parse_additions
251
- jam = Jam.new(true, 58)
252
- json = Oj.dump(jam, :mode => :compat, :use_to_json => true)
253
- obj = JSON.parse(json)
254
- assert_equal(jam, obj)
255
- obj = JSON.parse(json, :create_additions => true)
256
- assert_equal(jam, obj)
257
- obj = JSON.parse(json, :create_additions => false)
258
- assert_equal({'json_class' => 'SharedMimicTest::Jam', 'x' => true, 'y' => 58}, obj)
259
- json.gsub!('json_class', 'kson_class')
260
- JSON.create_id = 'kson_class'
261
- obj = JSON.parse(json, :create_additions => true)
262
- JSON.create_id = 'json_class'
263
- assert_equal(jam, obj)
264
- end
265
- def test_parse_bang
266
- json = %{{"a":1,"b":[true,false]}}
267
- obj = JSON.parse!(json)
268
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
269
- end
270
-
271
- # recurse_proc
272
- def test_recurse_proc
273
- children = []
274
- JSON.recurse_proc({ 'a' => 1, 'b' => [true, false]}) { |x| children << x }
275
- # JRuby 1.7.0 rb_yield() is broken and converts the [true, false] array into true
276
- unless 'jruby' == $ruby
277
- assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
278
- [true, false, [true, false], 1, { 'b' => [true, false], 'a' => 1}] == children)
279
- end
280
- end
281
-
282
- # make sure to_json is defined for object.
283
- def test_mimic_to_json
284
- {'a' => 1}.to_json()
285
- Object.new().to_json()
286
- end
287
- end # SharedMimicTest
288
-
289
- if defined?(ActiveSupport)
290
- class SharedMimicRailsTest < SharedMimicTest
291
- def test_activesupport_exception
292
- begin
293
- ActiveSupport::JSON.decode("{")
294
- puts "Failed"
295
- rescue ActiveSupport::JSON.parse_error
296
- assert(true)
297
- rescue Exception
298
- assert(false, 'Expected a JSON::ParserError')
299
- end
300
- end
301
-
302
- def test_activesupport_encode
303
- Oj.default_options= {:indent => 0}
304
- json = ActiveSupport::JSON.encode([1, true, nil])
305
- assert_equal(%{[1,true,null]}, json)
306
- end
307
- end # SharedMimicRailsTest
308
- end
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), '..')
5
-
6
- require 'helper'
7
- require 'isolated/shared'
8
-
9
- require 'json'
10
- Oj.mimic_JSON
11
-
12
- class MimicAfter < SharedMimicTest
13
- end # MimicAfter
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), '..')
5
-
6
- require 'helper'
7
- require 'isolated/shared'
8
-
9
- Oj.mimic_JSON
10
-
11
- class MimicAlone < SharedMimicTest
12
- end # MimicAlone
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
- require 'oj'
8
-
9
- class ObjectFolder < Minitest::Test
10
- class Raccoon
11
- attr_accessor :name
12
-
13
- def initialize(name)
14
- @name = name
15
- end
16
-
17
- def as_json(options={})
18
- {:name => @name}.merge(options)
19
- end
20
- end
21
-
22
- def setup
23
- @default_options = Oj.default_options
24
- end
25
-
26
- def teardown
27
- Oj.default_options = @default_options
28
- end
29
-
30
- def test_as_json_options
31
- Oj.mimic_JSON()
32
- raccoon = Raccoon.new('Rocket')
33
- json = raccoon.to_json()
34
- assert_equal(json, '{"name":"Rocket"}')
35
-
36
- json = raccoon.to_json(:occupation => 'bounty hunter')
37
- # depending on the ruby version the order of the hash members maybe different.
38
- if (json.start_with?('{"name'))
39
- assert_equal(json, '{"name":"Rocket","occupation":"bounty hunter"}')
40
- else
41
- assert_equal(json, '{"occupation":"bounty hunter","name":"Rocket"}')
42
- end
43
- end
44
-
45
- end
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), '..')
5
-
6
- require 'helper'
7
- require 'isolated/shared'
8
-
9
- Oj.mimic_JSON
10
- require 'json'
11
-
12
- class MimicBefore < SharedMimicTest
13
- end # MimicBefore
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), '..')
5
-
6
- require 'helper'
7
-
8
- class MimicDefine < Minitest::Test
9
- def test_mimic_define
10
- assert(defined?(JSON).nil?)
11
- Oj.mimic_JSON
12
-
13
- # Test constants
14
- assert(!defined?(JSON).nil?)
15
- assert(!defined?(JSON::ParserError).nil?)
16
- assert(Object.respond_to?(:to_json))
17
-
18
- # Test loaded features
19
- assert(!require('json'))
20
-
21
- begin
22
- require('json_spec')
23
- assert(false, '** should raise LoadError')
24
- rescue LoadError
25
- assert(true)
26
- end
27
- end
28
- end # MimicSingle