Skip to content

Instantly share code, notes, and snippets.

@sparksp
Last active August 29, 2015 14:08
Show Gist options
  • Save sparksp/af09d0e0ad04bfdc3fbc to your computer and use it in GitHub Desktop.
Save sparksp/af09d0e0ad04bfdc3fbc to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem "oj"
gem "activesupport", "~> 4.0.0"
gem "minitest"
require "minitest/spec"
require "minitest/autorun"
require "oj"
# Uncomment this line and test_big_decimal will fail
# require "active_support/json"
# With ActiveSupport 4.0, neither of these settings affect BigDecimal#to_json,
# only BigDecimal#as_json
#
# ActiveSupport.encode_big_decimal_as_string = false
# ActiveSupport::JSON::Encoding.encode_big_decimal_as_string = false
describe Oj do
# Options set by default in Rails 4.0 / Rabl
def options
{
:bigdecimal_as_decimal=>true, # default = true
:use_to_json=>true, # default = false
:mode=>:compat, # default = object
:time_format=>:ruby, # default = unix
}
end
def test_big_decimal
orig = BigDecimal.new("3.14159265359")
json = Oj.dump(orig, options)
value = Oj.load(json)
assert_equal(value, orig)
# by default, without active support
# assert_equal("0.314159265359E1", json)
# in Rails 4.1, with active support
# assert_equal("3.14159265359", json)
end
# Floats are unaffected
def test_float
orig = 3.14159265359
json = Oj.dump(orig, options)
assert_equal("3.14159265359", json)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment