aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-05-01 12:34:45 -0700
committerPiotr Sarnacki <drogus@gmail.com>2012-05-01 12:34:45 -0700
commitec94d8442567b6658e82ad6c4e2428abb23720c9 (patch)
tree0c62ffdda6411110c1aedb56391007dfb1e6bf9e /activesupport/lib/active_support
parent7d2df5fa62da393e429393ccd161d237117932fd (diff)
parentd53877880835db241e1afeeb9bd6a8037c32e2fd (diff)
downloadrails-ec94d8442567b6658e82ad6c4e2428abb23720c9.tar.gz
rails-ec94d8442567b6658e82ad6c4e2428abb23720c9.tar.bz2
rails-ec94d8442567b6658e82ad6c4e2428abb23720c9.zip
Merge pull request #6096 from hasclass/as_json__encode_infinite_and_nan_bigdecimals_as_null
JSON: encode BigDecimal NaN/Infinity as null.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/json/encoding.rb4
1 files changed, 3 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