-
-
Notifications
You must be signed in to change notification settings - Fork 234
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
Unexpected JSON serialisation behaviour #287
Comments
It's a little weird to add support for I think just delegating calls from |
It looks like they try to skip that for OpenStructs but the check is too specific. Is there anything we can do to work around that? https://github.com/rails/jbuilder/blob/master/lib/jbuilder.rb#L313 |
I'm not sure I am seeing where the issue is coming into play, but does this change help us at all? rails/jbuilder@e2e8623#diff-8be890cee05d282eadfd5ce2da69682cae8b9b87bb5c1e8d04004c39d7375a62 |
@rdubya I tested On commit |
The change in behavior introduced with jbuilder 2.9.1 (rails/jbuilder@e2e8623) is interesting because it seems like the intent was to start respecting |
So in OP's example, hash = { config: Config::Options.new(foo: :bar) }
hash.to_json which produces the string Before 2.9.1, hash = { config: Config::Options.new(foo: :bar) }
JSON.dump(hash) which produces This is all assuming you're using The Calling One interesting side-effect of all of this is that if you're just using the JSON.load Jbuilder.new { |json| json.config Config::Options.new(foo: :bar) }.target! produces the desired result, even in Jbuilder 2.9.1 {"config"=>{"foo"=>"bar"}} |
Given all of that, I think OP's original suggestion of making an explicit implementation of |
Opened #292 which adds Longer term, I think a more robust solution would be to remove But removing |
I ran into a weird issue when upgrading a Rails service recently.
Long story short, jbuilder has changed the way a template is transformed to JSON over the last couple of versions. See https://github.com/rails/jbuilder/blob/master/lib/jbuilder.rb#L250-L252 and the history on that line for details.
That was kind of unexpected. The reason for that is, that
Config::Options
includesEnumerable
andEnumerable#as_json
callsto_a
before serialising into JSON.My current workaround would be to call
to_h
/to_hash
on theConfig::Options
that's rendered or maybe even monkey-patchingConfig::Options#as_json
.I think it would be a good idea to implement
Config::Options#as_json
, maybe even removingConfig::Options#to_json
? Though that's a breaking change and would require a major version release? What do you all think?The text was updated successfully, but these errors were encountered: