oj 2.10.4 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  #!/usr/bin/env ruby
2
4
  # encoding: UTF-8
3
5
 
@@ -5,60 +7,47 @@ $: << File.dirname(__FILE__)
5
7
 
6
8
  require 'helper'
7
9
 
8
- require 'oj'
9
- require 'securerandom'
10
-
11
10
  class Handler
12
- def hash_start() {} end
13
- def hash_set(h,k,v) h.store(k,v) end
14
- def array_start() [] end
15
- def array_append(a,v) a << v end
16
- def error(message, line, column)
17
- raise Exception.new(message, line, column)
11
+ def initialize
12
+ @state = []
13
+ end
14
+
15
+ def hash_start
16
+ @state << {}
17
+ @state.last
18
+ end
19
+
20
+ def hash_end
21
+ @state.pop
22
+ end
23
+
24
+ def hash_set(h,k,v)
25
+ h.store(k,v)
18
26
  end
19
- end
20
27
 
21
- json = Oj.dump({"this"=>"object"})
22
-
23
- if true
24
- name = "/tmp/#{SecureRandom.uuid}"
25
- `mkfifo #{name}`
26
- if fork
27
- open(name, 'r+') do |read_io|
28
- p "start reading #{read_io.stat.ftype}"
29
- Oj.sc_parse(Handler.new, read_io) {|v| p v}
30
- p "stop reading"
31
- end
32
- else
33
- open(name, 'w+') do |write_io|
34
- p "start writing #{write_io.stat.ftype} autoclose: #{write_io.autoclose?}"
35
- write_io.write json
36
- write_io.write json
37
- p "stop writing"
38
- end
39
- sleep(1) # make it obvious that there are two threads
40
- open(name, 'w+') do |write_io|
41
- p "start writing #{write_io.stat.ftype}"
42
- write_io.write json
43
- write_io.write json
44
- p "stop writing"
45
- end
28
+ def array_start
29
+ @state << []
30
+ @state.last
46
31
  end
47
- else
48
- IO.pipe do |read_io, write_io|
49
- if fork
50
- write_io.close
51
- p "start reading #{read_io.stat.ftype}"
52
- Oj.sc_parse(Handler.new, read_io) {|v| p v}
53
- p "stop reading"
54
- read_io.close
55
- else
56
- read_io.close
57
- p "start writing #{write_io.stat.ftype}"
58
- write_io.write json
59
- write_io.write json
60
- p "stop writing"
61
- write_io.close
62
- end
32
+
33
+
34
+ def array_end
35
+ @state.pop
36
+ end
37
+
38
+ def array_append(a,v)
39
+ a << v
40
+ end
41
+
42
+ def add_value(v)
43
+ p v
63
44
  end
45
+
46
+ def error(message, line, column); p "ERROR: #{message}" end
64
47
  end
48
+
49
+ $handler = Handler.new
50
+
51
+ IO.popen("cat tst") { |p| puts Oj.sc_parse($handler, p) }
52
+
53
+ #File.open('tst', 'r') { |file| Oj.sc_parse($handler, file) }
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #!/usr/bin/env ruby
4
+ # encoding: UTF-8
5
+
6
+ $: << File.dirname(__FILE__)
7
+
8
+ require 'helper'
9
+
10
+
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ class Handler
9
+ def initialize
10
+ @state = []
11
+ end
12
+
13
+ def hash_start
14
+ @state << {}
15
+ @state.last
16
+ end
17
+
18
+ def hash_end
19
+ @state.pop
20
+ end
21
+
22
+ def hash_set(h,k,v)
23
+ h.store(k,v)
24
+ end
25
+
26
+ def array_start
27
+ @state << []
28
+ @state.last
29
+ end
30
+
31
+
32
+ def array_end
33
+ @state.pop
34
+ end
35
+
36
+ def array_append(a,v)
37
+ a << v
38
+ end
39
+
40
+ def error(message, line, column); p "ERROR: #{message}" end
41
+ end
42
+
43
+ handler = Handler.new
44
+ def handler.add_value(v)
45
+ p v
46
+ end
47
+
48
+ Oj.sc_parse(handler, StringIO.new('{"a":"b","c":[1,2,{"d":"e"}]}[4,5,6]'))
@@ -1,19 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: UTF-8
3
3
 
4
- $VERBOSE = true
5
-
6
4
  %w(lib ext test).each do |dir|
7
5
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
8
6
  end
9
7
 
10
- require 'rubygems' if RUBY_VERSION.start_with?('1.8.')
11
8
  require 'oj'
12
9
 
13
- Oj.mimic_JSON
14
-
15
- #puts Oj.default_options
16
-
17
- range = ("01".."12")
18
10
 
