JSON gem compatibility#12862
Merged
Merged
Conversation
Member
|
Good idea. This works how everyone would expect. |
Member
Author
|
👍 Good to merge |
Member
Author
There was a problem hiding this comment.
Is this important? Is there a way to preserve the documentation when using AMC?
Previously, calling `::JSON.{generate,dump}` sometimes causes
unexpected failures such as intridea/multi_json#86.
`::JSON.{generate,dump}` now bypasses the ActiveSupport JSON encoder
completely and yields the same result with or without ActiveSupport.
This means that it will **not** call `as_json` and will ignore any
options that the JSON gem does not natively understand. To invoke
ActiveSupport's JSON encoder instead, use `obj.to_json(options)` or
`ActiveSupport::JSON.encode(obj, options)`.
This was referenced Nov 24, 2013
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is extracted from #12183 as an isolated PR. I documented the current state of JSON-gem-vs-active-support incompatibility here. Clearly, you cannot currently use the JSON gem's
dumporgeneratemethod in conjunction with ActiveSupport's JSON encoder, otherwise, things may break unexpectedly on certain input at runtime with a cryptic error. This limitation is not currently very well known. It's probably safe to assume that the situation is going to get worse as we move to Ruby 1.9+ (where the JSON gem ships with the stdlib) and as multi_json is being EOL'ed and removed from Rails 4.1 (many gem authors simply replacedMultiJson.dumpwith::JSON.{dump,generate}.To improve the situation, we can do one of the following things:
::JSON.{dump,generate}to raise an exception when ActiveSupport::JSON is loaded: while this might be ideal from a purist perspective, I'm afraid this is a non-starter – even some of our dependencies are currently using::JSON.{dump,generate}in their codebase.With this PR, calling
::JSON.{generate,dump}will invoke the JSON gem's encoder. It will not invoke any of theActiveSupport::JSONlogic, i.e. it will bypassas_jsonand ignore any options that it does not natively understand (e.g.only,except, etc). Instead of using the ad-hoc approach in #9295, this PR changes our version ofto_jsonto add some triage code. The end result is that invoking::JSON.{generate,dump}will yield the same result before and afterActiveSupport::JSONis loaded.If I get a 👍 from core, I'll can clean things up, add the changelog, etc.
cc @jeremy