oj 2.18.3 → 3.13.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1324 -0
- data/README.md +51 -204
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +49 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -68
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +221 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +231 -107
- data/ext/oj/custom.c +1125 -0
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +935 -2513
- data/ext/oj/dump.h +108 -0
- data/ext/oj/dump_compat.c +936 -0
- data/ext/oj/dump_leaf.c +164 -0
- data/ext/oj/dump_object.c +761 -0
- data/ext/oj/dump_strict.c +410 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -54
- data/ext/oj/err.h +52 -46
- data/ext/oj/extconf.rb +21 -30
- data/ext/oj/fast.c +1097 -1080
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +893 -0
- data/ext/oj/object.c +549 -620
- data/ext/oj/odd.c +155 -167
- data/ext/oj/odd.h +37 -63
- data/ext/oj/oj.c +1661 -2063
- data/ext/oj/oj.h +341 -270
- data/ext/oj/parse.c +974 -737
- data/ext/oj/parse.h +105 -97
- data/ext/oj/parser.c +1526 -0
- data/ext/oj/parser.h +90 -0
- data/ext/oj/rails.c +1504 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +141 -163
- data/ext/oj/reader.h +75 -113
- data/ext/oj/resolve.c +45 -93
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +143 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +447 -511
- data/ext/oj/saj2.c +348 -0
- data/ext/oj/scp.c +91 -138
- data/ext/oj/sparse.c +793 -644
- data/ext/oj/stream_writer.c +331 -0
- data/ext/oj/strict.c +145 -109
- data/ext/oj/string_writer.c +493 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +62 -70
- data/ext/oj/val_stack.h +95 -129
- data/ext/oj/validate.c +51 -0
- data/ext/oj/wab.c +622 -0
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +17 -8
- data/lib/oj/error.rb +10 -11
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +158 -19
- data/lib/oj/state.rb +132 -0
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +1 -31
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +327 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- data/test/activerecord/result_test.rb +32 -0
- data/test/activesupport4/decoding_test.rb +108 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +133 -0
- data/test/activesupport5/encoding_test.rb +500 -0
- data/test/activesupport5/encoding_test_cases.rb +98 -0
- data/test/activesupport5/test_helper.rb +72 -0
- data/test/activesupport5/time_zone_test_helpers.rb +39 -0
- 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/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +9 -0
- data/test/baz.rb +16 -0
- data/test/bug.rb +11 -46
- data/test/foo.rb +69 -16
- data/test/helper.rb +10 -1
- data/test/isolated/shared.rb +12 -8
- data/test/isolated/test_mimic_rails_after.rb +3 -3
- data/test/isolated/test_mimic_rails_before.rb +3 -3
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +153 -0
- data/test/json_gem/json_encoding_test.rb +107 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +397 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +26 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_compat.rb +30 -28
- data/test/perf_dump.rb +50 -0
- data/test/perf_object.rb +1 -1
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +30 -19
- data/test/perf_wab.rb +131 -0
- data/test/prec.rb +23 -0
- data/test/sample.rb +0 -1
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +219 -102
- data/test/test_custom.rb +533 -0
- data/test/test_fast.rb +107 -35
- data/test/test_file.rb +19 -25
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +72 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +357 -70
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +245 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +39 -2
- data/test/test_strict.rb +186 -7
- data/test/test_various.rb +160 -774
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +90 -2
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +194 -56
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
@@ -0,0 +1,133 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "abstract_unit"
|
4
|
+
require "active_support/json"
|
5
|
+
require "active_support/time"
|
6
|
+
require_relative "time_zone_test_helpers"
|
7
|
+
|
8
|
+
require 'oj'
|
9
|
+
|
10
|
+
Oj::Rails.set_decoder()
|
11
|
+
|
12
|
+
class TestJSONDecoding < ActiveSupport::TestCase
|
13
|
+
include TimeZoneTestHelpers
|
14
|
+
|
15
|
+
# Added for testing if Oj is used.
|
16
|
+
test "oj is used as an encoder" do
|
17
|
+
assert_equal ActiveSupport.json_encoder, Oj::Rails::Encoder
|
18
|
+
end
|
19
|
+
|
20
|
+
class Foo
|
21
|
+
def self.json_create(object)
|
22
|
+
"Foo"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
TESTS = {
|
27
|
+
%q({"returnTo":{"\/categories":"\/"}}) => { "returnTo" => { "/categories" => "/" } },
|
28
|
+
%q({"return\\"To\\":":{"\/categories":"\/"}}) => { "return\"To\":" => { "/categories" => "/" } },
|
29
|
+
%q({"returnTo":{"\/categories":1}}) => { "returnTo" => { "/categories" => 1 } },
|
30
|
+
%({"returnTo":[1,"a"]}) => { "returnTo" => [1, "a"] },
|
31
|
+
%({"returnTo":[1,"\\"a\\",", "b"]}) => { "returnTo" => [1, "\"a\",", "b"] },
|
32
|
+
%({"a": "'", "b": "5,000"}) => { "a" => "'", "b" => "5,000" },
|
33
|
+
%({"a": "a's, b's and c's", "b": "5,000"}) => { "a" => "a's, b's and c's", "b" => "5,000" },
|
34
|
+
# multibyte
|
35
|
+
%({"matzue": "松江", "asakusa": "浅草"}) => { "matzue" => "松江", "asakusa" => "浅草" },
|
36
|
+
%({"a": "2007-01-01"}) => { "a" => Date.new(2007, 1, 1) },
|
37
|
+
%({"a": "2007-01-01 01:12:34 Z"}) => { "a" => Time.utc(2007, 1, 1, 1, 12, 34) },
|
38
|
+
%(["2007-01-01 01:12:34 Z"]) => [Time.utc(2007, 1, 1, 1, 12, 34)],
|
39
|
+
%(["2007-01-01 01:12:34 Z", "2007-01-01 01:12:35 Z"]) => [Time.utc(2007, 1, 1, 1, 12, 34), Time.utc(2007, 1, 1, 1, 12, 35)],
|
40
|
+
# no time zone
|
41
|
+
%({"a": "2007-01-01 01:12:34"}) => { "a" => Time.new(2007, 1, 1, 1, 12, 34, "-05:00") },
|
42
|
+
# invalid date
|
43
|
+
%({"a": "1089-10-40"}) => { "a" => "1089-10-40" },
|
44
|
+
# xmlschema date notation
|
45
|
+
%({"a": "2009-08-10T19:01:02"}) => { "a" => Time.new(2009, 8, 10, 19, 1, 2, "-04:00") },
|
46
|
+
%({"a": "2009-08-10T19:01:02Z"}) => { "a" => Time.utc(2009, 8, 10, 19, 1, 2) },
|
47
|
+
%({"a": "2009-08-10T19:01:02+02:00"}) => { "a" => Time.utc(2009, 8, 10, 17, 1, 2) },
|
48
|
+
%({"a": "2009-08-10T19:01:02-05:00"}) => { "a" => Time.utc(2009, 8, 11, 00, 1, 2) },
|
49
|
+
# needs to be *exact*
|
50
|
+
%({"a": " 2007-01-01 01:12:34 Z "}) => { "a" => " 2007-01-01 01:12:34 Z " },
|
51
|
+
%({"a": "2007-01-01 : it's your birthday"}) => { "a" => "2007-01-01 : it's your birthday" },
|
52
|
+
%([]) => [],
|
53
|
+
%({}) => {},
|
54
|
+
%({"a":1}) => { "a" => 1 },
|
55
|
+
%({"a": ""}) => { "a" => "" },
|
56
|
+
%({"a":"\\""}) => { "a" => "\"" },
|
57
|
+
%({"a": null}) => { "a" => nil },
|
58
|
+
%({"a": true}) => { "a" => true },
|
59
|
+
%({"a": false}) => { "a" => false },
|
60
|
+
'{"bad":"\\\\","trailing":""}' => { "bad" => "\\", "trailing" => "" },
|
61
|
+
%q({"a": "http:\/\/test.host\/posts\/1"}) => { "a" => "http://test.host/posts/1" },
|
62
|
+
%q({"a": "\u003cunicode\u0020escape\u003e"}) => { "a" => "<unicode escape>" },
|
63
|
+
'{"a": "\\\\u0020skip double backslashes"}' => { "a" => "\\u0020skip double backslashes" },
|
64
|
+
%q({"a": "\u003cbr /\u003e"}) => { "a" => "<br />" },
|
65
|
+
%q({"b":["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => { "b" => ["<i>", "<b>", "<u>"] },
|
66
|
+
# test combination of dates and escaped or unicode encoded data in arrays
|
67
|
+
%q([{"d":"1970-01-01", "s":"\u0020escape"},{"d":"1970-01-01", "s":"\u0020escape"}]) =>
|
68
|
+
[{ "d" => Date.new(1970, 1, 1), "s" => " escape" }, { "d" => Date.new(1970, 1, 1), "s" => " escape" }],
|
69
|
+
%q([{"d":"1970-01-01","s":"http:\/\/example.com"},{"d":"1970-01-01","s":"http:\/\/example.com"}]) =>
|
70
|
+
[{ "d" => Date.new(1970, 1, 1), "s" => "http://example.com" },
|
71
|
+
{ "d" => Date.new(1970, 1, 1), "s" => "http://example.com" }],
|
72
|
+
# tests escaping of "\n" char with Yaml backend
|
73
|
+
%q({"a":"\n"}) => { "a" => "\n" },
|
74
|
+
%q({"a":"\u000a"}) => { "a" => "\n" },
|
75
|
+
%q({"a":"Line1\u000aLine2"}) => { "a" => "Line1\nLine2" },
|
76
|
+
# prevent json unmarshalling
|
77
|
+
'{"json_class":"TestJSONDecoding::Foo"}' => { "json_class" => "TestJSONDecoding::Foo" },
|
78
|
+
# json "fragments" - these are invalid JSON, but ActionPack relies on this
|
79
|
+
'"a string"' => "a string",
|
80
|
+
"1.1" => 1.1,
|
81
|
+
"1" => 1,
|
82
|
+
"-1" => -1,
|
83
|
+
"true" => true,
|
84
|
+
"false" => false,
|
85
|
+
"null" => nil
|
86
|
+
}
|
87
|
+
|
88
|
+
TESTS.each_with_index do |(json, expected), index|
|
89
|
+
fail_message = "JSON decoding failed for #{json}"
|
90
|
+
|
91
|
+
test "json decodes #{index}" do
|
92
|
+
with_tz_default "Eastern Time (US & Canada)" do
|
93
|
+
with_parse_json_times(true) do
|
94
|
+
silence_warnings do
|
95
|
+
if expected.nil?
|
96
|
+
assert_nil ActiveSupport::JSON.decode(json), fail_message
|
97
|
+
else
|
98
|
+
assert_equal expected, ActiveSupport::JSON.decode(json), fail_message
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
test "json decodes time json with time parsing disabled" do
|
107
|
+
with_parse_json_times(false) do
|
108
|
+
expected = { "a" => "2007-01-01 01:12:34 Z" }
|
109
|
+
assert_equal expected, ActiveSupport::JSON.decode(%({"a": "2007-01-01 01:12:34 Z"}))
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_failed_json_decoding
|
114
|
+
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%(undefined)) }
|
115
|
+
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({a: 1})) }
|
116
|
+
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({: 1})) }
|
117
|
+
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%()) }
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_cannot_pass_unsupported_options
|
121
|
+
assert_raise(ArgumentError) { ActiveSupport::JSON.decode("", create_additions: true) }
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def with_parse_json_times(value)
|
127
|
+
old_value = ActiveSupport.parse_json_times
|
128
|
+
ActiveSupport.parse_json_times = value
|
129
|
+
yield
|
130
|
+
ensure
|
131
|
+
ActiveSupport.parse_json_times = old_value
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,500 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
require_relative "abstract_unit"
|
5
|
+
require "active_support/core_ext/string/inflections"
|
6
|
+
require "active_support/core_ext/regexp"
|
7
|
+
require "active_support/json"
|
8
|
+
require "active_support/time"
|
9
|
+
require_relative "time_zone_test_helpers"
|
10
|
+
require_relative "encoding_test_cases"
|
11
|
+
|
12
|
+
require 'oj'
|
13
|
+
|
14
|
+
# Sets the ActiveSupport encoder to be Oj and also wraps the setting of
|
15
|
+
# globals.
|
16
|
+
Oj::Rails.set_encoder()
|
17
|
+
#Oj::Rails.optimize(Hash, Array, BigDecimal, Time, Range, Regexp, ActiveSupport::TimeWithZone)
|
18
|
+
Oj::Rails.optimize()
|
19
|
+
|
20
|
+
class TestJSONEncoding < ActiveSupport::TestCase
|
21
|
+
include TimeZoneTestHelpers
|
22
|
+
|
23
|
+
# Added for testing if Oj is used.
|
24
|
+
test "oj is used as an encoder" do
|
25
|
+
assert_equal ActiveSupport.json_encoder, Oj::Rails::Encoder
|
26
|
+
end
|
27
|
+
|
28
|
+
def sorted_json(json)
|
29
|
+
if json.start_with?("{") && json.end_with?("}")
|
30
|
+
"{" + json[1..-2].split(",").sort.join(",") + "}"
|
31
|
+
else
|
32
|
+
json
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
JSONTest::EncodingTestCases.constants.each do |class_tests|
|
37
|
+
define_method("test_#{class_tests[0..-6].underscore}") do
|
38
|
+
begin
|
39
|
+
prev = ActiveSupport.use_standard_json_time_format
|
40
|
+
|
41
|
+
standard_class_tests = /Standard/.match?(class_tests)
|
42
|
+
|
43
|
+
ActiveSupport.escape_html_entities_in_json = !standard_class_tests
|
44
|
+
ActiveSupport.use_standard_json_time_format = standard_class_tests
|
45
|
+
JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
|
46
|
+
assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
|
47
|
+
end
|
48
|
+
ensure
|
49
|
+
ActiveSupport.escape_html_entities_in_json = false
|
50
|
+
ActiveSupport.use_standard_json_time_format = prev
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_process_status
|
56
|
+
rubinius_skip "https://github.com/rubinius/rubinius/issues/3334"
|
57
|
+
|
58
|
+
# There doesn't seem to be a good way to get a handle on a Process::Status object without actually
|
59
|
+
# creating a child process, hence this to populate $?
|
60
|
+
system("not_a_real_program_#{SecureRandom.hex}")
|
61
|
+
assert_equal %({"exitstatus":#{$?.exitstatus},"pid":#{$?.pid}}), ActiveSupport::JSON.encode($?)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_hash_encoding
|
65
|
+
assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(a: :b)
|
66
|
+
assert_equal %({\"a\":1}), ActiveSupport::JSON.encode("a" => 1)
|
67
|
+
assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode("a" => [1, 2])
|
68
|
+
assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2)
|
69
|
+
|
70
|
+
assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(a: :b, c: :d))
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_hash_keys_encoding
|
74
|
+
ActiveSupport.escape_html_entities_in_json = true
|
75
|
+
assert_equal "{\"\\u003c\\u003e\":\"\\u003c\\u003e\"}", ActiveSupport::JSON.encode("<>" => "<>")
|
76
|
+
ensure
|
77
|
+
ActiveSupport.escape_html_entities_in_json = false
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_utf8_string_encoded_properly
|
81
|
+
# The original test seems to expect that
|
82
|
+
# ActiveSupport.escape_html_entities_in_json reverts to true even after
|
83
|
+
# being set to false. I haven't been able to figure that out so the value is
|
84
|
+
# set to true, the default, before running the test. This might be wrong but
|
85
|
+
# for now it will have to do.
|
86
|
+
ActiveSupport.escape_html_entities_in_json = true
|
87
|
+
result = ActiveSupport::JSON.encode("€2.99")
|
88
|
+
assert_equal '"€2.99"', result
|
89
|
+
assert_equal(Encoding::UTF_8, result.encoding)
|
90
|
+
|
91
|
+
result = ActiveSupport::JSON.encode("✎☺")
|
92
|
+
assert_equal '"✎☺"', result
|
93
|
+
assert_equal(Encoding::UTF_8, result.encoding)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_non_utf8_string_transcodes
|
97
|
+
s = "二".encode("Shift_JIS")
|
98
|
+
result = ActiveSupport::JSON.encode(s)
|
99
|
+
assert_equal '"二"', result
|
100
|
+
assert_equal Encoding::UTF_8, result.encoding
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_wide_utf8_chars
|
104
|
+
w = "𠜎"
|
105
|
+
result = ActiveSupport::JSON.encode(w)
|
106
|
+
assert_equal '"𠜎"', result
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_wide_utf8_roundtrip
|
110
|
+
hash = { string: "𐒑" }
|
111
|
+
json = ActiveSupport::JSON.encode(hash)
|
112
|
+
decoded_hash = ActiveSupport::JSON.decode(json)
|
113
|
+
assert_equal "𐒑", decoded_hash["string"]
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_hash_key_identifiers_are_always_quoted
|
117
|
+
values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
|
118
|
+
assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_hash_should_allow_key_filtering_with_only
|
122
|
+
assert_equal %({"a":1}), ActiveSupport::JSON.encode({ "a" => 1, :b => 2, :c => 3 }, { only: "a" })
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_hash_should_allow_key_filtering_with_except
|
126
|
+
assert_equal %({"b":2}), ActiveSupport::JSON.encode({ "foo" => "bar", :b => 2, :c => 3 }, { except: ["foo", :c] })
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_time_to_json_includes_local_offset
|
130
|
+
with_standard_json_time_format(true) do
|
131
|
+
with_env_tz "US/Eastern" do
|
132
|
+
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005, 2, 1, 15, 15, 10))
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_hash_with_time_to_json
|
138
|
+
with_standard_json_time_format(false) do
|
139
|
+
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { time: Time.utc(2009) }.to_json
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_nested_hash_with_float
|
144
|
+
assert_nothing_raised do
|
145
|
+
hash = {
|
146
|
+
"CHI" => {
|
147
|
+
display_name: "chicago",
|
148
|
+
latitude: 123.234
|
149
|
+
}
|
150
|
+
}
|
151
|
+
ActiveSupport::JSON.encode(hash)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_hash_like_with_options
|
156
|
+
h = JSONTest::Hashlike.new
|
157
|
+
json = h.to_json only: [:foo]
|
158
|
+
|
159
|
+
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_object_to_json_with_options
|
163
|
+
obj = Object.new
|
164
|
+
obj.instance_variable_set :@foo, "hello"
|
165
|
+
obj.instance_variable_set :@bar, "world"
|
166
|
+
json = obj.to_json only: ["foo"]
|
167
|
+
|
168
|
+
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_struct_to_json_with_options
|
172
|
+
struct = Struct.new(:foo, :bar).new
|
173
|
+
struct.foo = "hello"
|
174
|
+
struct.bar = "world"
|
175
|
+
json = struct.to_json only: [:foo]
|
176
|
+
|
177
|
+
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_hash_should_pass_encoding_options_to_children_in_as_json
|
181
|
+
person = {
|
182
|
+
name: "John",
|
183
|
+
address: {
|
184
|
+
city: "London",
|
185
|
+
country: "UK"
|
186
|
+
}
|
187
|
+
}
|
188
|
+
json = person.as_json only: [:address, :city]
|
189
|
+
|
190
|
+
assert_equal({ "address" => { "city" => "London" } }, json)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_hash_should_pass_encoding_options_to_children_in_to_json
|
194
|
+
person = {
|
195
|
+
name: "John",
|
196
|
+
address: {
|
197
|
+
city: "London",
|
198
|
+
country: "UK"
|
199
|
+
}
|
200
|
+
}
|
201
|
+
json = person.to_json only: [:address, :city]
|
202
|
+
|
203
|
+
assert_equal(%({"address":{"city":"London"}}), json)
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_array_should_pass_encoding_options_to_children_in_as_json
|
207
|
+
people = [
|
208
|
+
{ name: "John", address: { city: "London", country: "UK" } },
|
209
|
+
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
210
|
+
]
|
211
|
+
json = people.as_json only: [:address, :city]
|
212
|
+
expected = [
|
213
|
+
{ "address" => { "city" => "London" } },
|
214
|
+
{ "address" => { "city" => "Paris" } }
|
215
|
+
]
|
216
|
+
|
217
|
+
assert_equal(expected, json)
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_array_should_pass_encoding_options_to_children_in_to_json
|
221
|
+
people = [
|
222
|
+
{ name: "John", address: { city: "London", country: "UK" } },
|
223
|
+
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
224
|
+
]
|
225
|
+
json = people.to_json only: [:address, :city]
|
226
|
+
|
227
|
+
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
228
|
+
end
|
229
|
+
|
230
|
+
People = Class.new(BasicObject) do
|
231
|
+
include Enumerable
|
232
|
+
def initialize
|
233
|
+
@people = [
|
234
|
+
{ name: "John", address: { city: "London", country: "UK" } },
|
235
|
+
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
236
|
+
]
|
237
|
+
end
|
238
|
+
def each(*, &blk)
|
239
|
+
@people.each do |p|
|
240
|
+
yield p if blk
|
241
|
+
p
|
242
|
+
end.each
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_enumerable_should_generate_json_with_as_json
|
247
|
+
json = People.new.as_json only: [:address, :city]
|
248
|
+
expected = [
|
249
|
+
{ "address" => { "city" => "London" } },
|
250
|
+
{ "address" => { "city" => "Paris" } }
|
251
|
+
]
|
252
|
+
|
253
|
+
assert_equal(expected, json)
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_enumerable_should_generate_json_with_to_json
|
257
|
+
json = People.new.to_json only: [:address, :city]
|
258
|
+
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
259
|
+
end
|
260
|
+
|
261
|
+
def test_enumerable_should_pass_encoding_options_to_children_in_as_json
|
262
|
+
json = People.new.each.as_json only: [:address, :city]
|
263
|
+
expected = [
|
264
|
+
{ "address" => { "city" => "London" } },
|
265
|
+
{ "address" => { "city" => "Paris" } }
|
266
|
+
]
|
267
|
+
|
268
|
+
assert_equal(expected, json)
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_enumerable_should_pass_encoding_options_to_children_in_to_json
|
272
|
+
json = People.new.each.to_json only: [:address, :city]
|
273
|
+
|
274
|
+
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
275
|
+
end
|
276
|
+
|
277
|
+
class CustomWithOptions
|
278
|
+
attr_accessor :foo, :bar
|
279
|
+
|
280
|
+
def as_json(options = {})
|
281
|
+
options[:only] = %w(foo bar)
|
282
|
+
super(options)
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_hash_to_json_should_not_keep_options_around
|
287
|
+
f = CustomWithOptions.new
|
288
|
+
f.foo = "hello"
|
289
|
+
f.bar = "world"
|
290
|
+
|
291
|
+
hash = { "foo" => f, "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }
|
292
|
+
assert_equal({ "foo" => { "foo" => "hello", "bar" => "world" },
|
293
|
+
"other_hash" => { "foo" => "other_foo", "test" => "other_test" } }, ActiveSupport::JSON.decode(hash.to_json))
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_array_to_json_should_not_keep_options_around
|
297
|
+
f = CustomWithOptions.new
|
298
|
+
f.foo = "hello"
|
299
|
+
f.bar = "world"
|
300
|
+
|
301
|
+
array = [f, { "foo" => "other_foo", "test" => "other_test" }]
|
302
|
+
assert_equal([{ "foo" => "hello", "bar" => "world" },
|
303
|
+
{ "foo" => "other_foo", "test" => "other_test" }], ActiveSupport::JSON.decode(array.to_json))
|
304
|
+
end
|
305
|
+
|
306
|
+
class OptionsTest
|
307
|
+
def as_json(options = :default)
|
308
|
+
options
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_hash_as_json_without_options
|
313
|
+
json = { foo: OptionsTest.new }.as_json
|
314
|
+
assert_equal({ "foo" => :default }, json)
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_array_as_json_without_options
|
318
|
+
json = [ OptionsTest.new ].as_json
|
319
|
+
assert_equal([:default], json)
|
320
|
+
end
|
321
|
+
|
322
|
+
def test_struct_encoding
|
323
|
+
Struct.new("UserNameAndEmail", :name, :email)
|
324
|
+
Struct.new("UserNameAndDate", :name, :date)
|
325
|
+
Struct.new("Custom", :name, :sub)
|
326
|
+
user_email = Struct::UserNameAndEmail.new "David", "sample@example.com"
|
327
|
+
user_birthday = Struct::UserNameAndDate.new "David", Date.new(2010, 01, 01)
|
328
|
+
custom = Struct::Custom.new "David", user_birthday
|
329
|
+
|
330
|
+
json_strings = ""
|
331
|
+
json_string_and_date = ""
|
332
|
+
json_custom = ""
|
333
|
+
|
334
|
+
assert_nothing_raised do
|
335
|
+
json_strings = user_email.to_json
|
336
|
+
json_string_and_date = user_birthday.to_json
|
337
|
+
json_custom = custom.to_json
|
338
|
+
end
|
339
|
+
|
340
|
+
assert_equal({ "name" => "David",
|
341
|
+
"sub" => {
|
342
|
+
"name" => "David",
|
343
|
+
"date" => "2010-01-01" } }, ActiveSupport::JSON.decode(json_custom))
|
344
|
+
|
345
|
+
assert_equal({ "name" => "David", "email" => "sample@example.com" },
|
346
|
+
ActiveSupport::JSON.decode(json_strings))
|
347
|
+
|
348
|
+
assert_equal({ "name" => "David", "date" => "2010-01-01" },
|
349
|
+
ActiveSupport::JSON.decode(json_string_and_date))
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_nil_true_and_false_represented_as_themselves
|
353
|
+
assert_nil nil.as_json
|
354
|
+
assert_equal true, true.as_json
|
355
|
+
assert_equal false, false.as_json
|
356
|
+
end
|
357
|
+
|
358
|
+
class HashWithAsJson < Hash
|
359
|
+
attr_accessor :as_json_called
|
360
|
+
|
361
|
+
def initialize(*)
|
362
|
+
super
|
363
|
+
end
|
364
|
+
|
365
|
+
def as_json(options = {})
|
366
|
+
@as_json_called = true
|
367
|
+
super
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_json_gem_dump_by_passing_active_support_encoder
|
372
|
+
h = HashWithAsJson.new
|
373
|
+
h[:foo] = "hello"
|
374
|
+
h[:bar] = "world"
|
375
|
+
|
376
|
+
assert_equal %({"foo":"hello","bar":"world"}), JSON.dump(h)
|
377
|
+
assert_nil h.as_json_called
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_json_gem_generate_by_passing_active_support_encoder
|
381
|
+
h = HashWithAsJson.new
|
382
|
+
h[:foo] = "hello"
|
383
|
+
h[:bar] = "world"
|
384
|
+
|
385
|
+
assert_equal %({"foo":"hello","bar":"world"}), JSON.generate(h)
|
386
|
+
assert_nil h.as_json_called
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_json_gem_pretty_generate_by_passing_active_support_encoder
|
390
|
+
h = HashWithAsJson.new
|
391
|
+
h[:foo] = "hello"
|
392
|
+
h[:bar] = "world"
|
393
|
+
|
394
|
+
assert_equal <<EXPECTED.chomp, JSON.pretty_generate(h)
|
395
|
+
{
|
396
|
+
"foo": "hello",
|
397
|
+
"bar": "world"
|
398
|
+
}
|
399
|
+
EXPECTED
|
400
|
+
assert_nil h.as_json_called
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
|
404
|
+
with_standard_json_time_format(false) do
|
405
|
+
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
406
|
+
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
407
|
+
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
|
412
|
+
with_standard_json_time_format(true) do
|
413
|
+
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
414
|
+
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
415
|
+
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_twz_to_json_with_custom_time_precision
|
420
|
+
with_standard_json_time_format(true) do
|
421
|
+
with_time_precision(0) do
|
422
|
+
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
423
|
+
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
424
|
+
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
def test_time_to_json_with_custom_time_precision
|
430
|
+
with_standard_json_time_format(true) do
|
431
|
+
with_time_precision(0) do
|
432
|
+
assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
|
433
|
+
end
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
def test_datetime_to_json_with_custom_time_precision
|
438
|
+
with_standard_json_time_format(true) do
|
439
|
+
with_time_precision(0) do
|
440
|
+
assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
|
441
|
+
end
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_twz_to_json_when_wrapping_a_date_time
|
446
|
+
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
447
|
+
time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
|
448
|
+
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_exception_to_json
|
452
|
+
exception = Exception.new("foo")
|
453
|
+
assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
|
454
|
+
end
|
455
|
+
|
456
|
+
class InfiniteNumber
|
457
|
+
def as_json(options = nil)
|
458
|
+
{ "number" => Float::INFINITY }
|
459
|
+
end
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_to_json_works_when_as_json_returns_infinite_number
|
463
|
+
assert_equal '{"number":null}', InfiniteNumber.new.to_json
|
464
|
+
end
|
465
|
+
|
466
|
+
class NaNNumber
|
467
|
+
def as_json(options = nil)
|
468
|
+
{ "number" => Float::NAN }
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
def test_to_json_works_when_as_json_returns_NaN_number
|
473
|
+
assert_equal '{"number":null}', NaNNumber.new.to_json
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_to_json_works_on_io_objects
|
477
|
+
assert_equal STDOUT.to_s.to_json, STDOUT.to_json
|
478
|
+
end
|
479
|
+
|
480
|
+
private
|
481
|
+
|
482
|
+
def object_keys(json_object)
|
483
|
+
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
|
484
|
+
end
|
485
|
+
|
486
|
+
def with_standard_json_time_format(boolean = true)
|
487
|
+
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
|
488
|
+
yield
|
489
|
+
ensure
|
490
|
+
ActiveSupport.use_standard_json_time_format = old
|
491
|
+
end
|
492
|
+
|
493
|
+
def with_time_precision(value)
|
494
|
+
old_value = ActiveSupport::JSON::Encoding.time_precision
|
495
|
+
ActiveSupport::JSON::Encoding.time_precision = value
|
496
|
+
yield
|
497
|
+
ensure
|
498
|
+
ActiveSupport::JSON::Encoding.time_precision = old_value
|
499
|
+
end
|
500
|
+
end
|