Skip to content

Commit 07438f9

Browse files
committedMay 2, 2012
Merge pull request rails#6040 from Paymium/issue-6033
BigDecimal string wrapping in JSON serialization can now be opted-out
2 parents 3b6a353 + 18aa1ae commit 07438f9

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
 

‎activesupport/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232

3333
* Unicode database updated to 6.1.0.
3434

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+
3538

3639
## Rails 3.2.2 (March 1, 2012) ##
3740

‎activesupport/lib/active_support/json/encoding.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module ActiveSupport
1717
class << self
1818
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
1919
:escape_html_entities_in_json, :escape_html_entities_in_json=,
20+
:encode_big_decimal_as_string, :encode_big_decimal_as_string=,
2021
:to => :'ActiveSupport::JSON::Encoding'
2122
end
2223

@@ -104,6 +105,9 @@ class << self
104105
# If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format.
105106
attr_accessor :use_standard_json_time_format
106107

108+
# If false, serializes BigDecimal objects as numeric instead of wrapping them in a string
109+
attr_accessor :encode_big_decimal_as_string
110+
107111
attr_accessor :escape_regex
108112
attr_reader :escape_html_entities_in_json
109113

@@ -133,6 +137,7 @@ def escape(string)
133137

134138
self.use_standard_json_time_format = true
135139
self.escape_html_entities_in_json = false
140+
self.encode_big_decimal_as_string = true
136141
end
137142
end
138143
end
@@ -197,7 +202,15 @@ class BigDecimal
197202
# That's why a JSON string is returned. The JSON literal is not numeric, but if
198203
# the other end knows by contract that the data is supposed to be a BigDecimal,
199204
# 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
201214
end
202215

203216
class Regexp

‎activesupport/test/json/encoding_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,17 @@ def test_struct_encoding
274274
JSON.parse(json_string_and_date))
275275
end
276276

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+
277288
protected
278289

279290
def object_keys(json_object)

0 commit comments

Comments
 (0)
Please sign in to comment.