19
- puts Oj.dump(range)
11
+ Thread.new do
12
+ string_io = StringIO.new('{"foo":"bar"}')
13
+ Oj.load(string_io)
14
+ string_io.rewind
15
+ puts string_io.read
16
+ end.join
@@ -0,0 +1,29 @@
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 'oj'
14
+
15
+ A = Struct.new(:a,:b,:c,:d)
16
+ B = Struct.new(:e,:f)
17
+
18
+ obj = [A.new(55, B.new(1, 'X'), B.new(2, 'Y'), 3)]
19
+
20
+ s = Oj.dump(obj, :mode => :object)
21
+
22
+ 100000.times do
23
+ Oj.load(s, :mode => :object)
24
+ # ds = Oj.dump(o, :mode => :object)
25
+ # if ds != s
26
+ # puts ds
27
+ # raise "holy crap"
28
+ # end
29
+ end
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ %w(lib ext test).each do |dir|
7
+ $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
8
+ end
9
+
10
+ require 'minitest'
11
+ require 'minitest/autorun'
12
+ require 'oj'
13
+
14
+ Oj.mimic_JSON
15
+
16
+ require 'rails/all'
17
+ require 'active_model'
18
+ require 'active_model_serializers'
19
+ require 'active_support/json'
20
+
21
+ #Oj.mimic_JSON
22
+
23
+ class Category
24
+ include ActiveModel::Model
25
+ include ActiveModel::SerializerSupport
26
+
27
+ attr_accessor :id, :name
28
+
29
+ def initialize(id, name)
30
+ @id = id
31
+ @name = name
32
+ end
33
+ end
34
+
35
+ class CategorySerializer < ActiveModel::Serializer
36
+ attributes :id, :name
37
+ end
38
+
39
+ class MimicRails < Minitest::Test
40
+
41
+ def test_dump_object
42
+ Oj.default_options= {:indent => 0}
43
+ category = Category.new(1, 'test')
44
+ serializer = CategorySerializer.new(category)
45
+
46
+ json = serializer.to_json()
47
+ puts "*** serializer.to_json() #{serializer.to_json()}"
48
+ assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
49
+
50
+ json = serializer.as_json()
51
+ puts "*** serializer.as_json() #{serializer.as_json()}"
52
+ assert_equal({"category" => {:id => 1, :name => "test"}}, json)
53
+
54
+ json = JSON.dump(serializer)
55
+ puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
56
+ assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
57
+ end
58
+
59
+ end # MimicRails
@@ -7,6 +7,9 @@ require 'helper'
7
7
 
8
8
  class Juice < Minitest::Test
9
9
 
10
+ module TestModule
11
+ end
12
+
10
13
  class Jam
11
14
  attr_accessor :x, :y
12
15
 
@@ -87,57 +90,67 @@ class Juice < Minitest::Test
87
90
  Oj.default_options = @default_options
88
91
  end
89
92
 
90
- def test0_get_options
93
+ =begin
94
+ # Depending on the order the values may have changed. The set_options sets
95
+ # should cover the function itself.
96
+ def test_get_options
91
97
  opts = Oj.default_options()
92
98
  assert_equal({ :indent=>0,
93
99
  :second_precision=>9,
94
100
  :circular=>false,
101
+ :class_cache=>true,
95
102
  :auto_define=>false,
96
103
  :symbol_keys=>false,
97
- :class_cache=>true,
98
- :escape_mode=>:json,
99
- :mode=>:object,
100
- :time_format=>:unix,
101
104
  :bigdecimal_as_decimal=>true,
102
- :bigdecimal_load=>:auto,
103
- :use_to_json=>false,
105
+ :use_to_json=>true,
104
106
  :nilnil=>false,
105
107
  :allow_gc=>true,
108
+ :quirks_mode=>true,
109
+ :float_precision=>15,
110
+ :mode=>:object,
111
+ :escape_mode=>:json,
112
+ :time_format=>:unix,
113
+ :bigdecimal_load=>:auto,
106
114
  :create_id=>'json_class'}, opts)
107
115
  end
116
+ =end
108
117
 
