-
-
Notifications
You must be signed in to change notification settings - Fork 254
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
Update usage of :bigdecimal_as_decimal in Rails Mode #716
Conversation
The option `:bigdecimal_as_decimal` is not available in Rails Mode (as discussed here ohler55#693), as the original JSON and Rails implementation does not have a way to "force" bigdecimal values to be encoded as numbers in json.
I have to verify if the change is indeed accurate before merging. I seem to remember having that option available as non-standard for rails and hence the footnote. It might take a day or two to get to it though. |
Sure, no problem. TBH I hope that I'm wrong on this one and this option is re-added to OJ >.< |
It took a while but I did finally get a break from moving to look at this. There is a way to set the |
Thanks for looking at it and I hope that everything went smoothly when moving. require 'bundler/inline'
OJ_VERSION = '3.13.9'
puts "With OJ version (#{OJ_VERSION})"
gemfile do
source 'https://rubygems.org'
gem 'rails'
gem 'oj', OJ_VERSION
end
puts "Before optimizing"
puts "Load"
p JSON.load("{\"ticker\":\"ASAI3\",\"price\":\"18.7\",\"rate\":-0.8}")
p Oj.load("{\"ticker\":\"ASAI3\",\"price\":\"18.7\",\"rate\":-0.8}")
puts "Generate"
p JSON.generate({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d})
p Oj.generate({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d})
Oj.optimize_rails
Oj.default_options = {
mode: :rails,
bigdecimal_as_decimal: true,
bigdecimal_load: true
}
puts "After optimizing"
puts "Load"
p JSON.load("{\"ticker\":\"ASAI3\",\"price\":\"18.7\",\"rate\":-0.8}")
p Oj.load("{\"ticker\":\"ASAI3\",\"price\":\"18.7\",\"rate\":-0.8}")
puts "Generate"
p JSON.generate({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d})
p Oj.generate({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d}) JSON is still generated as strings:
|
Upon further investigation, there seems to be a "problem" with p JSON.generate({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d})
p Oj.generate({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d})
p Oj.dump({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d})
p({:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d}.to_json) Result:
So |
I'll try and look tomorrow. |
Similar results but added the rails require 'rails'
require 'oj'
$data = {:ticker=>"ASAI3", :price=>18.7, :rate=>-0.8.to_d}
def encode
p "JSON.generate: #{JSON.generate($data)}"
p "Oj.generate: #{Oj.generate($data)}"
p "Oj.dump: #{Oj.dump($data)}"
p "to_json: #{$data.to_json}"
p "ActiveSupport::JSON.encode: #{ActiveSupport::JSON.encode($data)}"
end
puts "With Oj version (#{Oj::VERSION})"
puts
puts "Before optimizing"
encode
Oj.optimize_rails
Oj.default_options = {
mode: :rails,
bigdecimal_as_decimal: true,
bigdecimal_load: true
}
puts
puts "After optimizing"
encode results in:
|
So, But TBH it seems like a bug to me, as those methods should generate the same JSON output. Correct me if I'm wrong, but aren't the options there to change the gem behaviour? If I do set that option, I do it knowing that it might not generate strictly similar JSON results to vanilla JSON or Rails, but I'm fine with it. If someone required strict results wouldn't it be just a matter of not using an option that's there to change it? |
It is the desire behaviour that The Tje options are indeed there to change the gem behaviour and in Finally, the JSON gem behaviour is not consistent between |
Ok, I understand now, that's a lot of crazy stuff going with all those JSON implementations and whatnot. |
@ohler55 PR updated |
The option
:bigdecimal_as_decimal
is not available in Rails Mode (as discussed here #693), as the original JSON and Rails implementation does not have a way to "force" bigdecimal values to be encoded as numbers in json.