diff options
author | David FRANCOIS <david.francois@webflows.fr> | 2012-04-28 23:44:51 +0200 |
---|---|---|
committer | David FRANCOIS <david.francois@webflows.fr> | 2012-05-02 07:16:00 +0200 |
commit | 18aa1ae29c3459a6b2c10c7634770209a72c6cfe (patch) | |
tree | 8229d8c1ef8c9d0ac0fc6ecf8fe32ce25fd9e41a /activesupport/lib | |
parent | c0a7038412c60cf48274267f75bc4376e733dc69 (diff) | |
download | rails-18aa1ae29c3459a6b2c10c7634770209a72c6cfe.tar.gz rails-18aa1ae29c3459a6b2c10c7634770209a72c6cfe.tar.bz2 rails-18aa1ae29c3459a6b2c10c7634770209a72c6cfe.zip |
BigDecimal string wrapping in JSON serialization can now be opted-out, fixes #6033
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/json/encoding.rb | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index ef45a546b6..ab12f3f454 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -17,6 +17,7 @@ module ActiveSupport class << self delegate :use_standard_json_time_format, :use_standard_json_time_format=, :escape_html_entities_in_json, :escape_html_entities_in_json=, + :encode_big_decimal_as_string, :encode_big_decimal_as_string=, :to => :'ActiveSupport::JSON::Encoding' end @@ -104,6 +105,9 @@ module ActiveSupport # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format. attr_accessor :use_standard_json_time_format + # If false, serializes BigDecimal objects as numeric instead of wrapping them in a string + attr_accessor :encode_big_decimal_as_string + attr_accessor :escape_regex attr_reader :escape_html_entities_in_json @@ -133,6 +137,7 @@ module ActiveSupport self.use_standard_json_time_format = true self.escape_html_entities_in_json = false + self.encode_big_decimal_as_string = true end end end @@ -197,7 +202,15 @@ 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) finite? ? to_s : NilClass::AS_JSON end #:nodoc: + # + # Use ActiveSupport.use_standard_json_big_decimal_format = true to override this behaviour + def as_json(options = nil) #:nodoc: + if finite? + ActiveSupport.encode_big_decimal_as_string ? to_s : self + else + NilClass::AS_JSON + end + end end class Regexp |