Skip to content
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

as_json keys and key order different to to_json #72

Open
ConorSheehan1 opened this issue Oct 1, 2018 · 0 comments
Open

as_json keys and key order different to to_json #72

ConorSheehan1 opened this issue Oct 1, 2018 · 0 comments

Comments

@ConorSheehan1
Copy link
Contributor

I've noticed that as_json nests all the keys that are included in to_json under data, and the keys are in a different order. For example:

(byebug) manifest.to_json
"{\"@context\":\"http://iiif.io/api/presentation/2/context.json\",\"@id\":\"http://localhost:3000/iiif/collection/gq67jr29s.json\",\"@type\":\"sc:Collection\",\"label\":\"t\",\"description\":\"t\"}"
(byebug) manifest.as_json
{"data"=>{"@id"=>"http://localhost:3000/iiif/collection/gq67jr29s.json", "label"=>"t", "description"=>"t", "@type"=>"sc:Collection", "@context"=>"http://iiif.io/api/presentation/2/context.json"}}
(byebug) 

Usually as_json returns a hash respresentation which is then used in to_json, for example:

(byebug) User.first.as_json.to_json == User.first.to_json
true
(byebug) manifest.as_json.to_json == manifest.to_json
false

Since the keys are in a different order under data, re-parsing is the only way to get an equal result

(byebug) manifest.as_json["data"].to_json == manifest.to_json
false
(byebug) JSON.parse(manifest.as_json["data"].to_json) == JSON.parse(manifest.to_json)
true

Would it make sense to patch this so that as_json returns a hash representation that would in turn be used for to_json?

I realize that I could use something like this to monkey patch it

def as_json
  JSON.parse(self.to_json)
end

but it wouldn't be very efficient, and I thought it might make sense to change it here instead of opening up the class when I'm usign it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant