-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Rename to_json as serialized to support other output types #603
Conversation
@@ -13,6 +13,13 @@ def setup | |||
|
|||
def test_null_adapter | |||
assert_equal('{"name":"Name 1","description":"Description 1"}', | |||
@adapter.serialized) | |||
|
|||
JSON |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, I don't even... likely a copy/paste artifact from the one below (line 18/25), from what I can see neither one is necessary.
I thought I commented, but it appears to be gone for some reason :) Anyway - we generally should be implementing #as_json to return the appropriate Ruby object representation (a hash, array, string etc) rather than implementing #to_json |
And that applies to the rest of the project as well. Basically we shouldn't have any to_json in here |
477b9d5
to
256e6cf
Compare
@chancancode replaced to_json with as_json; A bit of context, @steveklabnik and I were talking earlier about how I'd go about supporting XML, thus the issue with defaulting the interface to a name with json... I largely just did the alias to maintain backwards compatibility, not sure if it's even necessary given the name change. |
I believe we used to have a more generic method called "serialized_hash", would that work for your purpose? |
@chancancode that'd be on the input side, the question is, what gets called to produce the output. The output here isn't a hash, it's the JSON/XML. |
If you drop the to_json call in here as I suggested in the line note, your "serialized" method is basically what "serialized_hash used to be. Then as_json can either be defined as |
@steveklanbnik of you defined as_json to return the Ruby hash, Rails json encoder will pick that up and do the right thing. The Object#to_json defined in Rails basically walks the object tree, convert everything along its way by calling as_json, then encode that resulting hash/array/whatever as JSON string. So basically, if you defined as_json, to_json would Just Work TM |
Historically, this has been really bad, but I think newer Rails it's okay? I need to review. |
I'm happy to help however I can, just trying to move things forward as I try and get to the point where I might attempt an XML implementation... |
I think this name is fine. This needs a rebase though, and the last build failed, though maybe it's passing now. |
22751ba
to
f673f24
Compare
@steveklabnik sorry I completely forgot about my comment earlier 😄 As far as I know, all currently supported versions of Rails has reasonable class Object
def to_json(options = {})
::JSON.generate(self.as_json(options), quirks_mode: true)
end
def as_json(options = {})
# convert this object to a hash...
end
end So, This example illustrates it well.. >> require 'active_support/json'
=> true
>> class ToJsonObject
>> def to_json(options = {})
>> '"zomg"' # remember, this should return valid JSON values, so the returned string should be quoted
>> end
>> end
=> nil
>> ToJsonObject.new.to_json
=> "\"zomg\""
>> [ToJsonObject.new].to_json
=> "[{}]"
>> class AsJsonObject
>> def as_json(options = {})
>> 'zomg'
>> end
>> end
=> nil
>> AsJsonObject.new.to_json
=> "\"zomg\""
>> [AsJsonObject.new].to_json
=> "[\"zomg\"]" So, unless there are very special needs here we should go with Hope it helps! Sorry that I wasn't able to put a lot of time in this project, thanks for picking it up and shepherding it! ❤️ 💚 💙 💛 💜 |
I'm planning to do an adapter for Cap'n Proto so this makes sense for me 👍 |
@chancancode the idea is that not everything is JSON, so having JSON in the name isn't appropriate. |
Yeah, the line in this pull request was fixed, so doesn't directly apply anymore. The original line that caught my attention was an addition along the lines of active_model_serializers/lib/active_model/serializer/adapter.rb Lines 19 to 21 in 9360a10
I don't have any objections about this pull request anymore, was just elaborating on my first comment and answering the question you raised 😄 |
👍 ❤️ |
looks like the files this was based on have been moved (and changed), hard for me to tell if any changes are needed, or if this can be closed. |
@GeekOnCoffee can you check back on this PR? I believe this can be closed now as this has already been implemented on |
@steveklabnik based on comment discussion, happy for suggestions for a better name or other feedback