diff options
author | Florian Frank <flori@ping.de> | 2016-02-25 00:46:45 +0100 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2016-02-25 00:46:45 +0100 |
commit | 4922d2ae6cf93d14f7d51d416f518b5945935b3a (patch) | |
tree | a112556a783db0b231d0328ca9538c8cf7884dca | |
parent | 4dec168abb84a9175549e8d03a95a57df6a384e7 (diff) | |
download | json-4922d2ae6cf93d14f7d51d416f518b5945935b3a.tar.gz |
Check for existence of #to_json method before call
-rw-r--r-- | lib/json/pure/generator.rb | 12 | ||||
-rwxr-xr-x | tests/test_json_generate.rb | 2 |
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb index 9056a5d..1419074 100644 --- a/lib/json/pure/generator.rb +++ b/lib/json/pure/generator.rb @@ -363,7 +363,11 @@ module JSON result << state.space_before result << ':' result << state.space - result << value.to_json(state) + if value.respond_to?(:to_json) + result << value.to_json(state) + else + result << %{"#{String(value)}"} + end first = false } depth = state.depth -= 1 @@ -398,7 +402,11 @@ module JSON each { |value| result << delim unless first result << state.indent * depth if indent - result << value.to_json(state) + if value.respond_to?(:to_json) + result << value.to_json(state) + else + result << %{"#{String(value)}"} + end first = false } depth = state.depth -= 1 diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb index cc8e7cd..95c9266 100755 --- a/tests/test_json_generate.rb +++ b/tests/test_json_generate.rb @@ -342,7 +342,7 @@ EOT undef to_json end assert_nothing_raised(SystemStackError) do - assert_equal '[""]', JSON.generate([s.new]) + assert_equal '["foo"]', JSON.generate([s.new('foo')]) end end end |