#1075 accepted

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

Reported by mpd | October 3rd, 2009 @ 02:06 PM

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.

Comments and changes to this ticket

  • mpd

    mpd October 3rd, 2009 @ 02:06 PM

    • Tag set to dm-serializer, json, serialization
  • mpd

    mpd October 3rd, 2009 @ 02:15 PM

    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?

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) October 7th, 2009 @ 01:24 PM

    • State changed from “new” to “unconfirmed”
    • Assigned user set to “Jonathan Stott (namelessjon)”

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

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) October 9th, 2009 @ 08:52 AM

    • State changed from “unconfirmed” to “accepted”

    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.

  • mpd

    mpd October 9th, 2009 @ 10:28 PM

    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.

  • Dane Jensen

    Dane Jensen March 16th, 2010 @ 04:18 PM

    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.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 22nd, 2010 @ 01:08 AM

    Any progress on this issue?

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) May 28th, 2010 @ 08:19 AM

    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.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »