Project

General

Profile

Actions

Bug #12105

closed

JSON.pretty_generate throws a TypeError when there is a BigDecimal object as a value

Added by zyren (Adnan Abdulally) about 8 years ago. Updated about 8 years ago.

Status:
Third Party's Issue
Assignee:
-
Target version:
-
[ruby-core:73955]

Description

As the title states, JSON.pretty_generate throws a TypeError when there is a BigDecimal object as a value. This used to work in ruby 2.0 before upgrading in development. I havent tested to see how far back in ruby versions the error goes.

Example console log of how to replicate:

[17] pry(main)> hash = {"x" => BigDecimal('50.0')} => {"x"=>#<BigDecimal:1befe50,'0.5E2',9(18)>} [18] pry(main)> JSON.pretty_generate(hash) TypeError: wrong argument type Float (expected String) from /home/adnan/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/core_ext/object/json.rb:34:into_json'
`

Updated by zyren (Adnan Abdulally) about 8 years ago

Not sure if this is an active support bug or a ruby bug. if i try to create the hash regularly, it works fine:

[19] pry(main)> hash.to_json
=> "{"x":50.0}"

Updated by shyouhei (Shyouhei Urabe) about 8 years ago

  • Status changed from Open to Third Party's Issue

It works for me. Can you narrow the condition to reproduce yours?

zsh % rbenv exec irb
irb(main):001:0> RUBY_VERSION
=> "2.3.0"
irb(main):002:0> require 'json'
=> true
irb(main):003:0> require 'bigdecimal'
=> true
irb(main):004:0> hash = {"x" => BigDecimal('50.0')}
=> {"x"=>#<BigDecimal:7fa303986428,'0.5E2',9(18)>}
irb(main):005:0> JSON.pretty_generate(hash)
=> "{\n  \"x\": \"0.5E2\"\n}"
irb(main):006:0>
Actions

Also available in: Atom PDF

Like0
Like0Like0