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?
I've noticed that
as_jsonnests all the keys that are included into_jsonunderdata, and the keys are in a different order. For example:Usually
as_jsonreturns a hash respresentation which is then used into_json, for example:Since the keys are in a different order under
data, re-parsing is the only way to get an equal resultWould it make sense to patch this so that
as_jsonreturns a hash representation that would in turn be used forto_json?I realize that I could use something like this to monkey patch it
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?