Namespace
- MODULE TestJSONEncoding::JSONTest
- CLASS TestJSONEncoding::CustomWithOptions
- CLASS TestJSONEncoding::HashWithAsJson
- CLASS TestJSONEncoding::OptionsTest
Methods
- O
- S
- T
-
- test_array_as_json_without_options,
- test_array_should_pass_encoding_options_to_children_in_as_json,
- test_array_should_pass_encoding_options_to_children_in_to_json,
- test_array_to_json_should_not_keep_options_around,
- test_datetime_to_json_with_custom_time_precision,
- test_enumerable_should_generate_json_with_as_json,
- test_enumerable_should_generate_json_with_to_json,
- test_enumerable_should_pass_encoding_options_to_children_in_as_json,
- test_enumerable_should_pass_encoding_options_to_children_in_to_json,
- test_exception_raised_when_encoding_circular_reference_in_array,
- test_exception_raised_when_encoding_circular_reference_in_hash,
- test_exception_raised_when_encoding_circular_reference_in_hash_inside_array,
- test_hash_as_json_without_options,
- test_hash_encoding,
- test_hash_key_identifiers_are_always_quoted,
- test_hash_keys_encoding,
- test_hash_like_with_options,
- test_hash_should_allow_key_filtering_with_except,
- test_hash_should_allow_key_filtering_with_only,
- test_hash_should_pass_encoding_options_to_children_in_as_json,
- test_hash_should_pass_encoding_options_to_children_in_to_json,
- test_hash_to_json_should_not_keep_options_around,
- test_hash_with_time_to_json,
- test_json_gem_dump_by_passing_active_support_encoder,
- test_json_gem_generate_by_passing_active_support_encoder,
- test_json_gem_pretty_generate_by_passing_active_support_encoder,
- test_nested_hash_with_float,
- test_nil_true_and_false_represented_as_themselves,
- test_non_utf8_string_transcodes,
- test_object_to_json_with_options,
- test_process_status,
- test_reading_encode_big_decimal_as_string_option,
- test_setting_deprecated_encode_big_decimal_as_string_option,
- test_struct_encoding,
- test_struct_to_json_with_options,
- test_time_to_json_includes_local_offset,
- test_time_to_json_with_custom_time_precision,
- test_twz_to_json_when_wrapping_a_date_time,
- test_twz_to_json_with_custom_time_precision,
- test_twz_to_json_with_use_standard_json_time_format_config_set_to_false,
- test_twz_to_json_with_use_standard_json_time_format_config_set_to_true,
- test_utf8_string_encoded_properly,
- test_wide_utf8_chars,
- test_wide_utf8_roundtrip
- W
Included Modules
Constants
| People | = | Class.new(BasicObject) do include Enumerable def initialize() @people = [ { :name => 'John', :address => { :city => 'London', :country => 'UK' }}, { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }} ] end def each(*, &blk) @people.each do |p| yield p if blk p end.each end end |
Instance Public methods
sorted_json(json)
Link
test_array_as_json_without_options()
Link
test_array_should_pass_encoding_options_to_children_in_as_json()
Link
# File activesupport/test/json/encoding_test.rb, line 217 def test_array_should_pass_encoding_options_to_children_in_as_json people = [ { :name => 'John', :address => { :city => 'London', :country => 'UK' }}, { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }} ] json = people.as_json :only => [:address, :city] expected = [ { 'address' => { 'city' => 'London' }}, { 'address' => { 'city' => 'Paris' }} ] assert_equal(expected, json) end
test_array_should_pass_encoding_options_to_children_in_to_json()
Link
# File activesupport/test/json/encoding_test.rb, line 231 def test_array_should_pass_encoding_options_to_children_in_to_json people = [ { :name => 'John', :address => { :city => 'London', :country => 'UK' }}, { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }} ] json = people.to_json :only => [:address, :city] assert_equal(%Q([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json) end
test_array_to_json_should_not_keep_options_around()
Link
# File activesupport/test/json/encoding_test.rb, line 307 def test_array_to_json_should_not_keep_options_around f = CustomWithOptions.new f.foo = "hello" f.bar = "world" array = [f, {"foo" => "other_foo", "test" => "other_test"}] assert_equal([{"foo"=>"hello","bar"=>"world"}, {"foo"=>"other_foo","test"=>"other_test"}], ActiveSupport::JSON.decode(array.to_json)) end
test_datetime_to_json_with_custom_time_precision()
Link
# File activesupport/test/json/encoding_test.rb, line 449 def test_datetime_to_json_with_custom_time_precision with_standard_json_time_format(true) do with_time_precision(0) do assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000)) end end end
test_enumerable_should_generate_json_with_as_json()
Link
# File activesupport/test/json/encoding_test.rb, line 257 def test_enumerable_should_generate_json_with_as_json json = People.new.as_json :only => [:address, :city] expected = [ { 'address' => { 'city' => 'London' }}, { 'address' => { 'city' => 'Paris' }} ] assert_equal(expected, json) end
test_enumerable_should_generate_json_with_to_json()
Link
test_enumerable_should_pass_encoding_options_to_children_in_as_json()
Link
# File activesupport/test/json/encoding_test.rb, line 272 def test_enumerable_should_pass_encoding_options_to_children_in_as_json json = People.new.each.as_json :only => [:address, :city] expected = [ { 'address' => { 'city' => 'London' }}, { 'address' => { 'city' => 'Paris' }} ] assert_equal(expected, json) end
test_enumerable_should_pass_encoding_options_to_children_in_to_json()
Link
# File activesupport/test/json/encoding_test.rb, line 282 def test_enumerable_should_pass_encoding_options_to_children_in_to_json json = People.new.each.to_json :only => [:address, :city] assert_equal(%Q([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json) end
test_exception_raised_when_encoding_circular_reference_in_array()
Link
test_exception_raised_when_encoding_circular_reference_in_hash()
Link
# File activesupport/test/json/encoding_test.rb, line 111 def test_exception_raised_when_encoding_circular_reference_in_hash a = { :name => 'foo' } a[:next] = a assert_deprecated do assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) } end end
test_exception_raised_when_encoding_circular_reference_in_hash_inside_array()
Link
# File activesupport/test/json/encoding_test.rb, line 119 def test_exception_raised_when_encoding_circular_reference_in_hash_inside_array a = { :name => 'foo', :sub => [] } a[:sub] << a assert_deprecated do assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) } end end
test_hash_as_json_without_options()
Link
test_hash_encoding()
Link
# File activesupport/test/json/encoding_test.rb, line 41 def test_hash_encoding assert_equal %Q({\"a\":\"b\"}), ActiveSupport::JSON.encode(:a => :b) assert_equal %Q({\"a\":1}), ActiveSupport::JSON.encode('a' => 1) assert_equal %Q({\"a\":[1,2]}), ActiveSupport::JSON.encode('a' => [1,2]) assert_equal %Q({"1":2}), ActiveSupport::JSON.encode(1 => 2) assert_equal %Q({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(:a => :b, :c => :d)) end
test_hash_key_identifiers_are_always_quoted()
Link
# File activesupport/test/json/encoding_test.rb, line 127 def test_hash_key_identifiers_are_always_quoted values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"} assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values)) end
test_hash_keys_encoding()
Link
# File activesupport/test/json/encoding_test.rb, line 50 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
test_hash_like_with_options()
Link
test_hash_should_allow_key_filtering_with_except()
Link
test_hash_should_allow_key_filtering_with_only()
Link
test_hash_should_pass_encoding_options_to_children_in_as_json()
Link
# File activesupport/test/json/encoding_test.rb, line 191 def test_hash_should_pass_encoding_options_to_children_in_as_json person = { :name => 'John', :address => { :city => 'London', :country => 'UK' } } json = person.as_json :only => [:address, :city] assert_equal({ 'address' => { 'city' => 'London' }}, json) end
test_hash_should_pass_encoding_options_to_children_in_to_json()
Link
# File activesupport/test/json/encoding_test.rb, line 204 def test_hash_should_pass_encoding_options_to_children_in_to_json person = { :name => 'John', :address => { :city => 'London', :country => 'UK' } } json = person.to_json :only => [:address, :city] assert_equal(%Q({"address":{"city":"London"}}), json) end
test_hash_to_json_should_not_keep_options_around()
Link
# File activesupport/test/json/encoding_test.rb, line 297 def test_hash_to_json_should_not_keep_options_around f = CustomWithOptions.new f.foo = "hello" f.bar = "world" hash = {"foo" => f, "other_hash" => {"foo" => "other_foo", "test" => "other_test"}} assert_equal({"foo"=>{"foo"=>"hello","bar"=>"world"}, "other_hash" => {"foo"=>"other_foo","test"=>"other_test"}}, ActiveSupport::JSON.decode(hash.to_json)) end
test_hash_with_time_to_json()
Link
test_json_gem_dump_by_passing_active_support_encoder()
Link
test_json_gem_generate_by_passing_active_support_encoder()
Link
test_json_gem_pretty_generate_by_passing_active_support_encoder()
Link
# File activesupport/test/json/encoding_test.rb, line 401 def test_json_gem_pretty_generate_by_passing_active_support_encoder h = HashWithAsJson.new h[:foo] = "hello" h[:bar] = "world" assert_equal "{ "foo": "hello", "bar": "world" } ".chomp, JSON.pretty_generate(h) assert_nil h.as_json_called end
test_nested_hash_with_float()
Link
test_nil_true_and_false_represented_as_themselves()
Link
test_non_utf8_string_transcodes()
Link
test_object_to_json_with_options()
Link
# File activesupport/test/json/encoding_test.rb, line 173 def test_object_to_json_with_options obj = Object.new obj.instance_variable_set :@foo, "hello" obj.instance_variable_set :@bar, "world" json = obj.to_json :only => ["foo"] assert_equal({"foo"=>"hello"}, JSON.parse(json)) end
test_process_status()
Link
# File activesupport/test/json/encoding_test.rb, line 34 def test_process_status # 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 %Q({"exitstatus":#{$?.exitstatus},"pid":#{$?.pid}}), ActiveSupport::JSON.encode($?) end
test_reading_encode_big_decimal_as_string_option()
Link
test_setting_deprecated_encode_big_decimal_as_string_option()
Link
# File activesupport/test/json/encoding_test.rb, line 93 def test_setting_deprecated_encode_big_decimal_as_string_option assert_raise(NotImplementedError) do ActiveSupport.encode_big_decimal_as_string = true end assert_raise(NotImplementedError) do ActiveSupport.encode_big_decimal_as_string = false end end
test_struct_encoding()
Link
# File activesupport/test/json/encoding_test.rb, line 333 def test_struct_encoding 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 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 assert_equal({"name" => "David", "sub" => { "name" => "David", "date" => "2010-01-01" }}, ActiveSupport::JSON.decode(json_custom)) assert_equal({"name" => "David", "email" => "sample@example.com"}, ActiveSupport::JSON.decode(json_strings)) assert_equal({"name" => "David", "date" => "2010-01-01"}, ActiveSupport::JSON.decode(json_string_and_date)) end
test_struct_to_json_with_options()
Link
test_time_to_json_includes_local_offset()
Link
# File activesupport/test/json/encoding_test.rb, line 140 def test_time_to_json_includes_local_offset with_standard_json_time_format(true) do with_env_tz 'US/Eastern' do assert_equal %Q("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10)) end end end
test_time_to_json_with_custom_time_precision()
Link
test_twz_to_json_when_wrapping_a_date_time()
Link
# File activesupport/test/json/encoding_test.rb, line 457 def test_twz_to_json_when_wrapping_a_date_time zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone) assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time) end
test_twz_to_json_with_custom_time_precision()
Link
# File activesupport/test/json/encoding_test.rb, line 431 def test_twz_to_json_with_custom_time_precision with_standard_json_time_format(true) do with_time_precision(0) do zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone) assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time) end end end
test_twz_to_json_with_use_standard_json_time_format_config_set_to_false()
Link
# File activesupport/test/json/encoding_test.rb, line 415 def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false with_standard_json_time_format(false) do zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone) assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time) end end
test_twz_to_json_with_use_standard_json_time_format_config_set_to_true()
Link
# File activesupport/test/json/encoding_test.rb, line 423 def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true with_standard_json_time_format(true) do zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone) assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time) end end
test_utf8_string_encoded_properly()
Link
# File activesupport/test/json/encoding_test.rb, line 57 def test_utf8_string_encoded_properly result = ActiveSupport::JSON.encode('€2.99') assert_equal '"€2.99"', result assert_equal(Encoding::UTF_8, result.encoding) result = ActiveSupport::JSON.encode('✎☺') assert_equal '"✎☺"', result assert_equal(Encoding::UTF_8, result.encoding) end
test_wide_utf8_chars()
Link
test_wide_utf8_roundtrip()
Link
Instance Protected methods
object_keys(json_object)
Link
with_standard_json_time_format(boolean = true)
Link
# File activesupport/test/json/encoding_test.rb, line 469 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
with_time_precision(value)
Link