Skip to content

Commit

Permalink
Removed support for encoding BigDecimal as a JSON number
Browse files Browse the repository at this point in the history
This is because the new encoder will no longer support encode_json.
Therefore our only choice is to return `to_i` or `to_s` in
`BigDecimal#as_json`. Since casting a BigDecimal to an integer is
most likely a lossy operation, we chose to encode it as a string.

Support for encoding BigDecimal as a string will return via the
`activesupport-json_encoder` gem.
  • Loading branch information
chancancode committed Nov 26, 2013
1 parent ca12db0 commit 4d02296
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 25 deletions.
9 changes: 1 addition & 8 deletions activesupport/lib/active_support/core_ext/object/json.rb
Expand Up @@ -132,15 +132,8 @@ class BigDecimal
# 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.
#
# Use <tt>ActiveSupport.encode_big_decimal_as_string = true</tt> to
# override this behavior.
def as_json(options = nil) #:nodoc:
if finite?
ActiveSupport.encode_big_decimal_as_string ? to_s : self
else
nil
end
finite? ? to_s : nil
end
end

Expand Down
6 changes: 0 additions & 6 deletions activesupport/lib/active_support/json/encoding.rb
Expand Up @@ -7,7 +7,6 @@ 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

Expand Down Expand Up @@ -69,10 +68,6 @@ class << self
# 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

Expand Down Expand Up @@ -118,7 +113,6 @@ def const_missing(name)

self.use_standard_json_time_format = true
self.escape_html_entities_in_json = true
self.encode_big_decimal_as_string = true
end
end
end
11 changes: 0 additions & 11 deletions activesupport/test/json/encoding_test.rb
Expand Up @@ -399,17 +399,6 @@ def test_struct_encoding
ActiveSupport::JSON.decode(json_string_and_date))
end

def test_opt_out_big_decimal_string_serialization
big_decimal = BigDecimal('2.5')

begin
ActiveSupport.encode_big_decimal_as_string = false
assert_equal big_decimal.to_s, big_decimal.to_json
ensure
ActiveSupport.encode_big_decimal_as_string = true
end
end

def test_nil_true_and_false_represented_as_themselves
assert_equal nil, nil.as_json
assert_equal true, true.as_json
Expand Down

0 comments on commit 4d02296

Please sign in to comment.