aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebi Burkhard <sebi.burkhard@gmail.com>2012-05-01 11:28:46 +0700
committerSebi Burkhard <sebi.burkhard@gmail.com>2012-05-01 11:28:46 +0700
commitd53877880835db241e1afeeb9bd6a8037c32e2fd (patch)
tree839f48eda4a54216b5591309199cedb300c2b8db
parent3cc6995e714bc763b40eb3e1d710c1e5ded44384 (diff)
downloadrails-d53877880835db241e1afeeb9bd6a8037c32e2fd.tar.gz
rails-d53877880835db241e1afeeb9bd6a8037c32e2fd.tar.bz2
rails-d53877880835db241e1afeeb9bd6a8037c32e2fd.zip
JSON: encode BigDecimal NaN/Infinity as null.
-rw-r--r--activesupport/lib/active_support/json/encoding.rb4
-rw-r--r--activesupport/test/json/encoding_test.rb1
2 files changed, 4 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index e0dee0e072..ef45a546b6 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -183,6 +183,8 @@ class Numeric
end
class Float
+ # Encoding Infinity or NaN to JSON should return "null". The default returns
+ # "Infinity" or "NaN" what breaks parsing the JSON. E.g. JSON.parse('[NaN]').
def as_json(options = nil) finite? ? self : NilClass::AS_JSON end #:nodoc:
end
@@ -195,7 +197,7 @@ class BigDecimal
# That's why a JSON string is returned. The JSON literal is not numeric, but if
# the other end knows by contract that the data is supposed to be a BigDecimal,
# it still has the chance to post-process the string and get the real value.
- def as_json(options = nil) to_s end #:nodoc:
+ def as_json(options = nil) finite? ? to_s : NilClass::AS_JSON end #:nodoc:
end
class Regexp
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 8493f4dc2c..babacf4d3a 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -30,6 +30,7 @@ class TestJSONEncoding < ActiveSupport::TestCase
[ 0.0/0.0, %(null) ],
[ 1.0/0.0, %(null) ],
[ -1.0/0.0, %(null) ],
+ [ BigDecimal('0.0')/BigDecimal('0.0'), %(null) ],
[ BigDecimal('2.5'), %("#{BigDecimal('2.5').to_s}") ]]
StringTests = [[ 'this is the <string>', %("this is the \\u003Cstring\\u003E")],