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

CircularReferenceError with rails 'to_json' method #6

Closed
ghostd opened this issue Oct 25, 2011 · 6 comments
Closed

CircularReferenceError with rails 'to_json' method #6

ghostd opened this issue Oct 25, 2011 · 6 comments

Comments

@ghostd
Copy link

ghostd commented Oct 25, 2011

Hi,

I added an enum to my (working) class, and now i get CircularReferenceError when i try to convert my instance into JSON.

By default class is serialized with the enum, which is serialized with the owner... which is serialized with the enum :-)

I think the generator (or the base class) should override as_json to except the owner attribute.

I did this into by base enum class:
def as_json(options = {})
{:sym => to_s, :name => name}
end

@beerlington
Copy link
Owner

I looked into this a little but was getting a stack level too deep error, and not a CircularReferenceError. Perhaps it's the same thing? Either way, it should be an easy fix by just filtering out the owner from the json response as you suggested. I should have a patched version of the gem ready in the next day or so.

@beerlington
Copy link
Owner

Which version of the gem are you currently using?

@ghostd
Copy link
Author

ghostd commented Oct 27, 2011

Hi,

I got this issue with rails 3.1.0 and 3.1.1. I get a "stack level too deep error" when i try with web pages, and when i investigated in a rails console, i got a CircularReferenceError. I went more in depth about this error, here is what i found to explain the problem:

  • to encode the enum to_json calls the JSON encoder
    (see lib/active_support/core_ext/object/to_json.rb)
  • which calls as_json
    (see lib/active_support/json/encoding.rb)
  • which calls "instance_values" for the enum
    (see Object#as_json overriding in lib/active_support/json/encoding.rb)
  • which returns all properties including the owner (the loop is now created)

I think you should proivde a "as_json" implementation for the Enum base class.

PS: i think you should not package your .rvmrc in the gem... it makes warning when i go through the gems directories with mine

@beerlington
Copy link
Owner

Thanks for the info. I pushed a fix to a new branch and I was hoping you could try it out and see if it addresses your specific issue. https://github.com/beerlington/classy_enum/tree/_fix-json-recursion

Diff: master..._fix-json-recursion

@ghostd
Copy link
Author

ghostd commented Oct 27, 2011

It works here!

I'm going to continue to override 'as_json' because i need both values: symbol and name (since name is a method, it's not included by the introspection)

@beerlington
Copy link
Owner

Just pushed v 1.3.1 which fixes this issue. Thanks for reporting 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

2 participants