@@ -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
0 commit comments