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

from_hash expects keys to be symbols - serialized data normally string keys #93

Closed
mraaroncruz opened this issue Jul 28, 2012 · 5 comments

Comments

@mraaroncruz
Copy link

I have been having some issues with the from_hash method.
My data is serialized into json and stored in the database - then fetched out as a string and parsed to a hash.
This breaks from_hash because it is expecting symbols as keys.
There would be a simple fix to throw on a symbolize_keys but unfortunately there are nested hashes inside the serialized hash. This makes me have to write my own handler to recursively symbolize keys in the nested hashes which seems excessive.

It seems like the common use case to serialize the schedule into a string and then parse it (probably more often than not from a JSON encoded string).
Is there a good reason for the default behavior? Am I doing it wrong?
I will provide examples upon request.

Thank you!
I don't know what I would do without this project :)

@seejohnrun
Copy link
Collaborator

Heya - What JSON library are you using, 'json'?
This is definitely the common use case (and the case I developed the library for and used it for years) - that being said we can definitely switch to strings depending on JSON library

In the meantime you can fix the problem with deep_symbolize_keys!:

opt = { 'a' => { 'b' => 2 } } # or JSON.parse(data)
opt.deep_symbolize_keys!
IceCube::Schedule.from_hash opt

@mraaroncruz
Copy link
Author

I'm using yajl-ruby. I can only find a deep_symbolize_keys (without bang) as part of active_support 2.3.8 [link].
Where is that method implemented? I would totally be happy with this solution if I don't have to require another gem.

@mraaroncruz
Copy link
Author

I totally thought symbolize_keys was Ruby Standard until just now :-/

@seejohnrun
Copy link
Collaborator

Nope - and you can either use a HashWithIndifferentAccess, or use the non-bang form like:

opt = { 'a' => { 'b' => 2 } }.deep_symbolize_keys
IceCube::Schedule.from_hash opt

@mraaroncruz
Copy link
Author

It seems like these methods only exist on Rails master, not on stable (even on 3.2.7)

that being said we can definitely switch to strings depending on JSON library

could you elaborate on this

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