oj 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +17 -23
  3. data/README.md +74 -425
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +4 -0
  6. data/ext/oj/circarray.c +68 -0
  7. data/ext/oj/circarray.h +23 -0
  8. data/ext/oj/code.c +227 -0
  9. data/ext/oj/code.h +40 -0
  10. data/ext/oj/compat.c +243 -0
  11. data/ext/oj/custom.c +1097 -0
  12. data/ext/oj/dump.c +766 -1534
  13. data/ext/oj/dump.h +92 -0
  14. data/ext/oj/dump_compat.c +937 -0
  15. data/ext/oj/dump_leaf.c +254 -0
  16. data/ext/oj/dump_object.c +810 -0
  17. data/ext/oj/dump_rails.c +329 -0
  18. data/ext/oj/dump_strict.c +416 -0
  19. data/ext/oj/encode.h +51 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +17 -7
  23. data/ext/oj/fast.c +213 -180
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +817 -0
  28. data/ext/oj/mimic_rails.c +806 -0
  29. data/ext/oj/mimic_rails.h +17 -0
  30. data/ext/oj/object.c +752 -0
  31. data/ext/oj/odd.c +230 -0
  32. data/ext/oj/odd.h +44 -0
  33. data/ext/oj/oj.c +1288 -929
  34. data/ext/oj/oj.h +240 -69
  35. data/ext/oj/parse.c +1014 -0
  36. data/ext/oj/parse.h +92 -0
  37. data/ext/oj/reader.c +223 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +127 -0
  40. data/ext/oj/{cache.h → resolve.h} +6 -13
  41. data/ext/oj/rxclass.c +133 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +77 -175
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +911 -0
  46. data/ext/oj/stream_writer.c +301 -0
  47. data/ext/oj/strict.c +162 -0
  48. data/ext/oj/string_writer.c +480 -0
  49. data/ext/oj/val_stack.c +98 -0
  50. data/ext/oj/val_stack.h +188 -0
  51. data/lib/oj/active_support_helper.rb +41 -0
  52. data/lib/oj/bag.rb +6 -10
  53. data/lib/oj/easy_hash.rb +52 -0
  54. data/lib/oj/json.rb +172 -0
  55. data/lib/oj/mimic.rb +260 -5
  56. data/lib/oj/saj.rb +13 -10
  57. data/lib/oj/schandler.rb +142 -0
  58. data/lib/oj/state.rb +131 -0
  59. data/lib/oj/version.rb +1 -1
  60. data/lib/oj.rb +11 -23
  61. data/pages/Advanced.md +22 -0
  62. data/pages/Compatibility.md +25 -0
  63. data/pages/Custom.md +23 -0
  64. data/pages/Encoding.md +65 -0
  65. data/pages/JsonGem.md +79 -0
  66. data/pages/Modes.md +140 -0
  67. data/pages/Options.md +250 -0
  68. data/pages/Rails.md +60 -0
  69. data/pages/Security.md +20 -0
  70. data/test/_test_active.rb +76 -0
  71. data/test/_test_active_mimic.rb +96 -0
  72. data/test/_test_mimic_rails.rb +126 -0
  73. data/test/activesupport4/decoding_test.rb +105 -0
  74. data/test/activesupport4/encoding_test.rb +531 -0
  75. data/test/activesupport4/test_helper.rb +41 -0
  76. data/test/activesupport5/decoding_test.rb +125 -0
  77. data/test/activesupport5/encoding_test.rb +483 -0
  78. data/test/activesupport5/encoding_test_cases.rb +90 -0
  79. data/test/activesupport5/test_helper.rb +50 -0
  80. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  81. data/test/helper.rb +27 -0
  82. data/test/isolated/shared.rb +310 -0
  83. data/test/isolated/test_mimic_after.rb +13 -0
  84. data/test/isolated/test_mimic_alone.rb +12 -0
  85. data/test/isolated/test_mimic_as_json.rb +45 -0
  86. data/test/isolated/test_mimic_before.rb +13 -0
  87. data/test/isolated/test_mimic_define.rb +28 -0
  88. data/test/isolated/test_mimic_rails_after.rb +22 -0
  89. data/test/isolated/test_mimic_rails_before.rb +21 -0
  90. data/test/isolated/test_mimic_redefine.rb +15 -0
  91. data/test/json_gem/json_addition_test.rb +216 -0
  92. data/test/json_gem/json_common_interface_test.rb +143 -0
  93. data/test/json_gem/json_encoding_test.rb +109 -0
  94. data/test/json_gem/json_ext_parser_test.rb +20 -0
  95. data/test/json_gem/json_fixtures_test.rb +35 -0
  96. data/test/json_gem/json_generator_test.rb +383 -0
  97. data/test/json_gem/json_generic_object_test.rb +90 -0
  98. data/test/json_gem/json_parser_test.rb +470 -0
  99. data/test/json_gem/json_string_matching_test.rb +42 -0
  100. data/test/json_gem/test_helper.rb +18 -0
  101. data/test/perf_compat.rb +130 -0
  102. data/test/perf_fast.rb +9 -9
  103. data/test/perf_file.rb +64 -0
  104. data/test/{perf_obj.rb → perf_object.rb} +24 -10
  105. data/test/perf_scp.rb +151 -0
  106. data/test/perf_strict.rb +32 -113
  107. data/test/sample.rb +2 -3
  108. data/test/test_compat.rb +474 -0
  109. data/test/test_custom.rb +355 -0
  110. data/test/test_debian.rb +53 -0
  111. data/test/test_fast.rb +66 -16
  112. data/test/test_file.rb +237 -0
  113. data/test/test_gc.rb +49 -0
  114. data/test/test_hash.rb +29 -0
  115. data/test/test_null.rb +376 -0
  116. data/test/test_object.rb +1010 -0
  117. data/test/test_saj.rb +16 -16
  118. data/test/test_scp.rb +417 -0
  119. data/test/test_strict.rb +410 -0
  120. data/test/test_various.rb +815 -0
  121. data/test/test_writer.rb +308 -0
  122. data/test/tests.rb +9 -902
  123. data/test/tests_mimic.rb +14 -0
  124. data/test/tests_mimic_addition.rb +7 -0
  125. metadata +253 -38
  126. data/ext/oj/cache.c +0 -148
  127. data/ext/oj/foo.rb +0 -6
  128. data/ext/oj/load.c +0 -1049
  129. data/test/a.rb +0 -38
  130. data/test/perf1.rb +0 -64
  131. data/test/perf2.rb +0 -76
  132. data/test/perf_obj_old.rb +0 -213
  133. data/test/test_mimic.rb +0 -208
