Skip to content
This repository
Browse code

Merge pull request #6040 from Paymium/issue-6033

BigDecimal string wrapping in JSON serialization can now be opted-out
  • Loading branch information...
commit 07438f9ebb82b2d002563af8952db6fa812bbb94 2 parents 3b6a353 + 18aa1ae
José Valim authored May 02, 2012
3  activesupport/CHANGELOG.md
Source Rendered
@@ -32,6 +32,9 @@
32 32
 
33 33
 *    Unicode database updated to 6.1.0.
34 34
 
  35
+*    Adds `encode_big_decimal_as_string` option to force JSON serialization of BigDecimals as numeric instead
  36
+     of wrapping them in strings for safety.
  37
+
35 38
 
36 39
 ## Rails 3.2.2 (March 1, 2012) ##
37 40
 
15  activesupport/lib/active_support/json/encoding.rb
@@ -17,6 +17,7 @@ module ActiveSupport
17 17
   class << self
18 18
     delegate :use_standard_json_time_format, :use_standard_json_time_format=,
19 19
       :escape_html_entities_in_json, :escape_html_entities_in_json=,
  20
+      :encode_big_decimal_as_string, :encode_big_decimal_as_string=,
20 21
       :to => :'ActiveSupport::JSON::Encoding'
21 22
   end
22 23
 
@@ -104,6 +105,9 @@ class << self
104 105
         # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
105 106
         attr_accessor :use_standard_json_time_format
106 107
 
  108
+        # If false, serializes BigDecimal objects as numeric instead of wrapping them in a string
  109
+        attr_accessor :encode_big_decimal_as_string
  110
+
107 111
         attr_accessor :escape_regex
108 112
         attr_reader :escape_html_entities_in_json
109 113
 
@@ -133,6 +137,7 @@ def escape(string)
133 137
 
134 138
       self.use_standard_json_time_format = true
135 139
       self.escape_html_entities_in_json  = false
  140
+      self.encode_big_decimal_as_string  = true
136 141
     end
137 142
   end
138 143
 end
@@ -197,7 +202,15 @@ class BigDecimal
197 202
   # That's why a JSON string is returned. The JSON literal is not numeric, but if
198 203
   # the other end knows by contract that the data is supposed to be a BigDecimal,
199 204
   # it still has the chance to post-process the string and get the real value.
200  
-  def as_json(options = nil) finite? ? to_s : NilClass::AS_JSON end #:nodoc:
  205
+  #
  206
+  # Use ActiveSupport.use_standard_json_big_decimal_format = true to override this behaviour
  207
+  def as_json(options = nil) #:nodoc:
  208
+    if finite?
  209
+      ActiveSupport.encode_big_decimal_as_string ? to_s : self
  210
+    else
  211
+      NilClass::AS_JSON
  212
+    end
  213
+  end
201 214
 end
202 215
 
203 216
 class Regexp
11  activesupport/test/json/encoding_test.rb
@@ -274,6 +274,17 @@ def test_struct_encoding
274 274
                  JSON.parse(json_string_and_date))
275 275
   end
276 276
 
  277
+  def test_opt_out_big_decimal_string_serialization
  278
+    big_decimal = BigDecimal('2.5')
  279
+
  280
+    begin
  281
+      ActiveSupport.encode_big_decimal_as_string = false
  282
+      assert_equal big_decimal.to_s, big_decimal.to_json
  283
+    ensure
  284
+      ActiveSupport.encode_big_decimal_as_string = true
  285
+    end
  286
+  end
  287
+
277 288
   protected
278 289
 
279 290
     def object_keys(json_object)

0 notes on commit 07438f9

Please sign in to comment.
Something went wrong with that request. Please try again.