Project

General

Profile

Actions

Bug #7306

closed

BigDecimal does not properly marshal to JSON and back

Added by vbatts (Vincent Batts) almost 12 years ago. Updated almost 12 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 2.0.0dev (2012-11-06 trunk 37483) [x86_64-linux]
Backport:
[ruby-core:49048]

Description

$ ruby -v -S irb -r bigdecimal -r json
ruby 2.0.0dev (2012-11-06 trunk 37483) [x86_64-linux]
/opt/ruby/2.0.0-git4e73323a6/lib/ruby/2.0.0/irb/ext/save-history.rb:29: warning: method redefined; discarding old save_history=
/opt/ruby/2.0.0-git4e73323a6/lib/ruby/2.0.0/irb/extend-command.rb:211: warning: previous definition of save_history= was here
irb(main):001:0> inf = BigDecimal.new('Infinity')
=> #BigDecimal:2880908,'Infinity',9(9)
irb(main):002:0> inf2 = JSON.load(inf.to_json)
=> "Infinity"
irb(main):003:0> inf3 = JSON.parse(inf.to_json)
JSON::ParserError: 757: unexpected token at '"Infinity"'
from /opt/ruby/2.0.0-git4e73323a6/lib/ruby/2.0.0/json/common.rb:149:in parse' from /opt/ruby/2.0.0-git4e73323a6/lib/ruby/2.0.0/json/common.rb:149:in parse'
from (irb):3
from /opt/ruby/2.0.0-git4e73323a6/bin/irb:12:in `'
irb(main):004:0> JSON.dump inf
=> ""Infinity""
irb(main):005:0> JSON.load(JSON.dump(inf))
=> "Infinity"

Updated by mame (Yusuke Endoh) almost 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to naruse (Yui NARUSE)

I'm not sure what you expect.

Are you expecting that any object supports dumping and loading JSON notation? I don't think that it is virtually impossible.
Or, are you saying that JSON should not define Object#to_json? It makes sense to me, but impossible because of compatibility.

Naruse-san, what do you think?

--
Yusuke Endoh

Updated by naruse (Yui NARUSE) almost 12 years ago

  • Status changed from Assigned to Rejected

JSON can't express infinity.
Confirm inf.to_json is "Infinity", it should be unexpected result.

Updated by vbatts (Vincent Batts) almost 12 years ago

Shouldn't it reinitialize a Bigdecimal object, with value "Infinity"?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0