oj 3.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +104 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1218 -0
  13. data/ext/oj/dump.c +1249 -0
  14. data/ext/oj/dump.h +96 -0
  15. data/ext/oj/dump_compat.c +975 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +844 -0
  18. data/ext/oj/dump_strict.c +434 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +53 -0
  23. data/ext/oj/fast.c +1771 -0
  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 +890 -0
  28. data/ext/oj/object.c +775 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1723 -0
  32. data/ext/oj/oj.h +387 -0
  33. data/ext/oj/parse.c +1134 -0
  34. data/ext/oj/parse.h +112 -0
  35. data/ext/oj/rails.c +1528 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +924 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +534 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +155 -0
  73. data/pages/Options.md +287 -0
  74. data/pages/Rails.md +155 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/abstract_unit.rb +45 -0
  85. data/test/activesupport5/decoding_test.rb +133 -0
  86. data/test/activesupport5/encoding_test.rb +500 -0
  87. data/test/activesupport5/encoding_test_cases.rb +98 -0
  88. data/test/activesupport5/test_helper.rb +72 -0
  89. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  90. data/test/activesupport6/abstract_unit.rb +44 -0
  91. data/test/activesupport6/decoding_test.rb +133 -0
  92. data/test/activesupport6/encoding_test.rb +507 -0
  93. data/test/activesupport6/encoding_test_cases.rb +98 -0
  94. data/test/activesupport6/test_common.rb +17 -0
  95. data/test/activesupport6/test_helper.rb +163 -0
  96. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  97. data/test/bar.rb +35 -0
  98. data/test/baz.rb +16 -0
  99. data/test/files.rb +29 -0
  100. data/test/foo.rb +52 -0
  101. data/test/helper.rb +26 -0
  102. data/test/isolated/shared.rb +308 -0
  103. data/test/isolated/test_mimic_after.rb +13 -0
  104. data/test/isolated/test_mimic_alone.rb +12 -0
  105. data/test/isolated/test_mimic_as_json.rb +45 -0
  106. data/test/isolated/test_mimic_before.rb +13 -0
  107. data/test/isolated/test_mimic_define.rb +28 -0
  108. data/test/isolated/test_mimic_rails_after.rb +22 -0
  109. data/test/isolated/test_mimic_rails_before.rb +21 -0
  110. data/test/isolated/test_mimic_redefine.rb +15 -0
  111. data/test/json_gem/json_addition_test.rb +216 -0
  112. data/test/json_gem/json_common_interface_test.rb +148 -0
  113. data/test/json_gem/json_encoding_test.rb +107 -0
  114. data/test/json_gem/json_ext_parser_test.rb +20 -0
  115. data/test/json_gem/json_fixtures_test.rb +35 -0
  116. data/test/json_gem/json_generator_test.rb +383 -0
  117. data/test/json_gem/json_generic_object_test.rb +90 -0
  118. data/test/json_gem/json_parser_test.rb +470 -0
  119. data/test/json_gem/json_string_matching_test.rb +42 -0
  120. data/test/json_gem/test_helper.rb +18 -0
  121. data/test/perf.rb +107 -0
  122. data/test/perf_compat.rb +130 -0
  123. data/test/perf_fast.rb +164 -0
  124. data/test/perf_file.rb +64 -0
  125. data/test/perf_object.rb +138 -0
  126. data/test/perf_saj.rb +109 -0
  127. data/test/perf_scp.rb +151 -0
  128. data/test/perf_simple.rb +287 -0
  129. data/test/perf_strict.rb +145 -0
  130. data/test/perf_wab.rb +131 -0
  131. data/test/prec.rb +23 -0
  132. data/test/sample.rb +54 -0
  133. data/test/sample/change.rb +14 -0
  134. data/test/sample/dir.rb +19 -0
  135. data/test/sample/doc.rb +36 -0
  136. data/test/sample/file.rb +48 -0
  137. data/test/sample/group.rb +16 -0
  138. data/test/sample/hasprops.rb +16 -0
  139. data/test/sample/layer.rb +12 -0
  140. data/test/sample/line.rb +20 -0
  141. data/test/sample/oval.rb +10 -0
  142. data/test/sample/rect.rb +10 -0
  143. data/test/sample/shape.rb +35 -0
  144. data/test/sample/text.rb +20 -0
  145. data/test/sample_json.rb +37 -0
  146. data/test/test_compat.rb +502 -0
  147. data/test/test_custom.rb +527 -0
  148. data/test/test_debian.rb +53 -0
  149. data/test/test_fast.rb +470 -0
  150. data/test/test_file.rb +239 -0
  151. data/test/test_gc.rb +49 -0
  152. data/test/test_hash.rb +29 -0
  153. data/test/test_integer_range.rb +72 -0
  154. data/test/test_null.rb +376 -0
  155. data/test/test_object.rb +1027 -0
  156. data/test/test_rails.rb +26 -0
  157. data/test/test_saj.rb +186 -0
  158. data/test/test_scp.rb +433 -0
  159. data/test/test_strict.rb +433 -0
  160. data/test/test_various.rb +719 -0
  161. data/test/test_wab.rb +307 -0
  162. data/test/test_writer.rb +380 -0
  163. data/test/tests.rb +25 -0
  164. data/test/tests_mimic.rb +14 -0
  165. data/test/tests_mimic_addition.rb +7 -0
  166. data/test/zoo.rb +13 -0
  167. metadata +381 -0
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'oj'
4
+
5
+ extras = {"locationLng" => -97.14690769100295}
6
+
7
+ Oj.default_options = {float_precision: 17}
8
+
9
+ encoded = Oj.dump(extras)
10
+ puts encoded
11
+ puts Oj.load(encoded)
12
+
13
+ require "active_record"
14
+
15
+ Oj::Rails.set_encoder()
16
+ Oj::Rails.set_decoder()
17
+
18
+ Oj.default_options = {float_precision: 17}
19
+ # Using Oj rails encoder, gets the correct value: {"locationLng":-97.14690769100295}
20
+ encoded = ActiveSupport::JSON.encode(extras)
21
+ puts encoded
22
+ puts ActiveSupport::JSON.decode(encoded)
23
+ puts Oj.load(encoded)
@@ -0,0 +1,54 @@
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/doc'
12
+
13
+ def sample_doc(size=3)
14
+ colors = [ :black, :gray, :white, :red, :blue, :yellow, :green, :purple, :orange ]
15
+
16
+ d = ::Sample::Doc.new('Sample')
17
+
18
+ # add some history
19
+ (0..size * 10).each do |i|
20
+ d.add_change("Changed at t+#{i}.")
21
+ end
22
+
23
+ # add some layers
24
+ (1..size).each do |i|
25
+ layer = ::Sample::Layer.new("Layer-#{i}")
26
+ (1..size).each do |j|
27
+ g = ::Sample::Group.new
28
+ (1..size).each do |k|
29
+ g2 = ::Sample::Group.new
30
+ r = ::Sample::Rect.new(j * 40 + 10.0, i * 10.0,
31
+ 10.123456 / k, 10.0 / k, colors[(i + j + k) % colors.size])
32
+ r.add_prop(:part_of, layer.name)
33
+ g2 << r
34
+ g2 << ::Sample::Text.new("#{k} in #{j}", r.left, r.top, r.width, r.height)
35
+ g << g2
36
+ end
37
+ g2 = ::Sample::Group.new
38
+ (1..size).each do |k|
39
+ o = ::Sample::Oval.new(j * 40 + 12.0, i * 10.0 + 2.0,
40
+ 6.0 / k, 6.0 / k, colors[(i + j + k) % colors.size])
41
+ o.add_prop(:inside, true)
42
+ g << o
43
+ end
44
+ g << g2
45
+ layer << g
46
+ end
47
+ d.layers[layer.name] = layer
48
+ end
49
+
50
+ # some properties
51
+ d.add_prop(:purpose, 'an example')
52
+
53
+ d
54
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Sample
3
+ class Change
4
+ attr_accessor :time
5
+ attr_accessor :user
6
+ attr_accessor :comment
7
+
8
+ def initialize(comment=nil, time=nil, user=nil)
9
+ @user = user || ENV['USER']
10
+ @time = time || Time.now
11
+ @comment = comment
12
+ end
13
+ end # Change
14
+ end # Sample
@@ -0,0 +1,19 @@
1
+
2
+ require 'etc'
3
+
4
+ module Sample
5
+
6
+ class Dir < File
7
+ attr_accessor :files
8
+
9
+ def initialize(filename)
10
+ super
11
+ @files = []
12
+ end
13
+
14
+ def <<(f)
15
+ @files << f
16
+ end
17
+
18
+ end # Dir
19
+ end # Sample
@@ -0,0 +1,36 @@
1
+
2
+ require 'sample/hasprops'
3
+ require 'sample/group'
4
+ require 'sample/layer'
5
+ require 'sample/line'
6
+ require 'sample/shape'
7
+ require 'sample/oval'
8
+ require 'sample/rect'
9
+ require 'sample/text'
10
+ require 'sample/change'
11
+
12
+ module Sample
13
+ class Doc
14
+ include HasProps
15
+
16
+ attr_accessor :title
17
+ attr_accessor :create_time
18
+ attr_accessor :user
19
+ # Hash of layers in the document indexed by layer name.
20
+ attr_reader :layers
21
+ attr_reader :change_history
22
+
23
+ def initialize(title)
24
+ @title = title
25
+ @user = ENV['USER']
26
+ @create_time = Time.now
27
+ @layers = { }
28
+ @change_history = []
29
+ end
30
+
31
+ def add_change(comment, time=nil, user=nil)
32
+ @change_history << Change.new(comment, time, user)
33
+ end
34
+
35
+ end # Doc
36
+ end # Sample
@@ -0,0 +1,48 @@
1
+
2
+ require 'etc'
3
+
4
+ module Sample
5
+
6
+ class File
7
+ attr_accessor :name, :ctime, :mtime, :size, :owner, :group, :permissions
8
+
9
+ def initialize(filename)
10
+ @name = ::File.basename(filename)
11
+ stat = ::File.stat(filename)
12
+ @ctime = stat.ctime
13
+ @mtime = stat.mtime
14
+ @size = stat.size
15
+ @owner = Etc.getpwuid(stat.uid).name
16
+ @group = Etc.getgrgid(stat.gid).name
17
+ if false
18
+ @permissions = {
19
+ 'user' => {
20
+ 'read' => (0 != (stat.mode & 0x0100)),
21
+ 'write' => (0 != (stat.mode & 0x0080)),
22
+ 'execute' => (0 != (stat.mode & 0x0040))},
23
+ 'group' => {
24
+ 'read' => (0 != (stat.mode & 0x0020)),
25
+ 'write' => (0 != (stat.mode & 0x0010)),
26
+ 'execute' => (0 != (stat.mode & 0x0008))},
27
+ 'other' => {
28
+ 'read' => (0 != (stat.mode & 0x0004)),
29
+ 'write' => (0 != (stat.mode & 0x0002)),
30
+ 'execute' => (0 != (stat.mode & 0x0001))}
31
+ }
32
+ else
33
+ @permissions = {
34
+ 'user' => [(0 != (stat.mode & 0x0100)) ? 'r' : '-',
35
+ (0 != (stat.mode & 0x0080)) ? 'w' : '-',
36
+ (0 != (stat.mode & 0x0040)) ? 'x' : '-'].join(''),
37
+ 'group' => [(0 != (stat.mode & 0x0020)) ? 'r' : '-',
38
+ (0 != (stat.mode & 0x0010)) ? 'w' : '-',
39
+ (0 != (stat.mode & 0x0008)) ? 'x' : '-'].join(''),
40
+ 'other' => [(0 != (stat.mode & 0x0004)) ? 'r' : '-',
41
+ (0 != (stat.mode & 0x0002)) ? 'w' : '-',
42
+ (0 != (stat.mode & 0x0001)) ? 'x' : '-'].join('')
43
+ }
44
+ end
45
+ end
46
+
47
+ end # File
48
+ end # Sample
@@ -0,0 +1,16 @@
1
+
2
+ module Sample
3
+ class Group
4
+ attr_reader :members
5
+
6
+ def initialize()
7
+ @members = []
8
+ end
9
+
10
+ def <<(member)
11
+ @members << member
12
+ end
13
+
14
+ end # Group
15
+ end # Sample
16
+
@@ -0,0 +1,16 @@
1
+
2
+ module Sample
3
+ module HasProps
4
+
5
+ def add_prop(key, value)
6
+ @props = { } unless self.instance_variable_defined?(:@props)
7
+ @props[key] = value
8
+ end
9
+
10
+ def props
11
+ @props = { } unless self.instance_variable_defined?(:@props)
12
+ @props
13
+ end
14
+
15
+ end # HasProps
16
+ end # Sample
@@ -0,0 +1,12 @@
1
+
2
+ module Sample
3
+ class Layer < Group
4
+ attr_accessor :name
5
+
6
+ def initialize(name)
7
+ super()
8
+ @name = name
9
+ end
10
+
11
+ end # Layer
12
+ end # Sample
@@ -0,0 +1,20 @@
1
+ module Sample
2
+
3
+ class Line
4
+ include HasProps
5
+
6
+ attr_accessor :x, :y, :dx, :dy
7
+ attr_accessor :color
8
+ attr_accessor :thick
9
+
10
+ def initialize(x, y, dx, dy, thick, color)
11
+ @x = x
12
+ @y = y
13
+ @dx = dx
14
+ @dy = dy
15
+ @thick = thick
16
+ @color = color
17
+ end
18
+
19
+ end # Line
20
+ end # Sample
@@ -0,0 +1,10 @@
1
+ module Sample
2
+
3
+ class Oval < Shape
4
+
5
+ def initialize(left, top, wide, high, color=nil)
6
+ super
7
+ end
8
+
9
+ end # Oval
10
+ end # Sample
@@ -0,0 +1,10 @@
1
+
2
+ module Sample
3
+ class Rect < Shape
4
+
5
+ def initialize(left, top, wide, high, color=nil)
6
+ super
7
+ end
8
+
9
+ end # Rect
10
+ end # Sample
@@ -0,0 +1,35 @@
1
+
2
+ module Sample
3
+ class Shape
4
+ include HasProps
5
+
6
+ attr_accessor :bounds
7
+ attr_accessor :color
8
+ attr_accessor :border, :border_color
9
+
10
+ def initialize(left, top, wide, high, color=nil)
11
+ @bounds = [[left, top], [left + wide, top + high]]
12
+ @color = color
13
+ @border = 1
14
+ @border_color = :black
15
+ end
16
+
17
+ def left
18
+ @bounds[0][0]
19
+ end
20
+
21
+ def top
22
+ @bounds[0][1]
23
+ end
24
+
25
+ def width
26
+ @bounds[1][0] - @bounds[0][0]
27
+ end
28
+
29
+ def height
30
+ @bounds[1][1] - @bounds[0][1]
31
+ end
32
+
33
+ end # Shape
34
+ end # Sample
35
+
@@ -0,0 +1,20 @@
1
+
2
+ module Sample
3
+ class Text < Shape
4
+ attr_accessor :text
5
+ attr_accessor :font
6
+ attr_accessor :font_size
7
+ attr_accessor :just
8
+ attr_accessor :text_color
9
+
10
+ def initialize(text, left, top, wide, high, color=nil)
11
+ super(left, top, wide, high, color)
12
+ @text = text
13
+ @font = 'helvetica'
14
+ @font_size = 14
15
+ @just = 'left'
16
+ @text_color = 'black'
17
+ end
18
+
19
+ end # Text
20
+ end # Sample
@@ -0,0 +1,37 @@
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 'oj'
12
+
13
+ def sample_json(size=3)
14
+ colors = [ :black, :gray, :white, :red, :blue, :yellow, :green, :purple, :orange ]
15
+ container = []
16
+ size.times do |i|
17
+ box = {
18
+ 'color' => colors[i % colors.size],
19
+ 'fragile' => (0 == (i % 2)),
20
+ 'width' => i,
21
+ 'height' => i,
22
+ 'depth' => i,
23
+ 'weight' => i * 1.3,
24
+ 'address' => {
25
+ 'street' => "#{i} Main Street",
26
+ 'city' => 'Sity',
27
+ 'state' => nil
28
+ }
29
+ }
30
+ container << box
31
+ end
32
+ container
33
+ end
34
+
35
+ if $0 == __FILE__
36
+ File.open('sample.json', "w") { |f| f.write(Oj.dump(sample_json(3), :indent => 2)) }
37
+ end
@@ -0,0 +1,502 @@
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 'minitest'
11
+ require 'minitest/autorun'
12
+ require 'stringio'
13
+ require 'date'
14
+ require 'bigdecimal'
15
+ require 'oj'
16
+
17
+ class CompatJuice < Minitest::Test
18
+
19
+ class Jeez
20
+ attr_accessor :x, :y
21
+
22
+ def initialize(x, y)
23
+ @x = x
24
+ @y = y
25
+ end
26
+
27
+ def eql?(o)
28
+ self.class == o.class && @x == o.x && @y == o.y
29
+ end
30
+ alias == eql?
31
+
32
+ def to_json(*a)
33
+ %|{"json_class":"#{self.class.to_s}","x":#{@x},"y":#{@y}}|
34
+ end
35
+
36
+ def self.json_create(h)
37
+ self.new(h['x'], h['y'])
38
+ end
39
+ end # Jeez
40
+
41
+ class Argy
42
+ def initialize()
43
+ end
44
+
45
+ def to_json(*a)
46
+ %|{"args":"#{a}"}|
47
+ end
48
+ end # Argy
49
+
50
+ class Stringy
51
+ def initialize()
52
+ end
53
+
54
+ def to_s()
55
+ %|[1,2]|
56
+ end
57
+ end # Stringy
58
+
59
+ module One
60
+ module Two
61
+ module Three
62
+ class Deep
63
+
64
+ def initialize()
65
+ end
66
+
67
+ def eql?(o)
68
+ self.class == o.class
69
+ end
70
+ alias == eql?
71
+
72
+ def to_json(*a)
73
+ %|{"json_class":"#{self.class.name}"}|
74
+ end
75
+
76
+ def self.json_create(h)
77
+ self.new()
78
+ end
79
+ end # Deep
80
+ end # Three
81
+ end # Two
82
+ end # One
83
+
84
+ def setup
85
+ @default_options = Oj.default_options
86
+ # in compat mode other options other than the JSON gem globals and options
87
+ # are not used.
88
+ Oj.default_options = { :mode => :compat }
89
+ end
90
+
91
+ def teardown
92
+ Oj.default_options = @default_options
93
+ end
94
+
95
+ def test_nil
96
+ dump_and_load(nil, false)
97
+ end
98
+
99
+ def test_true
100
+ dump_and_load(true, false)
101
+ end
102
+
103
+ def test_false
104
+ dump_and_load(false, false)
105
+ end
106
+
107
+ def test_fixnum
108
+ dump_and_load(0, false)
109
+ dump_and_load(12345, false)
110
+ dump_and_load(-54321, false)
111
+ dump_and_load(1, false)
112
+ end
113
+
114
+ def test_float
115
+ dump_and_load(0.0, false)
116
+ dump_and_load(0.56, false)
117
+ dump_and_load(3.0, false)
118
+ dump_and_load(12345.6789, false)
119
+ dump_and_load(70.35, false)
120
+ dump_and_load(-54321.012, false)
121
+ dump_and_load(1.7775, false)
122
+ dump_and_load(2.5024, false)
123
+ dump_and_load(2.48e16, false)
124
+ dump_and_load(2.48e100 * 1.0e10, false)
125
+ dump_and_load(-2.48e100 * 1.0e10, false)
126
+ dump_and_load(1405460727.723866, false)
127
+ dump_and_load(0.5773, false)
128
+ dump_and_load(0.6768, false)
129
+ dump_and_load(0.685, false)
130
+ dump_and_load(0.7032, false)
131
+ dump_and_load(0.7051, false)
132
+ dump_and_load(0.8274, false)
133
+ dump_and_load(0.9149, false)
134
+ dump_and_load(64.4, false)
135
+ dump_and_load(71.6, false)
136
+ dump_and_load(73.4, false)
137
+ dump_and_load(80.6, false)
138
+ dump_and_load(-95.640172, false)
139
+ end
140
+
141
+ def test_string
142
+ dump_and_load('', false)
143
+ dump_and_load('abc', false)
144
+ dump_and_load("abc\ndef", false)
145
+ dump_and_load("a\u0041", false)
146
+ end
147
+
148
+ def test_encode
149
+ opts = Oj.default_options
150
+ Oj.default_options = { :ascii_only => true }
151
+ json = Oj.dump("ぴーたー")
152
+ assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
153
+ Oj.default_options = opts
154
+ end
155
+
156
+ def test_unicode
157
+ # hits the 3 normal ranges and one extended surrogate pair
158
+ json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
159
+ obj = Oj.load(json)
160
+ json2 = Oj.dump(obj, :ascii_only => true)
161
+ assert_equal(json, json2)
162
+ end
163
+
164
+ def test_array
165
+ dump_and_load([], false)
166
+ dump_and_load([true, false], false)
167
+ dump_and_load(['a', 1, nil], false)
168
+ dump_and_load([[nil]], false)
169
+ dump_and_load([[nil], 58], false)
170
+ end
171
+
172
+ def test_array_deep
173
+ dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
174
+ end
175
+
176
+ def test_symbol
177
+ json = Oj.dump(:abc, :mode => :compat)
178
+ assert_equal('"abc"', json)
179
+ end
180
+
181
+ def test_time
182
+ t = Time.xmlschema("2012-01-05T23:58:07.123456000+09:00")
183
+ #t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
184
+ json = Oj.dump(t, :mode => :compat)
185
+ assert_equal(%{"2012-01-05 23:58:07 +0900"}, json)
186
+ end
187
+
188
+ def test_class
189
+ json = Oj.dump(CompatJuice, :mode => :compat)
190
+ assert_equal(%{"CompatJuice"}, json)
191
+ end
192
+
193
+ def test_module
194
+ json = Oj.dump(One::Two, :mode => :compat)
195
+ assert_equal(%{"CompatJuice::One::Two"}, json)
196
+ end
197
+
198
+ # Hash
199
+ def test_non_str_hash
200
+ json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
201
+ h = Oj.load(json, :mode => :strict)
202
+ assert_equal({ "1" => true, "0" => false }, h)
203
+ end
204
+ def test_hash
205
+ dump_and_load({}, false)
206
+ dump_and_load({ 'true' => true, 'false' => false}, false)
207
+ dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
208
+ end
209
+
210
+ def test_hash_deep
211
+ dump_and_load({'1' => {
212
+ '2' => {
213
+ '3' => {
214
+ '4' => {
215
+ '5' => {
216
+ '6' => {
217
+ '7' => {
218
+ '8' => {
219
+ '9' => {
220
+ '10' => {
221
+ '11' => {
222
+ '12' => {
223
+ '13' => {
224
+ '14' => {
225
+ '15' => {
226
+ '16' => {
227
+ '17' => {
228
+ '18' => {
229
+ '19' => {
230
+ '20' => {}}}}}}}}}}}}}}}}}}}}}, false)
231
+ end
232
+
233
+ def test_hash_escaped_key
234
+ json = %{{"a\nb":true,"c\td":false}}
235
+ obj = Oj.compat_load(json)
236
+ assert_equal({"a\nb" => true, "c\td" => false}, obj)
237
+ end
238
+
239
+ def test_hash_escaping
240
+ json = Oj.to_json({'<>' => '<>'}, mode: :compat)
241
+ assert_equal(json, '{"<>":"<>"}')
242
+ end
243
+
244
+ def test_bignum_object
245
+ dump_and_load(7 ** 55, false)
246
+ end
247
+
248
+ def test_json_object
249
+ obj = Jeez.new(true, 58)
250
+ json = Oj.to_json(obj)
251
+ assert(%|{"json_class":"CompatJuice::Jeez","x":true,"y":58}| == json ||
252
+ %|{"json_class":"CompatJuice::Jeez","y":58,"x":true}| == json)
253
+ dump_to_json_and_load(obj, false)
254
+ end
255
+
256
+ def test_json_object_create_id
257
+ Oj.default_options = { :create_id => 'kson_class', :create_additions => true}
258
+ expected = Jeez.new(true, 58)
259
+ json = %{{"kson_class":"CompatJuice::Jeez","x":true,"y":58}}
260
+ obj = Oj.load(json)
261
+ assert_equal(expected, obj)
262
+ Oj.default_options = { :create_id => 'json_class' }
263
+ end
264
+
265
+ def test_bignum_compat
266
+ json = Oj.dump(7 ** 55, :mode => :compat)
267
+ b = Oj.load(json, :mode => :strict)
268
+ assert_equal(30226801971775055948247051683954096612865741943, b)
269
+ end
270
+
271
+ # BigDecimal
272
+ def test_bigdecimal
273
+ # BigDecimals are dumped as strings and can not be restored to the
274
+ # original value.
275
+ json = Oj.dump(BigDecimal('3.14159265358979323846'))
276
+ # 2.4.0 changes the exponent to lowercase
277
+ assert_equal('"0.314159265358979323846e1"', json.downcase)
278
+ end
279
+
280
+ def test_infinity
281
+ assert_raises(Oj::ParseError) { Oj.load('Infinity', :mode => :strict) }
282
+ x = Oj.load('Infinity', :mode => :compat)
283
+ assert_equal('Infinity', x.to_s)
284
+ end
285
+
286
+ # Time
287
+ def test_time
288
+ t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
289
+ expect = '"' + t.to_s + '"'
290
+ json = Oj.dump(t)
291
+ assert_equal(expect, json)
292
+ end
293
+
294
+ def test_date_compat
295
+ orig = Date.new(2012, 6, 19)
296
+ json = Oj.dump(orig, :mode => :compat)
297
+ x = Oj.load(json, :mode => :compat)
298
+ # Some Rubies implement Date as data and some as a real Object. Either are
299
+ # okay for the test.
300
+ if x.is_a?(String)
301
+ assert_equal(orig.to_s, x)
302
+ else # better be a Hash
303
+ assert_equal({"year" => orig.year, "month" => orig.month, "day" => orig.day, "start" => orig.start}, x)
304
+ end
305
+ end
306
+
307
+ def test_datetime_compat
308
+ orig = DateTime.new(2012, 6, 19, 20, 19, 27)
309
+ json = Oj.dump(orig, :mode => :compat)
310
+ x = Oj.load(json, :mode => :compat)
311
+ # Some Rubies implement Date as data and some as a real Object. Either are
312
+ # okay for the test.
313
+ assert_equal(orig.to_s, x)
314
+ end
315
+
316
+ # Stream IO
317
+ def test_io_string
318
+ json = %{{
319
+ "x":true,
320
+ "y":58,
321
+ "z": [1,2,3]
322
+ }
323
+ }
324
+ input = StringIO.new(json)
325
+ obj = Oj.compat_load(input)
326
+ assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
327
+ end
328
+
329
+ def test_io_file
330
+ filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
331
+ File.open(filename, 'w') { |f| f.write(%{{
332
+ "x":true,
333
+ "y":58,
334
+ "z": [1,2,3]
335
+ }
336
+ }) }
337
+ f = File.new(filename)
338
+ obj = Oj.compat_load(f)
339
+ f.close()
340
+ assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
341
+ end
342
+
343
+ # symbol_keys option
344
+ def test_symbol_keys
345
+ json = %{{
346
+ "x":true,
347
+ "y":58,
348
+ "z": [1,2,3]
349
+ }
350
+ }
351
+ obj = Oj.compat_load(json, :symbol_keys => true)
352
+ assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
353
+ end
354
+
355
+ # comments
356
+ def test_comment_slash
357
+ json = %{{
358
+ "x":true,//three
359
+ "y":58,
360
+ "z": [1,2,
361
+ 3 // six
362
+ ]}
363
+ }
364
+ obj = Oj.compat_load(json)
365
+ assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
366
+ end
367
+
368
+ def test_comment_c
369
+ json = %{{
370
+ "x"/*one*/:/*two*/true,
371
+ "y":58,
372
+ "z": [1,2,3]}
373
+ }
374
+ obj = Oj.compat_load(json)
375
+ assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
376
+ end
377
+
378
+ def test_comment
379
+ json = %{{
380
+ "x"/*one*/:/*two*/true,//three
381
+ "y":58/*four*/,
382
+ "z": [1,2/*five*/,
383
+ 3 // six
384
+ ]
385
+ }
386
+ }
387
+ obj = Oj.compat_load(json)
388
+ assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
389
+ end
390
+
391
+ # If mimic_JSON has not been called then Oj.dump will call to_json on the
392
+ # top level object only.
393
+ def test_json_object_top
394
+ obj = Jeez.new(true, 58)
395
+ dump_to_json_and_load(obj, false)
396
+ end
397
+
398
+ # A child to_json should not be called.
399
+ def test_json_object_child
400
+ obj = { "child" => Jeez.new(true, 58) }
401
+ assert_equal('{"child":{"json_class":"CompatJuice::Jeez","x":true,"y":58}}', Oj.dump(obj))
402
+ end
403
+
404
+ def test_json_module_object
405
+ obj = One::Two::Three::Deep.new()
406
+ dump_to_json_and_load(obj, false)
407
+ end
408
+
409
+ def test_json_object_dump_create_id
410
+ expected = Jeez.new(true, 58)
411
+ json = Oj.to_json(expected)
412
+ obj = Oj.compat_load(json, :create_additions => true)
413
+ assert_equal(expected, obj)
414
+ end
415
+
416
+ def test_json_object_bad
417
+ json = %{{"json_class":"CompatJuice::Junk","x":true}}
418
+ begin
419
+ Oj.compat_load(json, :create_additions => true)
420
+ rescue Exception => e
421
+ assert_equal("ArgumentError", e.class().name)
422
+ return
423
+ end
424
+ assert(false, "*** expected an exception")
425
+ end
426
+
427
+ def test_json_object_create_cache
428
+ expected = Jeez.new(true, 58)
429
+ json = Oj.to_json(expected)
430
+ obj = Oj.compat_load(json, :class_cache => true, :create_additions => true)
431
+ assert_equal(expected, obj)
432
+ obj = Oj.compat_load(json, :class_cache => false, :create_additions => true)
433
+ assert_equal(expected, obj)
434
+ end
435
+
436
+ def test_json_object_create_id_other
437
+ expected = Jeez.new(true, 58)
438
+ json = Oj.to_json(expected)
439
+ json.gsub!('json_class', '_class_')
440
+ obj = Oj.compat_load(json, :create_id => "_class_", :create_additions => true)
441
+ assert_equal(expected, obj)
442
+ end
443
+
444
+ def test_json_object_create_deep
445
+ expected = One::Two::Three::Deep.new()
446
+ json = Oj.to_json(expected)
447
+ obj = Oj.compat_load(json, :create_additions => true)
448
+ assert_equal(expected, obj)
449
+ end
450
+
451
+ def test_range
452
+ json = Oj.dump(1..7)
453
+ assert_equal('"1..7"', json)
454
+ end
455
+
456
+ def test_arg_passing
457
+ json = Oj.to_json(Argy.new(), :max_nesting=> 40)
458
+ assert_equal(%|{"args":"[{:max_nesting=>40}]"}|, json)
459
+ end
460
+
461
+ def test_bad_unicode
462
+ assert_raises() { Oj.to_json("\xE4xy") }
463
+ end
464
+
465
+ def test_bad_unicode_e2
466
+ assert_raises() { Oj.to_json("L\xE2m ") }
467
+ end
468
+
469
+ def test_bad_unicode_start
470
+ assert_raises() { Oj.to_json("\x8abc") }
471
+ end
472
+
473
+ def test_parse_to_s
474
+ s = Stringy.new
475
+ assert_equal([1,2], Oj.load(s, :mode => :compat))
476
+ end
477
+
478
+ def dump_and_load(obj, trace=false)
479
+ json = Oj.dump(obj)
480
+ puts json if trace
481
+ loaded = Oj.compat_load(json, :create_additions => true);
482
+ if obj.nil?
483
+ assert_nil(loaded)
484
+ else
485
+ assert_equal(obj, loaded)
486
+ end
487
+ loaded
488
+ end
489
+
490
+ def dump_to_json_and_load(obj, trace=false)
491
+ json = Oj.to_json(obj, :indent => ' ')
492
+ puts json if trace
493
+ loaded = Oj.compat_load(json, :create_additions => true);
494
+ if obj.nil?
495
+ assert_nil(loaded)
496
+ else
497
+ assert_equal(obj, loaded)
498
+ end
499
+ loaded
500
+ end
501
+
502
+ end