Namespace
- CLASS TestJSONEncoding::Custom
- CLASS TestJSONEncoding::CustomWithOptions
- CLASS TestJSONEncoding::Foo
- CLASS TestJSONEncoding::Hashlike
Methods
- O
- S
- T
-
- test_array_should_pass_encoding_options_to_children_in_as_json,
- test_array_should_pass_encoding_options_to_children_in_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_encoding,
- test_hash_key_identifiers_are_always_quoted,
- 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_with_time_to_json,
- test_nested_hash_with_float,
- test_nil_true_and_false_represented_as_themselves,
- test_non_utf8_string_transcodes,
- test_struct_encoding,
- test_time_to_json_includes_local_offset,
- test_to_json_should_not_keep_options_around,
- test_utf8_hash_key_does_not_change_the_encoding,
- test_utf8_string_encoded_properly_when_kcode_is_utf8
- W
Constants
| TrueTests | = | [[ true, %(true) ]] |
| FalseTests | = | [[ false, %(false) ]] |
| NilTests | = | [[ nil, %(null) ]] |
| NumericTests | = | [[ 1, %(1) ], [ 2.5, %(2.5) ], [ BigDecimal('2.5'), %("#{BigDecimal('2.5').to_s}") ]] |
| StringTests | = | [[ 'this is the <string>', %("this is the \\u003Cstring\\u003E")], [ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ], [ 'http://test.host/posts/1', %("http://test.host/posts/1")], [ "Control characters: \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", %("Control characters: \\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000B\\f\\r\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F") ]] |
| ArrayTests | = | [[ ['a', 'b', 'c'], %([\"a\",\"b\",\"c\"]) ], [ [1, 'a', :b, nil, false], %([1,\"a\",\"b\",null,false]) ]] |
| SymbolTests | = | [[ :a, %("a") ], [ :this, %("this") ], [ :"a b", %("a b") ]] |
| ObjectTests | = | [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]] |
| HashlikeTests | = | [[ Hashlike.new, %({\"a\":1}) ]] |
| CustomTests | = | [[ Custom.new, '"custom"' ]] |
| VariableTests | = | [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'], [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']] |
| RegexpTests | = | [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']] |
| DateTests | = | [[ Date.new(2005,2,1), %("2005/02/01") ]] |
| TimeTests | = | [[ Time.utc(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] |
| DateTimeTests | = | [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]] |
| StandardDateTests | = | [[ Date.new(2005,2,1), %("2005-02-01") ]] |
| StandardTimeTests | = | [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10Z") ]] |
| StandardDateTimeTests | = | [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]] |
| StandardStringTests | = | [[ 'this is the <string>', %("this is the <string>")]] |
Instance Public methods
sorted_json(json)
Link
test_array_should_pass_encoding_options_to_children_in_as_json()
Link
# File activesupport/test/json/encoding_test.rb, line 216 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 230 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_enumerable_should_pass_encoding_options_to_children_in_as_json()
Link
# File activesupport/test/json/encoding_test.rb, line 240 def test_enumerable_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.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 254 def test_enumerable_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.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
test_exception_raised_when_encoding_circular_reference_in_hash_inside_array()
Link
# File activesupport/test/json/encoding_test.rb, line 146 def test_exception_raised_when_encoding_circular_reference_in_hash_inside_array a = { :name => 'foo', :sub => [] } a[:sub] << a assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) } end
test_hash_encoding()
Link
# File activesupport/test/json/encoding_test.rb, line 90 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 152 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_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 190 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 203 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_with_time_to_json()
Link
test_nested_hash_with_float()
Link
test_nil_true_and_false_represented_as_themselves()
Link
test_non_utf8_string_transcodes()
Link
test_struct_encoding()
Link
# File activesupport/test/json/encoding_test.rb, line 274 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" }}, JSON.parse(json_custom)) assert_equal({"name" => "David", "email" => "sample@example.com"}, JSON.parse(json_strings)) assert_equal({"name" => "David", "date" => "2010/01/01"}, JSON.parse(json_string_and_date)) end
test_time_to_json_includes_local_offset()
Link
# File activesupport/test/json/encoding_test.rb, line 165 def test_time_to_json_includes_local_offset ActiveSupport.use_standard_json_time_format = true with_env_tz 'US/Eastern' do assert_equal %Q("2005-02-01T15:15:10-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10)) end ensure ActiveSupport.use_standard_json_time_format = false end
test_to_json_should_not_keep_options_around()
Link
# File activesupport/test/json/encoding_test.rb, line 264 def test_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"} }, JSON.parse(hash.to_json)) end
test_utf8_hash_key_does_not_change_the_encoding()
Link
# File activesupport/test/json/encoding_test.rb, line 119 def test_utf8_hash_key_does_not_change_the_encoding w = { '𠜎' => 'a' } result = ActiveSupport::JSON.encode(w) assert_equal '{"\u070e":"a"}', result if RUBY_VERSION >= '2.0' expected_encoding = Encoding::UTF_8 else expected_encoding = Encoding::US_ASCII end assert_equal expected_encoding, result.encoding end
test_utf8_string_encoded_properly_when_kcode_is_utf8()
Link
# File activesupport/test/json/encoding_test.rb, line 99 def test_utf8_string_encoded_properly_when_kcode_is_utf8 with_kcode 'UTF8' do result = ActiveSupport::JSON.encode('€2.99') assert_equal '"\u20ac2.99"', result assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding) result = ActiveSupport::JSON.encode('✎☺') assert_equal '"\u270e\u263a"', result assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding) end end
Instance Protected methods
object_keys(json_object)
Link