diff options
author | José Valim <jose.valim@gmail.com> | 2012-05-02 03:16:04 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2012-05-02 03:16:04 -0700 |
commit | 07438f9ebb82b2d002563af8952db6fa812bbb94 (patch) | |
tree | 2ba2653894b92a90412157ae02eee27726be97a5 /activesupport/lib/active_support/json/encoding.rb | |
parent | 3b6a3531745f38bcb9b78aa2f82f3aa9e3530f8d (diff) | |
parent | 18aa1ae29c3459a6b2c10c7634770209a72c6cfe (diff) | |
download | rails-07438f9ebb82b2d002563af8952db6fa812bbb94.tar.gz rails-07438f9ebb82b2d002563af8952db6fa812bbb94.tar.bz2 rails-07438f9ebb82b2d002563af8952db6fa812bbb94.zip |
Merge pull request #6040 from Paymium/issue-6033
BigDecimal string wrapping in JSON serialization can now be opted-out
Diffstat (limited to 'activesupport/lib/active_support/json/encoding.rb')
-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 |