data/test/a.rb DELETED
@@ -1,38 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'pp'
9
- require 'oj'
10
- require 'perf'
11
-
12
- obj = [[1],[2],[3],[4],[5],[6],[7],[8],[9]]
13
- obj = [[],[],[],[],[],[],[],[],[]]
14
- obj = {
15
- 'a' => 'Alpha', # string
16
- 'b' => true, # boolean
17
- 'c' => 12345, # number
18
- 'd' => [ true, [false, [12345, nil], 3.967, ['something', false], nil]], # mix it up array
19
- 'e' => { 'one' => 1, 'two' => 2 }, # hash
20
- 'f' => nil, # nil
21
- 'g' => 12345678901234567890123456789, # big number
22
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
23
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
24
- }
25
-
26
- json = Oj.dump(obj, mode: :compat)
27
-
28
- puts json
29
- #pp Oj.saj_parse(nil, json)
30
- pp Oj.t_parse(json)
31
-
32
- if true
33
- perf = Perf.new()
34
- perf.add('SAJ', 'oj') { Oj.saj_parse(nil, json) }
35
- perf.add('T', 'oj') { Oj.t_parse(json) }
36
- perf.add('load', 'oj') { Oj.load(json) }
37
- perf.run(10000)
38
- end
data/test/perf1.rb DELETED
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
6
-
7
- #require 'test/unit'
8
- require 'optparse'
9
- require 'oj'
10
- require 'ox'
11
-
12
- $indent = 2
13
-
14
- opts = OptionParser.new
15
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
16
- files = opts.parse(ARGV)
17
-
18
- iter = 100000
19
- s = %{
20
- { "class": "Foo::Bar",
21
- "attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
22
- "attr2": { "one": 1 }
23
- }
24
- }
25
- #s = File.read('sample.json')
26
-
27
- Oj.default_options = { :indent => 0 }
28
-
29
- obj = Oj.load(s)
30
- xml = Ox.dump(obj, :indent => 0)
31
-
32
- puts xml
33
-
34
- start = Time.now
35
- iter.times do
36
- Oj.load(s)
37
- end
38
- dt = Time.now - start
39
- puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
40
-
41
- start = Time.now
42
- iter.times do
43
- Ox.load(xml)
44
- end
45
- dt = Time.now - start
46
- puts "%d Ox.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
47
-
48
- puts
49
-
50
- start = Time.now
51
- iter.times do
52
- Oj.dump(obj)
53
- end
54
- dt = Time.now - start
55
- puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
56
-
57
- start = Time.now
58
- iter.times do
59
- Ox.dump(obj)
60
- end
61
- dt = Time.now - start
62
- puts "%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
63
-
64
- puts
data/test/perf2.rb DELETED
@@ -1,76 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
6
-
7
- #require 'test/unit'
8
- require 'optparse'
9
- require 'yajl'
10
- require 'oj'
11
-
12
- $indent = 2
13
-
14
- opts = OptionParser.new
15
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
16
- files = opts.parse(ARGV)
17
-
18
- class Foo
19
- def initialize()
20
- @x = true
21
- @y = 58
22
- end
23
- def to_json()
24
- %{{"x":#{@x},"y":#{@y}}}
25
- end
26
- def to_hash()
27
- { 'x' => @x, 'y' => @y }
28
- end
29
- end
30
-
31
- iter = 100000
32
- s = %{
33
- { "class": "Foo::Bar",
34
- "attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
35
- "attr2": { "one": 1 }
36
- }
37
- }
38
-
39
- obj = Oj.load(s)
40
- obj["foo"] = Foo.new()
41
-
42
- Oj.default_options = { :indent => 0, :effort => :internal }
43
-
44
- puts
45
-
46
- start = Time.now
47
- iter.times do
48
- Oj.load(s)
49
- end
50
- dt = Time.now - start
51
- puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [iter, dt, iter/dt/1000.0]
52
-
53
- start = Time.now
54
- iter.times do
55
- Yajl::Parser.parse(s)
56
- end
57
- dt = Time.now - start
58
- puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, dt, iter/dt/1000.0]
59
-
60
- puts
61
-
62
- start = Time.now
63
- iter.times do
64
- Oj.dump(obj)
65
- end
66
- dt = Time.now - start
67
- puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [iter, dt, iter/dt/1000.0]
68
-
69
- start = Time.now
70
- iter.times do
71
- Yajl::Encoder.encode(obj)
72
- end
73
- dt = Time.now - start
74
- puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [iter, dt, iter/dt/1000.0]
75
-
76
- puts
data/test/perf_obj_old.rb DELETED
@@ -1,213 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '.'
4
- $: << '..'
5
- $: << '../lib'
6
- $: << '../ext'
7
-
8
- if __FILE__ == $0
9
- if (i = ARGV.index('-I'))
10
- x,path = ARGV.slice!(i, 2)
11
- $: << path
12
- end
13
- end
14
-
15
- require 'optparse'
16
- require 'ox'
17
- require 'oj'
18
- require 'perf'
19
- require 'sample'
20
- require 'files'
21
-
22
- $verbose = 0
23
- $circular = false
24
- $indent = 0
25
-
26
- do_sample = false
27
- do_files = false
28
-
29
- do_load = false
30
- do_dump = false
31
- do_read = false
32
- do_write = false
33
- $iter = 1000
34
-
35
- opts = OptionParser.new
36
- opts.on("-v", "increase verbosity") { $verbose += 1 }
37
-
38
- opts.on("-c", "circular options") { $circular = true }
39
-
40
- opts.on("-s", "load and dump as sample Ruby object") { do_sample = true }
41
- opts.on("-f", "load and dump as files Ruby object") { do_files = true }
42
-
43
- opts.on("-l", "load") { do_load = true }
44
- opts.on("-d", "dump") { do_dump = true }
45
- opts.on("-r", "read") { do_read = true }
46
- opts.on("-w", "write") { do_write = true }
47
- opts.on("-a", "load, dump, read and write") { do_load = true; do_dump = true; do_read = true; do_write = true }
48
-
49
- opts.on("-i", "--iterations [Int]", Integer, "iterations") { |i| $iter = i }
50
-
51
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
52
- files = opts.parse(ARGV)
53
-
54
- if files.empty?
55
- data = []
56
- obj = do_sample ? sample_doc(2) : files('..')
57
- mars = Marshal.dump(obj)
58
- xml = Ox.dump(obj, :indent => $indent, circular: $circular)
59
- json = Oj.dump(obj, :indent => $indent, circular: $circular)
60
- File.open('sample.xml', 'w') { |f| f.write(xml) }
61
- File.open('sample.json', 'w') { |f| f.write(json) }
62
- File.open('sample.marshal', 'w') { |f| f.write(mars) }
63
- data << { :file => 'sample.xml', :obj => obj, :xml => xml, :marshal => mars, :json => json }
64
- else
65
- puts "loading and parsing #{files}\n\n"
66
- # TBD change to allow xml and json
67
- data = files.map do |f|
68
- xml = File.read(f)
69
- obj = Ox.load(xml);
70
- mars = Marshal.dump(obj)
71
- json = Oj.dump(obj, :indent => $indent, circular: $circular)
72
- { :file => f, :obj => obj, :xml => xml, :marshal => mars, :json => json }
73
- end
74
- end
75
-
76
- $ox_load_time = 0
77
- $mars_load_time = 0
78
- $ox_dump_time = 0
79
- $oj_dump_time = 0
80
- $mars_dump_time = 0
81
-
82
- def perf_load(d)
83
- filename = d[:file]
84
- marshal_filename = 'sample.marshal'
85
- xml = d[:xml]
86
- mars = d[:marshal]
87
- json = d[:json]
88
-
89
- if 0 < $verbose
90
- obj = Ox.load(xml, :mode => :object, :trace => $verbose)
91
- return
92
- end
93
- start = Time.now
94
- (1..$iter).each do
95
- obj = Ox.load(xml, :mode => :object)
96
- end
97
- $ox_load_time = Time.now - start
98
- puts "Parsing #{$iter} times with Ox took #{$ox_load_time} seconds."
99
-
100
- start = Time.now
101
- (1..$iter).each do
102
- obj = Oj.load(json, :mode => :object)
103
- end
104
- $oj_load_time = Time.now - start
105
- puts "Parsing #{$iter} times with Oj took #{$oj_load_time} seconds."
106
-
107
- start = Time.now
108
- (1..$iter).each do
109
- obj = Marshal.load(mars)
110
- end
111
- $mars_load_time = Time.now - start
112
- puts "Marshalling #{$iter} times took #{$mars_load_time} seconds."
113
- puts ">>> Ox is %0.1f faster than Marshal loading.\n\n" % [$mars_load_time/$ox_load_time]
114
- end
115
-
116
- def perf_dump(d)
117
- obj = d[:obj]
118
-
119
- start = Time.now
120
- (1..$iter).each do
121
- xml = Ox.dump(obj, :indent => $indent, :circular => $circular)
122
- #puts "*** ox:\n#{xml}"
123
- end
124
- $ox_dump_time = Time.now - start
125
- puts "Ox dumping #{$iter} times with ox took #{$ox_dump_time} seconds."
126
-
127
- Oj.default_options = {:indent => $indent}
128
- start = Time.now
129
- (1..$iter).each do
130
- json = Oj.dump(obj)
131
- end
132
- $oj_dump_time = Time.now - start
133
- puts "Oj dumping #{$iter} times with oj took #{$oj_dump_time} seconds."
134
-
135
- obj = d[:obj]
136
- start = Time.now
137
- (1..$iter).each do
138
- m = Marshal.dump(obj)
139
- end
140
- $mars_dump_time = Time.now - start
141
- puts "Marshal dumping #{$iter} times took #{$mars_dump_time} seconds."
142
- puts ">>> Ox is %0.1f faster than Marshal dumping.\n\n" % [$mars_dump_time/$ox_dump_time]
143
- end
144
-
145
- def perf_read(d)
146
- ox_read_time = 0
147
- mars_read_time = 0
148
-
149
- filename = d[:file]
150
- marshal_filename = 'sample.marshal'
151
- xml = d[:xml]
152
- mars = d[:marshal]
153
-
154
- # now load from the file
155
- start = Time.now
156
- (1..$iter).each do
157
- obj = Ox.load_file(filename, :mode => :object)
158
- end
159
- ox_read_time = Time.now - start
160
- puts "Loading and parsing #{$iter} times with ox took #{ox_read_time} seconds."
161
-
162
- start = Time.now
163
- (1..$iter).each do
164
- m = File.read(marshal_filename)
165
- obj = Marshal.load(m)
166
- end
167
- mars_read_time = Time.now - start
168
- puts "Reading and marshalling #{$iter} times took #{mars_read_time} seconds."
169
- puts ">>> Ox is %0.1f faster than Marshal loading and parsing.\n\n" % [mars_read_time/ox_read_time]
170
-
171
- end
172
-
173
- def perf_write(d)
174
- ox_write_time = 0
175
- mars_write_time = 0
176
-
177
- ox_filename = 'out.xml'
178
- marshal_filename = 'out.marshal'
179
- obj = d[:obj]
180
-
181
- start = Time.now
182
- (1..$iter).each do
183
- xml = Ox.to_file(ox_filename, obj, :indent => $indent)
184
- end
185
- ox_write_time = Time.now - start
186
- puts "Ox dumping #{$iter} times with ox took #{ox_write_time} seconds."
187
-
188
- start = Time.now
189
- (1..$iter).each do
190
- m = Marshal.dump(obj, circular: $circular)
191
- File.open(marshal_filename, "w") { |f| f.write(m) }
192
- end
193
- mars_write_time = Time.now - start
194
- puts "Marshal dumping and writing #{$iter} times took #{mars_write_time} seconds."
195
- puts ">>> Ox is %0.1f faster than Marshal dumping.\n\n" % [mars_write_time/ox_write_time]
196
-
197
- end
198
-
199
- #if do_sample or do_files
200
- data.each do |d|
201
- puts "Using file #{d[:file]}."
202
-
203
- perf_load(d) if do_load
204
- perf_dump(d) if do_dump
205
- if do_load and do_dump
206
- puts ">>> Ox is %0.1f faster than Marshal dumping and loading.\n\n" % [($mars_load_time + $mars_dump_time)/($ox_load_time + $ox_dump_time)] unless 0 == $mars_load_time
207
- end
208
-
209
- perf_read(d) if do_read
210
- perf_write(d) if do_write
211
-
212
- end
213
- #end
data/test/test_mimic.rb DELETED
@@ -1,208 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
5
- # required. That can be set in the RUBYOPT environment variable.
6
- # export RUBYOPT=-w
7
-
8
- $VERBOSE = true
9
-
10
- $: << File.join(File.dirname(__FILE__), "../lib")
11
- $: << File.join(File.dirname(__FILE__), "../ext")
12
-
13
- require 'test/unit'
14
- require 'stringio'
15
- require 'oj'
16
-
17
- $ruby = RUBY_DESCRIPTION.split(' ')[0]
18
- $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
19
-
20
- class Jam
21
- attr_accessor :x, :y
22
-
23
- def initialize(x, y)
24
- @x = x
25
- @y = y
26
- end
27
-
28
- def eql?(o)
29
- self.class == o.class && @x == o.x && @y == o.y
30
- end
31
- alias == eql?
32
-
33
- def to_json()
34
- %{{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}}
35
- end
36
-
37
- def self.json_create(h)
38
- self.new(h['x'], h['y'])
39
- end
40
-
41
- end # Jam
42
-
43
- class Mimic < ::Test::Unit::TestCase
44
-
45
- def test0_mimic_json
46
- assert(defined?(JSON).nil?)
47
- Oj.mimic_JSON
48
- assert(!defined?(JSON).nil?)
49
- end
50
-
51
- # dump
52
- def test_dump_string
53
- json = JSON.dump([1, true, nil])
54
- assert_equal(%{[1,true,null]}, json)
55
- end
56
-
57
- def test_dump_io
58
- s = StringIO.new()
59
- json = JSON.dump([1, true, nil], s)
60
- assert_equal(s, json)
61
- assert_equal(%{[1,true,null]}, s.string)
62
- end
63
- # TBD options
64
-
65
- # load
66
- def test_load_string
67
- json = %{{"a":1,"b":[true,false]}}
68
- obj = JSON.load(json)
69
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
70
- end
71
-
72
- def test_load_io
73
- json = %{{"a":1,"b":[true,false]}}
74
- obj = JSON.load(StringIO.new(json))
75
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
76
- end
77
-
78
- def test_load_proc
79
- Oj.mimic_JSON # TBD
80
- children = []
81
- json = %{{"a":1,"b":[true,false]}}
82
- if 'rubinius' == $ruby || 'jruby' == $ruby || '1.8.7' == RUBY_VERSION
83
- obj = JSON.load(json) {|x| children << x }
84
- else
85
- p = Proc.new {|x| children << x }
86
- obj = JSON.load(json, p)
87
- end
88
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
89
- # JRuby 1.7.0 rb_yield() is broken and converts the [true, falser] array into true
90
- unless 'jruby' == $ruby && '1.9.3' == RUBY_VERSION
91
- assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
92
- [true, false, [true, false], 1, { 'a' => 1, 'b' => [true, false]}] == children,
93
- "children don't match")
94
- end
95
- end
96
-
97
- # []
98
- def test_bracket_load
99
- json = %{{"a":1,"b":[true,false]}}
100
- obj = JSON[json]
101
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
102
- end
103
-
104
- def test_bracket_dump
105
- json = JSON[[1, true, nil]]
106
- assert_equal(%{[1,true,null]}, json)
107
- end
108
-
109
- # generate
110
- def test_generate
111
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
112
- assert(%{{"a":1,"b":[true,false]}} == json ||
113
- %{{"b":[true,false],"a":1}} == json)
114
- end
115
- def test_generate_options
116
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]},
117
- :indent => "--",
118
- :array_nl => "\n",
119
- :object_nl => "#\n",
120
- :space => "*",
121
- :space_before => "~")
122
- assert(%{{#
123
- --"a"~:*1,#
124
- --"b"~:*[
125
- ----true,
126
- ----false
127
- --]#
128
- }} == json ||
129
- %{{#
130
- --"b"~:*[
131
- ----true,
132
- ----false
133
- --],#
134
- --"a"~:*1#
135
- }} == json)
136
-
137
- end
138
-
139
- # fast_generate
140
- def test_fast_generate
141
- json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
142
- assert(%{{"a":1,"b":[true,false]}} == json ||
143
- %{{"b":[true,false],"a":1}} == json)
144
- end
145
-
146
- # pretty_generate
147
- def test_pretty_generate
148
- json = JSON.pretty_generate({ 'a' => 1, 'b' => [true, false]})
149
- assert(%{{
150
- "a" : 1,
151
- "b" : [
152
- true,
153
- false
154
- ]
155
- }} == json ||
156
- %{{
157
- "b" : [
158
- true,
159
- false
160
- ],
161
- "a" : 1
162
- }} == json)
163
- end
164
-
165
- # parse
166
- def test_parse
167
- json = %{{"a":1,"b":[true,false]}}
168
- obj = JSON.parse(json)
169
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
170
- end
171
- def test_parse_sym_names
172
- json = %{{"a":1,"b":[true,false]}}
173
- obj = JSON.parse(json, :symbolize_names => true)
174
- assert_equal({ :a => 1, :b => [true, false]}, obj)
175
- end
176
- def test_parse_additions
177
- jam = Jam.new(true, 58)
178
- json = Oj.dump(jam, :mode => :compat)
179
- obj = JSON.parse(json)
180
- assert_equal(jam, obj)
181
- obj = JSON.parse(json, :create_additions => true)
182
- assert_equal(jam, obj)
183
- obj = JSON.parse(json, :create_additions => false)
184
- assert_equal({'json_class' => 'Jam', 'x' => true, 'y' => 58}, obj)
185
- json.gsub!('json_class', 'kson_class')
186
- JSON.create_id = 'kson_class'
187
- obj = JSON.parse(json, :create_additions => true)
188
- JSON.create_id = 'json_class'
189
- assert_equal(jam, obj)
190
- end
191
- def test_parse_bang
192
- json = %{{"a":1,"b":[true,false]}}
193
- obj = JSON.parse!(json)
194
- assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
195
- end
196
-
197
- # recurse_proc
198
- def test_recurse_proc
199
- children = []
200
- JSON.recurse_proc({ 'a' => 1, 'b' => [true, false]}) { |x| children << x }
201
- # JRuby 1.7.0 rb_yield() is broken and converts the [true, falser] array into true
202
- unless 'jruby' == $ruby && '1.9.3' == RUBY_VERSION
203
- assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
204
- [true, false, [true, false], 1, { 'b' => [true, false], 'a' => 1}] == children)
205
- end
206
- end
207
-
208
- end # Mimic