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
BigDecimal string wrapping in JSON serialization can now be opted-out #6040
Conversation
How about something like |
Honestly, if all developers need to do is to override to_json in BigDecimal to get this behavior, I wouldn't even add this option. Because re-opening a class and overriding to_json is the official way anyway to customize JSON behavior. |
@josevalim The "official monkeypatch" doesn't even work on 3.2, see #6033 |
@jeremy I copied the |
@rafaelfranca No, because even with this patch the way BDs are serialized is not guaranteed to be quotes-free |
IMHO the way Here's my full reasoning : The change introduced in c1d7327 states that "a string is the best JSON representation for a BigDecimal". Which is wrong: the JSON specification simply says that a number is : "an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part." it does not say how, and with which precision these should be deserialized. This behaviour is also based on two other assumptions :
So you can only take advantage of the feature if :
But the price to pay is surprising behaviour when you knowinlgy use BDs for arbitrary precision calculations/storage and you try to talk to JSON APIs, and other cases (see pull request #2036). So this feature tries to pro-actively and preventively work-around hypothetical parsing bugs in third-party code at the price of breaking the principle of least suprise. I argue that working around hypothetical third-party bugs is not a reason that's good enough to break the principle of least surprise for all of us who use BDs and talk JSON to third-party APIs. One might argue that overriding the default is easy, but that's not a valid point for a bunch of reasons :
And I'm not just being anal about the monkey-patching part, the recommended official monkey-patch just stopped working as of Rails 3.2, probably because of some lazy/auto loading getting in the way. (See #6033 for a monkey patch that works). So, what IMHO should be in a future major release is as follows :
Sorry if it feels like a rant, it's not <3 Thoughts welcome! |
Ok, will do, which part of the changelog should I update ? Also, if it's relevant, how and where should I put the commented-out configuration setting (I've seen some commented settings in initializers and others in application.rb, not sure where it'd fit best). |
Let's discuss the opt-in or opt-out of the feature in another issue, since it is another discussion and we should get more feedback before. Let's leave this pull request for getting the feature and configuration options in (targeting master, aka 4.0) |
Rebased, updated |
BigDecimal string wrapping in JSON serialization can now be opted-out
Please see issue #6033
This commit allows one to easily opt-out of the specific BigDecimal JSON serialization (ie. get actual numeric serialization instead of having a string-wrapped number)
I think this should be the default behaviour in a future major release as this breaks the principle of least surprise and assumes that users always have control of the parsing end. It should be made opt-in instead when breaking backwards compatibility is acceptable.