109
- def test0_set_options
110
- orig = {
118
+ def test_set_options
119
+ orig ={
111
120
  :indent=>0,
112
121
  :second_precision=>9,
113
122
  :circular=>false,
123
+ :class_cache=>true,
114
124
  :auto_define=>false,
115
125
  :symbol_keys=>false,
116
- :class_cache=>true,
117
- :escape_mode=>:ascii,
118
- :mode=>:object,
119
- :time_format=>:unix,
120
126
  :bigdecimal_as_decimal=>true,
121
- :bigdecimal_load=>:auto,
122
127
  :use_to_json=>true,
123
128
  :nilnil=>false,
124
129
  :allow_gc=>true,
130
+ :quirks_mode=>true,
131
+ :float_precision=>15,
132
+ :mode=>:object,
133
+ :escape_mode=>:json,
134
+ :time_format=>:unix,
135
+ :bigdecimal_load=>:auto,
125
136
  :create_id=>'json_class'}
126
137
  o2 = {
127
138
  :indent=>4,
128
139
  :second_precision=>7,
129
140
  :circular=>true,
141
+ :class_cache=>false,
130
142
  :auto_define=>true,
131
143
  :symbol_keys=>true,
132
- :class_cache=>false,
133
- :escape_mode=>:json,
134
- :mode=>:compat,
135
- :time_format=>:ruby,
136
144
  :bigdecimal_as_decimal=>false,
137
- :bigdecimal_load=>:bigdecimal,
138
145
  :use_to_json=>false,
139
146
  :nilnil=>true,
140
147
  :allow_gc=>false,
148
+ :quirks_mode=>true,
149
+ :float_precision=>15,
150
+ :mode=>:compat,
151
+ :escape_mode=>:json,
152
+ :time_format=>:ruby,
153
+ :bigdecimal_load=>:bigdecimal,
141
154
  :create_id=>nil}
142
155
  o3 = { :indent => 4 }
143
156
  Oj.default_options = o2
@@ -166,6 +179,33 @@ class Juice < Minitest::Test
166
179
  dump_and_load(1, false)
167
180
  end
168
181
 
182
+ def test_float_dump
183
+ Oj.default_options = { :float_precision => 16 }
184
+ assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
185
+ Oj.default_options = { :float_precision => 5 }
186
+ assert_equal('1.4055', Oj.dump(1.405460727))
187
+ Oj.default_options = { :float_precision => 0 }
188
+ if RUBY_VERSION.start_with?('1.8')
189
+ assert_equal('1405460727.72387', Oj.dump(1405460727.723866))
190
+ else
191
+ assert_equal('1405460727.723866', Oj.dump(1405460727.723866))
192
+ end
193
+ Oj.default_options = { :float_precision => 15 }
194
+ assert_equal('0.56', Oj.dump(0.56))
195
+ assert_equal('0.5773', Oj.dump(0.5773))
196
+ assert_equal('0.6768', Oj.dump(0.6768))
197
+ assert_equal('0.685', Oj.dump(0.685))
198
+ assert_equal('0.7032', Oj.dump(0.7032))
199
+ assert_equal('0.7051', Oj.dump(0.7051))
200
+ assert_equal('0.8274', Oj.dump(0.8274))
201
+ assert_equal('0.9149', Oj.dump(0.9149))
202
+ assert_equal('64.4', Oj.dump(64.4))
203
+ assert_equal('71.6', Oj.dump(71.6))
204
+ assert_equal('73.4', Oj.dump(73.4))
205
+ assert_equal('80.6', Oj.dump(80.6))
206
+ assert_equal('-95.640172', Oj.dump(-95.640172))
207
+ end
208
+
169
209
  def test_float
170
210
  mode = Oj.default_options()[:mode]
171
211
  Oj.default_options = {:mode => :object}
@@ -486,6 +526,29 @@ class Juice < Minitest::Test
486
526
  dump_and_load(Juice, false)
487
527
  end
488
528
 
529
+ # Module
530
+ def test_module_strict
531
+ begin
532
+ Oj.dump(TestModule, :mode => :strict)
533
+ rescue Exception
534
+ assert(true)
535
+ return
536
+ end
537
+ assert(false, "*** expected an exception")
538
+ end
539
+ def test_module_null
540
+ json = Oj.dump(TestModule, :mode => :null)
541
+ assert_equal('null', json)
542
+ end
543
+ def test_module_compat
544
+ json = Oj.dump(TestModule, :mode => :compat)
545
+ assert_equal(%{"Juice::TestModule"}, json)
546
+ end
547
+ def test_module_object
548
+ Oj.default_options = { :mode => :object }
549
+ dump_and_load(TestModule, false)
550
+ end
551
+
489
552
  # Hash
490
553
  def test_hash
491
554
  Oj.default_options = { :mode => :strict }
@@ -880,7 +943,7 @@ class Juice < Minitest::Test
880
943
  BigDecimal.send(:define_method, :as_json) do
881
944
  %{this is big}
882
945
  end
883
- json = Oj.dump(orig, :mode => :compat)
946
+ json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => false)
884
947
  bg = Oj.load(json, :mode => :compat)
885
948
  assert_equal("this is big", bg)
886
949
  BigDecimal.send(:remove_method, :as_json) # cleanup