Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumping and parsing BigDecimal in scientific notation #462

Open
troex opened this issue Dec 24, 2020 · 0 comments
Open

Dumping and parsing BigDecimal in scientific notation #462

troex opened this issue Dec 24, 2020 · 0 comments

Comments

@troex
Copy link

troex commented Dec 24, 2020

It looks like JSON has no problem parsing scientific notation however it can't produce it, instead string is produced. In my case I work with BigDecimals mostly and I need to pass encoded JSON data from one node to another to continue computation on the other node. Sorry if I'm not aware if solution exists, but could been able to find one. Maybe related to #219

decimal = (BigDecimal('1') / BigDecimal('3')) # 0.333333333333333333e0
hash = { 'bc' => decimal } # {"bc"=>0.333333333333333333e0}
JSON(JSON(hash)) == hash # false
JSON(JSON(hash)) # {"bc"=>"0.333333333333333333e0"}
JSON(JSON(decimal)) == decimal # => false
JSON(JSON(decimal)) # => "0.333333333333333333e0"

UPD. I figured out that I can control to some degree how scientific notation is parsed, but didn't found a way to control how to dump it.

puts JSON.dump(hash) # {"bc":"0.333333333333333333e0"}
JSON.parse('{"bc":"0.333333333333333333e0"}', decimal_class: BigDecimal) # {"bc"=>"0.333333333333333333e0"}
JSON.parse('{"bc":0.333333333333333333e0}', decimal_class: BigDecimal) # {"bc"=>0.333333333333333333e0}
JSON.parse('{"bc":0.333333333333333333e0}') # {"bc"=>0.3333333333333333}
JSON.parse('{"bc":"0.333333333333333333e0"}') # {"bc"=>"0.333333333333333333e0"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant