Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Overriding to_json in a class causes associated objects to over-quote and over-escape #16

Open
solnic opened this issue May 17, 2011 · 9 comments

Comments

@solnic
Copy link
Contributor

solnic commented May 17, 2011

Any suggestions for a clearer title for this ticket would be very much appreciated ;)

If you override to_json in a class Foo, and class Bar has n foos, calling @bar.foos.to_json in an instance of Bar will emit bad JSON which has too many quotes and backslash escapes.

I created a toy project that demonstrates this at http://github.com/xxx/dm10-association_to_json .

Maybe there's just a different way to do this now.


Created by mpd - 2009-10-03 19:06:22 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1075

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

I did notice

def to_json(*args)
res = super
p res.class # Hash?
res
end

super is returning a hash, but that isn’t valid JSON.

What is the correct way to do this now?

by mpd

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

@namelessjon: assigning this to you because (I think?) you recently did some work on to_json in dm-serializer.

by Dan Kubb (dkubb)

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

Okay, I know why this is happening. It’s due to the changes I made to speed things up, which has the side effect of slightly breaking the assumptions the JSON library is making. I will try and work out how to safely allow overriding while keeping the speedup for users who arn’t doing this.

As a temporary measure, what are you trying to accomplish with the custom method? It may be possible to duplicate that through options to the to_json call.

by Jonathan Stott (namelessjon)

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

I’m aggregating a bunch of method return values from an associated object (and those methods are not known until runtime, so the associated object is queried both for the methods to include and the results.)

I could get around it by delegating methods or using method_missing. I’m not terribly keen on doing either for this sort of operation, though.

by mpd

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

You can work around it by calling to_a on the collection before to_json. So in the above example, @bar.foos.to_a.to_json should work fine.

by Dane Jensen

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

Any progress on this issue?

by Dan Kubb (dkubb)

@solnic
Copy link
Contributor Author

solnic commented May 17, 2011

As I see it, there are basically two options:

  1. Revert the original patch and go back to manually constructing JSON.
  2. Do as AMo do, and have a @as_hash@ (or method with that intent, at least) and a @to_json@. It would then be possible to override @as_hash@ however you liked, and @to_json@ would just call @as_hash.to_json@.

Also, the @to_json@ method in the example could check to see if @Args@ is a hash, and if it has the @:to_json@ option set to false, and if so, just return a hash.

by Jonathan Stott (namelessjon)

@gilbert
Copy link

gilbert commented May 9, 2012

Any progress on this issue?

@solnic
Copy link
Contributor Author

solnic commented May 9, 2012

No. Not really. Happy to accept pull requests though :)

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

No branches or pull requests

2 participants