oj 3.9.2 → 3.10.18
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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/ext/oj/buf.h +2 -30
- data/ext/oj/cache8.h +1 -29
- data/ext/oj/circarray.c +4 -8
- data/ext/oj/circarray.h +1 -4
- data/ext/oj/code.c +3 -6
- data/ext/oj/code.h +1 -4
- data/ext/oj/compat.c +6 -9
- data/ext/oj/custom.c +8 -7
- data/ext/oj/dump.c +33 -26
- data/ext/oj/dump.h +1 -4
- data/ext/oj/dump_compat.c +9 -14
- data/ext/oj/dump_leaf.c +2 -5
- data/ext/oj/dump_object.c +19 -15
- data/ext/oj/dump_strict.c +7 -9
- data/ext/oj/encode.h +1 -29
- data/ext/oj/err.c +1 -4
- data/ext/oj/err.h +1 -29
- data/ext/oj/extconf.rb +5 -0
- data/ext/oj/fast.c +14 -42
- data/ext/oj/hash.c +4 -32
- data/ext/oj/hash.h +1 -29
- data/ext/oj/hash_test.c +1 -29
- data/ext/oj/mimic_json.c +28 -10
- data/ext/oj/object.c +4 -6
- data/ext/oj/odd.c +1 -4
- data/ext/oj/odd.h +1 -4
- data/ext/oj/oj.c +74 -38
- data/ext/oj/oj.h +9 -7
- data/ext/oj/parse.c +127 -52
- data/ext/oj/parse.h +4 -5
- data/ext/oj/rails.c +38 -8
- data/ext/oj/rails.h +1 -4
- data/ext/oj/reader.c +5 -8
- data/ext/oj/reader.h +2 -5
- data/ext/oj/resolve.c +1 -4
- data/ext/oj/resolve.h +1 -4
- data/ext/oj/rxclass.c +3 -6
- data/ext/oj/rxclass.h +1 -4
- data/ext/oj/saj.c +6 -9
- data/ext/oj/scp.c +1 -4
- data/ext/oj/sparse.c +31 -26
- data/ext/oj/stream_writer.c +4 -9
- data/ext/oj/strict.c +3 -6
- data/ext/oj/string_writer.c +1 -4
- data/ext/oj/trace.c +5 -8
- data/ext/oj/trace.h +1 -4
- data/ext/oj/util.c +1 -1
- data/ext/oj/util.h +1 -1
- data/ext/oj/val_stack.c +1 -29
- data/ext/oj/val_stack.h +1 -29
- data/ext/oj/wab.c +10 -13
- data/lib/oj/mimic.rb +45 -1
- data/lib/oj/version.rb +1 -1
- data/lib/oj.rb +0 -8
- data/pages/Modes.md +1 -1
- data/pages/Options.md +15 -11
- data/pages/Rails.md +60 -21
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +68 -60
- data/test/activesupport5/encoding_test.rb +111 -96
- data/test/activesupport5/encoding_test_cases.rb +33 -25
- data/test/activesupport5/test_helper.rb +43 -21
- data/test/activesupport5/time_zone_test_helpers.rb +18 -3
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/bar.rb +21 -11
- data/test/baz.rb +16 -0
- data/test/foo.rb +39 -8
- data/test/json_gem/json_common_interface_test.rb +8 -3
- data/test/prec.rb +23 -0
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +14 -8
- data/test/test_custom.rb +36 -6
- data/test/test_integer_range.rb +1 -2
- data/test/test_object.rb +12 -3
- data/test/test_rails.rb +35 -0
- data/test/test_strict.rb +24 -1
- data/test/test_various.rb +42 -64
- data/test/tests.rb +1 -0
- metadata +29 -7
@@ -0,0 +1,163 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
gem "minitest" # make sure we get the gem, not stdlib
|
4
|
+
require "minitest"
|
5
|
+
require "active_support/testing/tagged_logging"
|
6
|
+
require "active_support/testing/setup_and_teardown"
|
7
|
+
require "active_support/testing/assertions"
|
8
|
+
require "active_support/testing/deprecation"
|
9
|
+
require "active_support/testing/declarative"
|
10
|
+
require "active_support/testing/isolation"
|
11
|
+
require "active_support/testing/constant_lookup"
|
12
|
+
require "active_support/testing/time_helpers"
|
13
|
+
require "active_support/testing/file_fixtures"
|
14
|
+
require "active_support/testing/parallelization"
|
15
|
+
require "concurrent/utility/processor_counter"
|
16
|
+
|
17
|
+
module ActiveSupport
|
18
|
+
class TestCase < ::Minitest::Test
|
19
|
+
Assertion = Minitest::Assertion
|
20
|
+
|
21
|
+
class << self
|
22
|
+
# Sets the order in which test cases are run.
|
23
|
+
#
|
24
|
+
# ActiveSupport::TestCase.test_order = :random # => :random
|
25
|
+
#
|
26
|
+
# Valid values are:
|
27
|
+
# * +:random+ (to run tests in random order)
|
28
|
+
# * +:parallel+ (to run tests in parallel)
|
29
|
+
# * +:sorted+ (to run tests alphabetically by method name)
|
30
|
+
# * +:alpha+ (equivalent to +:sorted+)
|
31
|
+
def test_order=(new_order)
|
32
|
+
ActiveSupport.test_order = new_order
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the order in which test cases are run.
|
36
|
+
#
|
37
|
+
# ActiveSupport::TestCase.test_order # => :random
|
38
|
+
#
|
39
|
+
# Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
|
40
|
+
# Defaults to +:random+.
|
41
|
+
def test_order
|
42
|
+
ActiveSupport.test_order ||= :random
|
43
|
+
end
|
44
|
+
|
45
|
+
# Parallelizes the test suite.
|
46
|
+
#
|
47
|
+
# Takes a +workers+ argument that controls how many times the process
|
48
|
+
# is forked. For each process a new database will be created suffixed
|
49
|
+
# with the worker number.
|
50
|
+
#
|
51
|
+
# test-database-0
|
52
|
+
# test-database-1
|
53
|
+
#
|
54
|
+
# If <tt>ENV["PARALLEL_WORKERS"]</tt> is set the workers argument will be ignored
|
55
|
+
# and the environment variable will be used instead. This is useful for CI
|
56
|
+
# environments, or other environments where you may need more workers than
|
57
|
+
# you do for local testing.
|
58
|
+
#
|
59
|
+
# If the number of workers is set to +1+ or fewer, the tests will not be
|
60
|
+
# parallelized.
|
61
|
+
#
|
62
|
+
# If +workers+ is set to +:number_of_processors+, the number of workers will be
|
63
|
+
# set to the actual core count on the machine you are on.
|
64
|
+
#
|
65
|
+
# The default parallelization method is to fork processes. If you'd like to
|
66
|
+
# use threads instead you can pass <tt>with: :threads</tt> to the +parallelize+
|
67
|
+
# method. Note the threaded parallelization does not create multiple
|
68
|
+
# database and will not work with system tests at this time.
|
69
|
+
#
|
70
|
+
# parallelize(workers: :number_of_processors, with: :threads)
|
71
|
+
#
|
72
|
+
# The threaded parallelization uses minitest's parallel executor directly.
|
73
|
+
# The processes parallelization uses a Ruby DRb server.
|
74
|
+
def parallelize(workers: :number_of_processors, with: :processes)
|
75
|
+
workers = Concurrent.physical_processor_count if workers == :number_of_processors
|
76
|
+
workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]
|
77
|
+
|
78
|
+
return if workers <= 1
|
79
|
+
|
80
|
+
executor = case with
|
81
|
+
when :processes
|
82
|
+
Testing::Parallelization.new(workers)
|
83
|
+
when :threads
|
84
|
+
Minitest::Parallel::Executor.new(workers)
|
85
|
+
else
|
86
|
+
raise ArgumentError, "#{with} is not a supported parallelization executor."
|
87
|
+
end
|
88
|
+
|
89
|
+
self.lock_threads = false if defined?(self.lock_threads) && with == :threads
|
90
|
+
|
91
|
+
Minitest.parallel_executor = executor
|
92
|
+
|
93
|
+
parallelize_me!
|
94
|
+
end
|
95
|
+
|
96
|
+
# Set up hook for parallel testing. This can be used if you have multiple
|
97
|
+
# databases or any behavior that needs to be run after the process is forked
|
98
|
+
# but before the tests run.
|
99
|
+
#
|
100
|
+
# Note: this feature is not available with the threaded parallelization.
|
101
|
+
#
|
102
|
+
# In your +test_helper.rb+ add the following:
|
103
|
+
#
|
104
|
+
# class ActiveSupport::TestCase
|
105
|
+
# parallelize_setup do
|
106
|
+
# # create databases
|
107
|
+
# end
|
108
|
+
# end
|
109
|
+
def parallelize_setup(&block)
|
110
|
+
ActiveSupport::Testing::Parallelization.after_fork_hook do |worker|
|
111
|
+
yield worker
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Clean up hook for parallel testing. This can be used to drop databases
|
116
|
+
# if your app uses multiple write/read databases or other clean up before
|
117
|
+
# the tests finish. This runs before the forked process is closed.
|
118
|
+
#
|
119
|
+
# Note: this feature is not available with the threaded parallelization.
|
120
|
+
#
|
121
|
+
# In your +test_helper.rb+ add the following:
|
122
|
+
#
|
123
|
+
# class ActiveSupport::TestCase
|
124
|
+
# parallelize_teardown do
|
125
|
+
# # drop databases
|
126
|
+
# end
|
127
|
+
# end
|
128
|
+
def parallelize_teardown(&block)
|
129
|
+
ActiveSupport::Testing::Parallelization.run_cleanup_hook do |worker|
|
130
|
+
yield worker
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
alias_method :method_name, :name
|
136
|
+
|
137
|
+
include ActiveSupport::Testing::TaggedLogging
|
138
|
+
prepend ActiveSupport::Testing::SetupAndTeardown
|
139
|
+
include ActiveSupport::Testing::Assertions
|
140
|
+
include ActiveSupport::Testing::Deprecation
|
141
|
+
include ActiveSupport::Testing::TimeHelpers
|
142
|
+
include ActiveSupport::Testing::FileFixtures
|
143
|
+
extend ActiveSupport::Testing::Declarative
|
144
|
+
|
145
|
+
# test/unit backwards compatibility methods
|
146
|
+
alias :assert_raise :assert_raises
|
147
|
+
alias :assert_not_empty :refute_empty
|
148
|
+
alias :assert_not_equal :refute_equal
|
149
|
+
alias :assert_not_in_delta :refute_in_delta
|
150
|
+
alias :assert_not_in_epsilon :refute_in_epsilon
|
151
|
+
alias :assert_not_includes :refute_includes
|
152
|
+
alias :assert_not_instance_of :refute_instance_of
|
153
|
+
alias :assert_not_kind_of :refute_kind_of
|
154
|
+
alias :assert_no_match :refute_match
|
155
|
+
alias :assert_not_nil :refute_nil
|
156
|
+
alias :assert_not_operator :refute_operator
|
157
|
+
alias :assert_not_predicate :refute_predicate
|
158
|
+
alias :assert_not_respond_to :refute_respond_to
|
159
|
+
alias :assert_not_same :refute_same
|
160
|
+
|
161
|
+
ActiveSupport.run_load_hooks(:active_support_test_case, self)
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TimeZoneTestHelpers
|
4
|
+
def with_tz_default(tz = nil)
|
5
|
+
old_tz = Time.zone
|
6
|
+
Time.zone = tz
|
7
|
+
yield
|
8
|
+
ensure
|
9
|
+
Time.zone = old_tz
|
10
|
+
end
|
11
|
+
|
12
|
+
def with_env_tz(new_tz = "US/Eastern")
|
13
|
+
old_tz, ENV["TZ"] = ENV["TZ"], new_tz
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
old_tz ? ENV["TZ"] = old_tz : ENV.delete("TZ")
|
17
|
+
end
|
18
|
+
|
19
|
+
def with_preserve_timezone(value)
|
20
|
+
old_preserve_tz = ActiveSupport.to_time_preserves_timezone
|
21
|
+
ActiveSupport.to_time_preserves_timezone = value
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
ActiveSupport.to_time_preserves_timezone = old_preserve_tz
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_tz_mappings(mappings)
|
28
|
+
old_mappings = ActiveSupport::TimeZone::MAPPING.dup
|
29
|
+
ActiveSupport::TimeZone.clear
|
30
|
+
ActiveSupport::TimeZone::MAPPING.clear
|
31
|
+
ActiveSupport::TimeZone::MAPPING.merge!(mappings)
|
32
|
+
|
33
|
+
yield
|
34
|
+
ensure
|
35
|
+
ActiveSupport::TimeZone.clear
|
36
|
+
ActiveSupport::TimeZone::MAPPING.clear
|
37
|
+
ActiveSupport::TimeZone::MAPPING.merge!(old_mappings)
|
38
|
+
end
|
39
|
+
end
|
data/test/bar.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
2
|
|
4
3
|
$: << File.dirname(__FILE__)
|
5
4
|
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
@@ -7,19 +6,30 @@ $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
|
7
6
|
$: << File.join($oj_dir, dir)
|
8
7
|
end
|
9
8
|
|
10
|
-
require '
|
9
|
+
require 'active_support'
|
10
|
+
require "active_support/json"
|
11
|
+
|
12
|
+
$s = "\u2014 & \n \u{1F618}"
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
#}
|
14
|
+
=begin
|
15
|
+
def check(label)
|
16
|
+
puts "\n--- #{label} --------------------"
|
15
17
|
|
16
|
-
|
18
|
+
ActiveSupport::JSON::Encoding.use_standard_json_time_format = true
|
19
|
+
puts "with standard_json == true: t.to_json - #{$t.to_json}"
|
20
|
+
ActiveSupport::JSON::Encoding.use_standard_json_time_format = false
|
21
|
+
puts "with standard_json == false: t.to_json - #{$t.to_json}"
|
22
|
+
end
|
17
23
|
|
18
|
-
|
24
|
+
check('Before Oj')
|
25
|
+
=end
|
26
|
+
|
27
|
+
require 'oj'
|
19
28
|
|
20
|
-
|
29
|
+
ActiveSupport::JSON::Encoding.escape_html_entities_in_json = false
|
30
|
+
puts "ActiveSupport.encode(s) - #{ActiveSupport::JSON.encode($s)}"
|
21
31
|
|
22
|
-
Oj.
|
23
|
-
|
32
|
+
Oj.optimize_rails
|
33
|
+
Oj.default_options = { mode: :rails }
|
24
34
|
|
25
|
-
puts Oj.
|
35
|
+
puts "Oj.dump(s) - #{Oj.dump($s)}"
|
data/test/baz.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$: << File.dirname(__FILE__)
|
4
|
+
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
5
|
+
%w(lib ext).each do |dir|
|
6
|
+
$: << File.join($oj_dir, dir)
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'oj'
|
10
|
+
Oj.mimic_JSON()
|
11
|
+
|
12
|
+
begin
|
13
|
+
::JSON.load('name=&email=&subject=&comment=&submit=Send+Message')
|
14
|
+
rescue ::JSON::ParserError
|
15
|
+
puts "*** Pass"
|
16
|
+
end
|
data/test/foo.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
2
|
|
4
3
|
$: << File.dirname(__FILE__)
|
5
4
|
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
@@ -7,15 +6,47 @@ $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
|
7
6
|
$: << File.join($oj_dir, dir)
|
8
7
|
end
|
9
8
|
|
9
|
+
require 'json'
|
10
|
+
|
11
|
+
t = [Time.now.utc]
|
12
|
+
|
13
|
+
puts "t.to_json - #{t.to_json}"
|
14
|
+
|
15
|
+
puts "--- active support"
|
16
|
+
|
17
|
+
require 'active_support'
|
18
|
+
require "active_support/json"
|
19
|
+
|
20
|
+
ActiveSupport::JSON::Encoding.use_standard_json_time_format = false
|
21
|
+
|
22
|
+
puts "t.as_json - #{t.as_json}"
|
23
|
+
puts "t.to_json - #{t.to_json}"
|
24
|
+
|
10
25
|
require 'oj'
|
26
|
+
|
27
|
+
t = [Time.now.utc]
|
28
|
+
|
29
|
+
puts "-----------------------"
|
30
|
+
|
31
|
+
#puts "t.as_json - #{t.as_json}"
|
32
|
+
puts "t.to_json - #{t.to_json}"
|
33
|
+
|
34
|
+
#Oj.mimic_JSON
|
35
|
+
|
36
|
+
#puts "Oj - t.as_json - #{t.as_json}"
|
37
|
+
|
38
|
+
puts "--- active support"
|
39
|
+
|
11
40
|
require 'active_support'
|
12
|
-
require
|
13
|
-
|
41
|
+
require "active_support/json"
|
42
|
+
|
43
|
+
ActiveSupport::JSON::Encoding.use_standard_json_time_format = false
|
14
44
|
|
15
|
-
|
16
|
-
|
17
|
-
Oj::Rails.optimize()
|
45
|
+
puts "t.as_json - #{t.as_json}"
|
46
|
+
puts "t.to_json - #{t.to_json}"
|
18
47
|
|
19
|
-
|
48
|
+
puts "--- optimize"
|
49
|
+
Oj.optimize_rails
|
20
50
|
|
21
|
-
puts
|
51
|
+
puts "t.as_json - #{t.as_json}"
|
52
|
+
puts "t.to_json - #{t.to_json}"
|
@@ -15,7 +15,7 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
|
|
15
15
|
def setup
|
16
16
|
@hash = {
|
17
17
|
'a' => 2,
|
18
|
-
'b' =>
|
18
|
+
#'b' => 5.23683071,
|
19
19
|
'c' => 'c',
|
20
20
|
'd' => [ 1, "b", 3.14 ],
|
21
21
|
'e' => { 'foo' => 'bar' },
|
@@ -23,8 +23,13 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
|
|
23
23
|
'h' => 1000.0,
|
24
24
|
'i' => 0.001
|
25
25
|
}
|
26
|
-
|
27
|
-
|
26
|
+
# Tired of chasing floating point rounding and precision. Oj now uses the
|
27
|
+
# Ruby float parser in compat mode yet on i386 machines there are issues
|
28
|
+
# with this test when the float is included.
|
29
|
+
#@json = '{"a":2,"b":5.23683071,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
30
|
+
#'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
31
|
+
@json = '{"a":2,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
32
|
+
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
28
33
|
end
|
29
34
|
|
30
35
|
def test_index
|
data/test/prec.rb
ADDED
@@ -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)
|
data/test/sample_json.rb
CHANGED
data/test/test_compat.rb
CHANGED
@@ -147,16 +147,9 @@ class CompatJuice < Minitest::Test
|
|
147
147
|
|
148
148
|
def test_encode
|
149
149
|
opts = Oj.default_options
|
150
|
-
Oj.default_options = { :ascii_only => false }
|
151
|
-
unless 'jruby' == $ruby
|
152
|
-
dump_and_load("ぴーたー", false)
|
153
|
-
end
|
154
150
|
Oj.default_options = { :ascii_only => true }
|
155
151
|
json = Oj.dump("ぴーたー")
|
156
152
|
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
|
157
|
-
unless 'jruby' == $ruby
|
158
|
-
dump_and_load("ぴーたー", false)
|
159
|
-
end
|
160
153
|
Oj.default_options = opts
|
161
154
|
end
|
162
155
|
|
@@ -243,6 +236,12 @@ class CompatJuice < Minitest::Test
|
|
243
236
|
assert_equal({"a\nb" => true, "c\td" => false}, obj)
|
244
237
|
end
|
245
238
|
|
239
|
+
def test_invalid_escapes_handled
|
240
|
+
json = '{"subtext":"\"404er\” \w \k \3 \a"}'
|
241
|
+
obj = Oj.compat_load(json)
|
242
|
+
assert_equal({"subtext" => "\"404er” w k 3 a"}, obj)
|
243
|
+
end
|
244
|
+
|
246
245
|
def test_hash_escaping
|
247
246
|
json = Oj.to_json({'<>' => '<>'}, mode: :compat)
|
248
247
|
assert_equal(json, '{"<>":"<>"}')
|
@@ -278,12 +277,19 @@ class CompatJuice < Minitest::Test
|
|
278
277
|
# BigDecimal
|
279
278
|
def test_bigdecimal
|
280
279
|
# BigDecimals are dumped as strings and can not be restored to the
|
281
|
-
# original value.
|
280
|
+
# original value without using an undocumented feature of the JSON gem.
|
282
281
|
json = Oj.dump(BigDecimal('3.14159265358979323846'))
|
283
282
|
# 2.4.0 changes the exponent to lowercase
|
284
283
|
assert_equal('"0.314159265358979323846e1"', json.downcase)
|
285
284
|
end
|
286
285
|
|
286
|
+
def test_bigdecimal_load
|
287
|
+
big = BigDecimal('3.14159265358979323846')
|
288
|
+
# :decimal_class is the undocumented feature.
|
289
|
+
json = Oj.load('3.14159265358979323846', mode: :compat, decimal_class: BigDecimal)
|
290
|
+
assert_equal(big, json)
|
291
|
+
end
|
292
|
+
|
287
293
|
def test_infinity
|
288
294
|
assert_raises(Oj::ParseError) { Oj.load('Infinity', :mode => :strict) }
|
289
295
|
x = Oj.load('Infinity', :mode => :compat)
|
data/test/test_custom.rb
CHANGED
@@ -20,11 +20,12 @@ class CustomJuice < Minitest::Test
|
|
20
20
|
end
|
21
21
|
|
22
22
|
class Jeez
|
23
|
-
attr_accessor :x, :y
|
23
|
+
attr_accessor :x, :y, :_z
|
24
24
|
|
25
25
|
def initialize(x, y)
|
26
26
|
@x = x
|
27
27
|
@y = y
|
28
|
+
@_z = x.to_s
|
28
29
|
end
|
29
30
|
def ==(o)
|
30
31
|
self.class == o.class && @x == o.x && @y = o.y
|
@@ -117,6 +118,17 @@ class CustomJuice < Minitest::Test
|
|
117
118
|
dump_and_load(-2.48e100 * 1.0e10, false)
|
118
119
|
end
|
119
120
|
|
121
|
+
def test_float_parse
|
122
|
+
f = Oj.load("12.123456789012345678", mode: :custom, bigdecimal_load: :float);
|
123
|
+
assert_equal(Float, f.class)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_float_parse_fast
|
127
|
+
f = Oj.load("12.123456789012345678", mode: :custom, bigdecimal_load: :fast);
|
128
|
+
assert_equal(Float, f.class)
|
129
|
+
assert(12.12345678901234 <= f && f < 12.12345678901236)
|
130
|
+
end
|
131
|
+
|
120
132
|
def test_nan_dump
|
121
133
|
assert_equal('null', Oj.dump(0/0.0, :nan => :null))
|
122
134
|
assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
|
@@ -247,7 +259,10 @@ class CustomJuice < Minitest::Test
|
|
247
259
|
|
248
260
|
def test_object
|
249
261
|
obj = Jeez.new(true, 58)
|
250
|
-
Oj.dump(obj, :
|
262
|
+
json = Oj.dump(obj, create_id: "^o", use_to_json: false, use_as_json: false, use_to_hash: false)
|
263
|
+
assert_equal(%|{"x":true,"y":58,"_z":"true"}|, json)
|
264
|
+
json = Oj.dump(obj, create_id: "^o", use_to_json: false, use_as_json: false, use_to_hash: false, ignore_under: true)
|
265
|
+
assert_equal(%|{"x":true,"y":58}|, json)
|
251
266
|
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
252
267
|
end
|
253
268
|
|
@@ -466,10 +481,10 @@ class CustomJuice < Minitest::Test
|
|
466
481
|
|
467
482
|
def test_time
|
468
483
|
obj = Time.now()
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
484
|
+
dump_load_dump(obj, false, :time_format => :unix, :create_id => "^o", :create_additions => true)
|
485
|
+
dump_load_dump(obj, false, :time_format => :unix_zone, :create_id => "^o", :create_additions => true)
|
486
|
+
dump_load_dump(obj, false, :time_format => :xmlschema, :create_id => "^o", :create_additions => true)
|
487
|
+
dump_load_dump(obj, false, :time_format => :ruby, :create_id => "^o", :create_additions => true)
|
473
488
|
end
|
474
489
|
|
475
490
|
def dump_and_load(obj, trace=false, options={})
|
@@ -500,4 +515,19 @@ class CustomJuice < Minitest::Test
|
|
500
515
|
loaded
|
501
516
|
end
|
502
517
|
|
518
|
+
def dump_load_dump(obj, trace=false, options={})
|
519
|
+
options = options.merge(:indent => 2, :mode => :custom)
|
520
|
+
json = Oj.dump(obj, options)
|
521
|
+
puts json if trace
|
522
|
+
|
523
|
+
loaded = Oj.load(json, options);
|
524
|
+
if obj.nil?
|
525
|
+
assert_nil(loaded)
|
526
|
+
else
|
527
|
+
json2 = Oj.dump(loaded, options)
|
528
|
+
assert_equal(json, json2)
|
529
|
+
end
|
530
|
+
loaded
|
531
|
+
end
|
532
|
+
|
503
533
|
end
|
data/test/test_integer_range.rb
CHANGED
@@ -15,7 +15,7 @@ class IntegerRangeTest < Minitest::Test
|
|
15
15
|
def setup
|
16
16
|
@default_options = Oj.default_options
|
17
17
|
# in null mode other options other than the number formats are not used.
|
18
|
-
Oj.default_options = { :mode => :null }
|
18
|
+
Oj.default_options = { :mode => :null, bigdecimal_as_decimal: true }
|
19
19
|
end
|
20
20
|
|
21
21
|
def teardown
|
@@ -70,4 +70,3 @@ class IntegerRangeTest < Minitest::Test
|
|
70
70
|
assert_equal(exp, Oj.dump(test, integer_range: false))
|
71
71
|
end
|
72
72
|
end
|
73
|
-
|
data/test/test_object.rb
CHANGED
@@ -7,11 +7,12 @@ require 'helper'
|
|
7
7
|
|
8
8
|
class ObjectJuice < Minitest::Test
|
9
9
|
class Jeez
|
10
|
-
attr_accessor :x, :y
|
10
|
+
attr_accessor :x, :y, :_z
|
11
11
|
|
12
12
|
def initialize(x, y)
|
13
13
|
@x = x
|
14
14
|
@y = y
|
15
|
+
@_z = x.to_s
|
15
16
|
end
|
16
17
|
|
17
18
|
def eql?(o)
|
@@ -687,7 +688,7 @@ class ObjectJuice < Minitest::Test
|
|
687
688
|
|
688
689
|
def test_json_object_object
|
689
690
|
obj = Jeez.new(true, 58)
|
690
|
-
json = Oj.dump(obj, :
|
691
|
+
json = Oj.dump(obj, mode: :object, indent: 2, ignore_under: true)
|
691
692
|
assert(%{{
|
692
693
|
"^o":"ObjectJuice::Jeez",
|
693
694
|
"x":true,
|
@@ -799,7 +800,7 @@ class ObjectJuice < Minitest::Test
|
|
799
800
|
@xyz = 123
|
800
801
|
end
|
801
802
|
end
|
802
|
-
|
803
|
+
|
803
804
|
def test_exception_subclass
|
804
805
|
err = nil
|
805
806
|
begin
|
@@ -868,6 +869,14 @@ class ObjectJuice < Minitest::Test
|
|
868
869
|
assert_equal(a2[1].__id__, a2.__id__)
|
869
870
|
end
|
870
871
|
|
872
|
+
def test_circular_array3
|
873
|
+
a = ['^r1']
|
874
|
+
json = Oj.dump(a, mode: :object, circular: true)
|
875
|
+
assert_equal(%{["^i1","\\u005er1"]}, json)
|
876
|
+
a2 = Oj.load(json, mode: :object, circular: true)
|
877
|
+
assert_equal(a, a2)
|
878
|
+
end
|
879
|
+
|
871
880
|
def test_circular_hash2
|
872
881
|
h = { 'a' => 7 }
|
873
882
|
h['b'] = h
|
data/test/test_rails.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class RailsJuice < Minitest::Test
|
9
|
+
|
10
|
+
def test_bigdecimal_dump
|
11
|
+
orig = Oj.default_options
|
12
|
+
Oj.default_options = { mode: :rails, bigdecimal_as_decimal: true }
|
13
|
+
bd = BigDecimal('123')
|
14
|
+
json = Oj.dump(bd)
|
15
|
+
Oj.default_options = orig
|
16
|
+
|
17
|
+
assert_equal('0.123e3', json.downcase)
|
18
|
+
|
19
|
+
json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: false)
|
20
|
+
assert_equal('"0.123e3"', json.downcase)
|
21
|
+
|
22
|
+
json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: true)
|
23
|
+
assert_equal('0.123e3', json.downcase)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_invalid_encoding
|
27
|
+
assert_raises(EncodingError) {
|
28
|
+
Oj.dump("\"\xf3j", mode: :rails)
|
29
|
+
}
|
30
|
+
assert_raises(EncodingError) {
|
31
|
+
Oj.dump("\xf3j", mode: :rails)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/test/test_strict.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# encoding:
|
2
|
+
# encoding: utf-8
|
3
3
|
|
4
4
|
$: << File.dirname(__FILE__)
|
5
5
|
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
@@ -379,6 +379,29 @@ class StrictJuice < Minitest::Test
|
|
379
379
|
assert_equal([{ 'x' => 1 }, { 'y' => 2 }], results)
|
380
380
|
end
|
381
381
|
|
382
|
+
def test_invalid_decimal_dot_start
|
383
|
+
assert_raises(Oj::ParseError) {
|
384
|
+
Oj.load('.123', mode: :strict)
|
385
|
+
}
|
386
|
+
assert_raises(Oj::ParseError) {
|
387
|
+
Oj.load('-.123', mode: :strict)
|
388
|
+
}
|
389
|
+
end
|
390
|
+
|
391
|
+
def test_invalid_decimal_dot_end
|
392
|
+
json = '123.'
|
393
|
+
assert_raises(Oj::ParseError) {
|
394
|
+
Oj.load(json, mode: :strict)
|
395
|
+
}
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_invalid_decimal_plus
|
399
|
+
json = '+12'
|
400
|
+
assert_raises(Oj::ParseError) {
|
401
|
+
Oj.load(json, mode: :strict)
|
402
|
+
}
|
403
|
+
end
|
404
|
+
|
382
405
|
def test_circular_hash
|
383
406
|
h = { 'a' => 7 }
|
384
407
|
h['b'] = h
|