2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "securerandom"
|
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/core_ext/string/inflections"
|
|
|
|
require "active_support/json"
|
|
|
|
require "active_support/time"
|
|
|
|
require "time_zone_test_helpers"
|
|
|
|
require "json/encoding_test_cases"
|
2005-12-26 22:11:03 -05:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class TestJSONEncoding < ActiveSupport::TestCase
|
2014-06-18 07:46:04 -04:00
|
|
|
include TimeZoneTestHelpers
|
|
|
|
|
2010-02-10 17:46:16 -05:00
|
|
|
def sorted_json(json)
|
2016-08-06 12:03:25 -04:00
|
|
|
if json.start_with?("{") && json.end_with?("}")
|
|
|
|
"{" + json[1..-2].split(",").sort.join(",") + "}"
|
2016-07-22 17:13:42 -04:00
|
|
|
else
|
|
|
|
json
|
|
|
|
end
|
2010-02-10 17:46:16 -05:00
|
|
|
end
|
|
|
|
|
2015-07-11 08:48:16 -04:00
|
|
|
JSONTest::EncodingTestCases.constants.each do |class_tests|
|
2008-04-01 16:25:26 -04:00
|
|
|
define_method("test_#{class_tests[0..-6].underscore}") do
|
|
|
|
begin
|
2013-05-03 18:37:18 -04:00
|
|
|
prev = ActiveSupport.use_standard_json_time_format
|
|
|
|
|
2016-07-22 17:13:42 -04:00
|
|
|
standard_class_tests = /Standard/.match?(class_tests)
|
|
|
|
|
|
|
|
ActiveSupport.escape_html_entities_in_json = !standard_class_tests
|
|
|
|
ActiveSupport.use_standard_json_time_format = standard_class_tests
|
2015-07-11 08:48:16 -04:00
|
|
|
JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
|
2010-02-10 17:46:16 -05:00
|
|
|
assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
|
2008-04-01 16:25:26 -04:00
|
|
|
end
|
|
|
|
ensure
|
2008-04-07 23:45:26 -04:00
|
|
|
ActiveSupport.escape_html_entities_in_json = false
|
2013-05-03 18:37:18 -04:00
|
|
|
ActiveSupport.use_standard_json_time_format = prev
|
2005-12-26 22:11:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-11-11 13:25:26 -05:00
|
|
|
|
2013-09-11 21:36:23 -04:00
|
|
|
def test_process_status
|
2015-03-02 11:14:45 -05:00
|
|
|
rubinius_skip "https://github.com/rubinius/rubinius/issues/3334"
|
|
|
|
|
2013-09-11 21:36:23 -04:00
|
|
|
# There doesn't seem to be a good way to get a handle on a Process::Status object without actually
|
|
|
|
# creating a child process, hence this to populate $?
|
|
|
|
system("not_a_real_program_#{SecureRandom.hex}")
|
|
|
|
assert_equal %({"exitstatus":#{$?.exitstatus},"pid":#{$?.pid}}), ActiveSupport::JSON.encode($?)
|
|
|
|
end
|
|
|
|
|
2006-08-20 13:27:28 -04:00
|
|
|
def test_hash_encoding
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(a: :b)
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal %({\"a\":1}), ActiveSupport::JSON.encode("a" => 1)
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode("a" => [1, 2])
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2)
|
2007-09-30 16:57:50 -04:00
|
|
|
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(a: :b, c: :d))
|
2006-08-20 13:27:28 -04:00
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2015-06-15 14:23:01 -04:00
|
|
|
def test_hash_keys_encoding
|
|
|
|
ActiveSupport.escape_html_entities_in_json = true
|
|
|
|
assert_equal "{\"\\u003c\\u003e\":\"\\u003c\\u003e\"}", ActiveSupport::JSON.encode("<>" => "<>")
|
|
|
|
ensure
|
|
|
|
ActiveSupport.escape_html_entities_in_json = false
|
|
|
|
end
|
|
|
|
|
2011-12-20 12:22:21 -05:00
|
|
|
def test_utf8_string_encoded_properly
|
2016-08-06 12:03:25 -04:00
|
|
|
result = ActiveSupport::JSON.encode("€2.99")
|
2012-12-13 18:52:19 -05:00
|
|
|
assert_equal '"€2.99"', result
|
2011-12-25 06:34:58 -05:00
|
|
|
assert_equal(Encoding::UTF_8, result.encoding)
|
2011-12-20 12:22:21 -05:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
result = ActiveSupport::JSON.encode("✎☺")
|
2012-12-13 18:52:19 -05:00
|
|
|
assert_equal '"✎☺"', result
|
2011-12-25 06:34:58 -05:00
|
|
|
assert_equal(Encoding::UTF_8, result.encoding)
|
2005-12-26 22:11:03 -05:00
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2011-12-20 12:22:21 -05:00
|
|
|
def test_non_utf8_string_transcodes
|
2016-08-06 12:03:25 -04:00
|
|
|
s = "二".encode("Shift_JIS")
|
2011-12-20 12:22:21 -05:00
|
|
|
result = ActiveSupport::JSON.encode(s)
|
2012-12-13 18:52:19 -05:00
|
|
|
assert_equal '"二"', result
|
2011-12-20 12:22:21 -05:00
|
|
|
assert_equal Encoding::UTF_8, result.encoding
|
2010-04-26 22:32:23 -04:00
|
|
|
end
|
|
|
|
|
2012-12-13 18:52:19 -05:00
|
|
|
def test_wide_utf8_chars
|
2016-08-06 12:03:25 -04:00
|
|
|
w = "𠜎"
|
2012-12-13 18:52:19 -05:00
|
|
|
result = ActiveSupport::JSON.encode(w)
|
|
|
|
assert_equal '"𠜎"', result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wide_utf8_roundtrip
|
|
|
|
hash = { string: "𐒑" }
|
|
|
|
json = ActiveSupport::JSON.encode(hash)
|
|
|
|
decoded_hash = ActiveSupport::JSON.decode(json)
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "𐒑", decoded_hash["string"]
|
2012-12-13 18:52:19 -05:00
|
|
|
end
|
|
|
|
|
2007-09-30 16:57:50 -04:00
|
|
|
def test_hash_key_identifiers_are_always_quoted
|
2016-08-16 03:30:11 -04:00
|
|
|
values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
|
2009-04-23 03:08:40 -04:00
|
|
|
assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
|
2007-03-18 03:05:58 -04:00
|
|
|
end
|
2007-09-30 16:57:50 -04:00
|
|
|
|
2007-10-03 23:28:42 -04:00
|
|
|
def test_hash_should_allow_key_filtering_with_only
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_equal %({"a":1}), ActiveSupport::JSON.encode({ "a" => 1, :b => 2, :c => 3 }, { only: "a" })
|
2007-10-03 23:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash_should_allow_key_filtering_with_except
|
2017-08-12 07:31:46 -04:00
|
|
|
assert_equal %({"b":2}), ActiveSupport::JSON.encode({ "foo" => "bar", :b => 2, :c => 3 }, { except: ["foo", :c] })
|
2007-10-03 23:28:42 -04:00
|
|
|
end
|
2010-04-28 06:24:53 -04:00
|
|
|
|
2008-05-18 11:48:11 -04:00
|
|
|
def test_time_to_json_includes_local_offset
|
2014-01-26 15:32:34 -05:00
|
|
|
with_standard_json_time_format(true) do
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005, 2, 1, 15, 15, 10))
|
2014-01-26 15:32:34 -05:00
|
|
|
end
|
2008-05-18 11:48:11 -04:00
|
|
|
end
|
|
|
|
end
|
2007-10-03 23:28:42 -04:00
|
|
|
|
2010-07-01 18:03:18 -04:00
|
|
|
def test_hash_with_time_to_json
|
2014-01-26 15:32:34 -05:00
|
|
|
with_standard_json_time_format(false) do
|
2016-08-06 13:38:33 -04:00
|
|
|
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { time: Time.utc(2009) }.to_json
|
2014-01-26 15:32:34 -05:00
|
|
|
end
|
2010-07-01 18:03:18 -04:00
|
|
|
end
|
|
|
|
|
2008-10-12 16:40:00 -04:00
|
|
|
def test_nested_hash_with_float
|
|
|
|
assert_nothing_raised do
|
|
|
|
hash = {
|
|
|
|
"CHI" => {
|
2016-08-06 13:38:33 -04:00
|
|
|
display_name: "chicago",
|
|
|
|
latitude: 123.234
|
2008-10-12 16:40:00 -04:00
|
|
|
}
|
|
|
|
}
|
2010-07-22 23:36:34 -04:00
|
|
|
ActiveSupport::JSON.encode(hash)
|
2008-10-12 16:40:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-05 22:22:03 -05:00
|
|
|
def test_hash_like_with_options
|
2015-07-11 08:48:16 -04:00
|
|
|
h = JSONTest::Hashlike.new
|
2016-08-06 13:38:33 -04:00
|
|
|
json = h.to_json only: [:foo]
|
2013-11-05 22:22:03 -05:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
2013-11-05 22:22:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_object_to_json_with_options
|
|
|
|
obj = Object.new
|
|
|
|
obj.instance_variable_set :@foo, "hello"
|
|
|
|
obj.instance_variable_set :@bar, "world"
|
2016-08-06 13:38:33 -04:00
|
|
|
json = obj.to_json only: ["foo"]
|
2013-11-05 22:22:03 -05:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
2013-11-05 22:22:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_struct_to_json_with_options
|
|
|
|
struct = Struct.new(:foo, :bar).new
|
|
|
|
struct.foo = "hello"
|
|
|
|
struct.bar = "world"
|
2016-08-06 13:38:33 -04:00
|
|
|
json = struct.to_json only: [:foo]
|
2013-11-05 22:22:03 -05:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
2013-11-05 22:22:03 -05:00
|
|
|
end
|
|
|
|
|
2018-10-11 15:28:52 -04:00
|
|
|
def test_struct_to_json_with_options_nested
|
|
|
|
klass = Struct.new(:foo, :bar)
|
|
|
|
struct = klass.new "hello", "world"
|
|
|
|
parent_struct = klass.new struct, "world"
|
|
|
|
json = parent_struct.to_json only: [:foo]
|
|
|
|
|
|
|
|
assert_equal({ "foo" => { "foo" => "hello" } }, JSON.parse(json))
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-08-29 10:10:31 -04:00
|
|
|
def test_hash_should_pass_encoding_options_to_children_in_as_json
|
|
|
|
person = {
|
2016-08-06 13:38:33 -04:00
|
|
|
name: "John",
|
|
|
|
address: {
|
|
|
|
city: "London",
|
|
|
|
country: "UK"
|
2010-08-29 10:10:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-06 13:38:33 -04:00
|
|
|
json = person.as_json only: [:address, :city]
|
2010-08-29 10:10:31 -04:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "address" => { "city" => "London" } }, json)
|
2010-08-29 10:10:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash_should_pass_encoding_options_to_children_in_to_json
|
|
|
|
person = {
|
2016-08-06 13:38:33 -04:00
|
|
|
name: "John",
|
|
|
|
address: {
|
|
|
|
city: "London",
|
|
|
|
country: "UK"
|
2010-08-29 10:10:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-06 13:38:33 -04:00
|
|
|
json = person.to_json only: [:address, :city]
|
2010-08-29 10:10:31 -04:00
|
|
|
|
|
|
|
assert_equal(%({"address":{"city":"London"}}), json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_should_pass_encoding_options_to_children_in_as_json
|
|
|
|
people = [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ name: "John", address: { city: "London", country: "UK" } },
|
2017-12-12 06:00:50 -05:00
|
|
|
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
2010-08-29 10:10:31 -04:00
|
|
|
]
|
2016-08-06 13:38:33 -04:00
|
|
|
json = people.as_json only: [:address, :city]
|
2010-08-29 10:10:31 -04:00
|
|
|
expected = [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ "address" => { "city" => "London" } },
|
|
|
|
{ "address" => { "city" => "Paris" } }
|
2010-08-29 10:10:31 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal(expected, json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_should_pass_encoding_options_to_children_in_to_json
|
|
|
|
people = [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ name: "John", address: { city: "London", country: "UK" } },
|
2017-12-12 06:00:50 -05:00
|
|
|
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
2010-08-29 10:10:31 -04:00
|
|
|
]
|
2016-08-06 13:38:33 -04:00
|
|
|
json = people.to_json only: [:address, :city]
|
2010-08-29 10:10:31 -04:00
|
|
|
|
|
|
|
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
|
|
|
end
|
|
|
|
|
2013-04-20 14:06:22 -04:00
|
|
|
People = Class.new(BasicObject) do
|
|
|
|
include Enumerable
|
2017-11-27 01:16:12 -05:00
|
|
|
def initialize
|
2013-04-20 14:06:22 -04:00
|
|
|
@people = [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ name: "John", address: { city: "London", country: "UK" } },
|
2017-12-12 06:00:50 -05:00
|
|
|
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
2013-04-20 14:06:22 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
def each(*, &blk)
|
|
|
|
@people.each do |p|
|
|
|
|
yield p if blk
|
|
|
|
p
|
|
|
|
end.each
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enumerable_should_generate_json_with_as_json
|
2016-08-06 13:38:33 -04:00
|
|
|
json = People.new.as_json only: [:address, :city]
|
2013-04-20 14:06:22 -04:00
|
|
|
expected = [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ "address" => { "city" => "London" } },
|
|
|
|
{ "address" => { "city" => "Paris" } }
|
2011-04-01 15:14:53 -04:00
|
|
|
]
|
2013-04-20 14:06:22 -04:00
|
|
|
|
|
|
|
assert_equal(expected, json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enumerable_should_generate_json_with_to_json
|
2016-08-06 13:38:33 -04:00
|
|
|
json = People.new.to_json only: [:address, :city]
|
2013-04-20 14:06:22 -04:00
|
|
|
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enumerable_should_pass_encoding_options_to_children_in_as_json
|
2016-08-06 13:38:33 -04:00
|
|
|
json = People.new.each.as_json only: [:address, :city]
|
2011-04-01 15:14:53 -04:00
|
|
|
expected = [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ "address" => { "city" => "London" } },
|
|
|
|
{ "address" => { "city" => "Paris" } }
|
2011-04-01 15:14:53 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
assert_equal(expected, json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enumerable_should_pass_encoding_options_to_children_in_to_json
|
2016-08-06 13:38:33 -04:00
|
|
|
json = People.new.each.to_json only: [:address, :city]
|
2011-04-01 15:14:53 -04:00
|
|
|
|
|
|
|
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
|
|
|
end
|
|
|
|
|
2015-07-11 08:48:16 -04:00
|
|
|
class CustomWithOptions
|
|
|
|
attr_accessor :foo, :bar
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def as_json(options = {})
|
2015-07-11 08:48:16 -04:00
|
|
|
options[:only] = %w(foo bar)
|
|
|
|
super(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-15 13:29:38 -05:00
|
|
|
def test_hash_to_json_should_not_keep_options_around
|
2012-11-12 08:33:20 -05:00
|
|
|
f = CustomWithOptions.new
|
|
|
|
f.foo = "hello"
|
|
|
|
f.bar = "world"
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
hash = { "foo" => f, "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "foo" => { "foo" => "hello", "bar" => "world" },
|
|
|
|
"other_hash" => { "foo" => "other_foo", "test" => "other_test" } }, ActiveSupport::JSON.decode(hash.to_json))
|
2012-11-12 08:33:20 -05:00
|
|
|
end
|
|
|
|
|
2013-11-15 13:29:38 -05:00
|
|
|
def test_array_to_json_should_not_keep_options_around
|
|
|
|
f = CustomWithOptions.new
|
|
|
|
f.foo = "hello"
|
|
|
|
f.bar = "world"
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
array = [f, { "foo" => "other_foo", "test" => "other_test" }]
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal([{ "foo" => "hello", "bar" => "world" },
|
|
|
|
{ "foo" => "other_foo", "test" => "other_test" }], ActiveSupport::JSON.decode(array.to_json))
|
2013-11-15 13:29:38 -05:00
|
|
|
end
|
|
|
|
|
2015-07-11 08:48:16 -04:00
|
|
|
class OptionsTest
|
|
|
|
def as_json(options = :default)
|
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-22 14:01:40 -05:00
|
|
|
def test_hash_as_json_without_options
|
|
|
|
json = { foo: OptionsTest.new }.as_json
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "foo" => :default }, json)
|
2013-11-22 14:01:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_as_json_without_options
|
|
|
|
json = [ OptionsTest.new ].as_json
|
|
|
|
assert_equal([:default], json)
|
|
|
|
end
|
|
|
|
|
2011-01-12 19:16:16 -05:00
|
|
|
def test_struct_encoding
|
2016-08-06 12:03:25 -04:00
|
|
|
Struct.new("UserNameAndEmail", :name, :email)
|
|
|
|
Struct.new("UserNameAndDate", :name, :date)
|
|
|
|
Struct.new("Custom", :name, :sub)
|
|
|
|
user_email = Struct::UserNameAndEmail.new "David", "sample@example.com"
|
|
|
|
user_birthday = Struct::UserNameAndDate.new "David", Date.new(2010, 01, 01)
|
|
|
|
custom = Struct::Custom.new "David", user_birthday
|
2011-01-12 19:16:16 -05:00
|
|
|
|
|
|
|
json_strings = ""
|
|
|
|
json_string_and_date = ""
|
|
|
|
json_custom = ""
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
json_strings = user_email.to_json
|
|
|
|
json_string_and_date = user_birthday.to_json
|
|
|
|
json_custom = custom.to_json
|
|
|
|
end
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "name" => "David",
|
2011-01-17 19:06:55 -05:00
|
|
|
"sub" => {
|
|
|
|
"name" => "David",
|
2016-08-16 03:30:11 -04:00
|
|
|
"date" => "2010-01-01" } }, ActiveSupport::JSON.decode(json_custom))
|
2011-01-17 19:06:55 -05:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "name" => "David", "email" => "sample@example.com" },
|
2013-11-05 23:05:58 -05:00
|
|
|
ActiveSupport::JSON.decode(json_strings))
|
2011-01-17 19:06:55 -05:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "name" => "David", "date" => "2010-01-01" },
|
2013-11-05 23:05:58 -05:00
|
|
|
ActiveSupport::JSON.decode(json_string_and_date))
|
2011-01-12 19:16:16 -05:00
|
|
|
end
|
2010-08-29 10:10:31 -04:00
|
|
|
|
2012-05-29 17:40:43 -04:00
|
|
|
def test_nil_true_and_false_represented_as_themselves
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil nil.as_json
|
2012-05-29 17:40:43 -04:00
|
|
|
assert_equal true, true.as_json
|
|
|
|
assert_equal false, false.as_json
|
|
|
|
end
|
|
|
|
|
2015-07-11 08:48:16 -04:00
|
|
|
class HashWithAsJson < Hash
|
|
|
|
attr_accessor :as_json_called
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def as_json(options = {})
|
2015-07-11 08:48:16 -04:00
|
|
|
@as_json_called = true
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-12 04:57:21 -05:00
|
|
|
def test_json_gem_dump_by_passing_active_support_encoder
|
|
|
|
h = HashWithAsJson.new
|
|
|
|
h[:foo] = "hello"
|
|
|
|
h[:bar] = "world"
|
|
|
|
|
|
|
|
assert_equal %({"foo":"hello","bar":"world"}), JSON.dump(h)
|
|
|
|
assert_nil h.as_json_called
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_json_gem_generate_by_passing_active_support_encoder
|
|
|
|
h = HashWithAsJson.new
|
|
|
|
h[:foo] = "hello"
|
|
|
|
h[:bar] = "world"
|
|
|
|
|
|
|
|
assert_equal %({"foo":"hello","bar":"world"}), JSON.generate(h)
|
|
|
|
assert_nil h.as_json_called
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_json_gem_pretty_generate_by_passing_active_support_encoder
|
|
|
|
h = HashWithAsJson.new
|
|
|
|
h[:foo] = "hello"
|
|
|
|
h[:bar] = "world"
|
|
|
|
|
|
|
|
assert_equal <<EXPECTED.chomp, JSON.pretty_generate(h)
|
|
|
|
{
|
|
|
|
"foo": "hello",
|
|
|
|
"bar": "world"
|
|
|
|
}
|
|
|
|
EXPECTED
|
|
|
|
assert_nil h.as_json_called
|
|
|
|
end
|
|
|
|
|
2014-01-26 15:32:34 -05:00
|
|
|
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
|
|
|
|
with_standard_json_time_format(false) do
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2014-01-26 15:32:34 -05:00
|
|
|
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
|
|
|
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
|
|
|
|
with_standard_json_time_format(true) do
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2014-01-26 15:32:34 -05:00
|
|
|
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
|
|
|
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-26 15:39:16 -05:00
|
|
|
def test_twz_to_json_with_custom_time_precision
|
2014-01-26 15:32:34 -05:00
|
|
|
with_standard_json_time_format(true) do
|
2014-06-22 06:36:07 -04:00
|
|
|
with_time_precision(0) do
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2014-06-22 06:36:07 -04:00
|
|
|
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
|
|
|
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
|
|
|
|
end
|
2014-01-26 15:32:34 -05:00
|
|
|
end
|
2014-01-26 16:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_to_json_with_custom_time_precision
|
|
|
|
with_standard_json_time_format(true) do
|
2014-06-22 06:36:07 -04:00
|
|
|
with_time_precision(0) do
|
|
|
|
assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
|
|
|
|
end
|
2014-01-26 16:09:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_datetime_to_json_with_custom_time_precision
|
|
|
|
with_standard_json_time_format(true) do
|
2014-06-22 06:36:07 -04:00
|
|
|
with_time_precision(0) do
|
|
|
|
assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
|
|
|
|
end
|
2014-01-26 16:09:06 -05:00
|
|
|
end
|
2014-01-26 15:32:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_twz_to_json_when_wrapping_a_date_time
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2014-01-26 15:32:34 -05:00
|
|
|
time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
|
|
|
|
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
|
|
|
|
end
|
|
|
|
|
2016-02-15 21:35:52 -05:00
|
|
|
def test_exception_to_json
|
|
|
|
exception = Exception.new("foo")
|
|
|
|
assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
|
|
|
|
end
|
|
|
|
|
2016-10-30 04:42:43 -04:00
|
|
|
class InfiniteNumber
|
|
|
|
def as_json(options = nil)
|
2016-11-13 10:31:05 -05:00
|
|
|
{ "number" => Float::INFINITY }
|
2016-10-30 04:42:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_json_works_when_as_json_returns_infinite_number
|
2016-11-13 10:31:05 -05:00
|
|
|
assert_equal '{"number":null}', InfiniteNumber.new.to_json
|
2016-10-30 04:42:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class NaNNumber
|
|
|
|
def as_json(options = nil)
|
2016-11-13 10:59:13 -05:00
|
|
|
{ "number" => Float::NAN }
|
2016-10-30 04:42:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_json_works_when_as_json_returns_NaN_number
|
2016-11-13 10:31:05 -05:00
|
|
|
assert_equal '{"number":null}', NaNNumber.new.to_json
|
2016-10-30 04:42:43 -04:00
|
|
|
end
|
|
|
|
|
2017-10-23 05:47:31 -04:00
|
|
|
def test_to_json_works_on_io_objects
|
|
|
|
assert_equal STDOUT.to_s.to_json, STDOUT.to_json
|
|
|
|
end
|
|
|
|
|
2016-12-23 06:31:57 -05:00
|
|
|
private
|
2007-12-14 21:28:20 -05:00
|
|
|
|
2007-03-18 03:05:58 -04:00
|
|
|
def object_keys(json_object)
|
|
|
|
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
|
|
|
|
end
|
2010-04-28 06:24:53 -04:00
|
|
|
|
2014-01-26 15:32:34 -05:00
|
|
|
def with_standard_json_time_format(boolean = true)
|
|
|
|
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveSupport.use_standard_json_time_format = old
|
|
|
|
end
|
2014-06-22 06:36:07 -04:00
|
|
|
|
|
|
|
def with_time_precision(value)
|
|
|
|
old_value = ActiveSupport::JSON::Encoding.time_precision
|
|
|
|
ActiveSupport::JSON::Encoding.time_precision = value
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveSupport::JSON::Encoding.time_precision = old_value
|
|
|
|
end
|
2005-12-26 22:11:03 -05:00
|
|
|
end
|