Sha256: f482a6f7a178c81c9ae608ccc27cca3c6d0c7a62c40c3804535512954923fdcc

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 KB

Contents

#!/usr/bin/env ruby
# encoding: UTF-8

$VERBOSE = true

%w(lib ext test).each do |dir|
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
end

require 'rubygems' if RUBY_VERSION.start_with?('1.8.')

#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")
    puts "*** to_s: #{orig.to_s}"
    puts "*** to_json: #{orig.to_json}"
    puts "*** JSON.dump: #{JSON.dump(orig)}"
    json = Oj.dump(orig, options)
    puts "*** json: #{json}"
 
    value = Oj.load(json)
    puts "*** value: #{value.class}"
    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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
oj-2.17.4 test/test_bigd.rb
oj-2.16.0 test/test_bigd.rb
oj-2.15.1 test/test_bigd.rb
oj-2.15.0 test/test_bigd.rb
oj-2.12.11 test/test_bigd.rb
oj-2.12.9 test/test_bigd.rb
oj-2.12.6 test/test_bigd.rb
oj-2.12.1 test/test_bigd.rb
oj-2.11.5 test/test_bigd.rb
oj-2.11.4 test/test_bigd.rb