Ruby Forum Ruby on Rails > Using json gem's encode instead of Rails'

Posted by Rasmus Nielsen (rasmus)
on 02.12.2009 12:08
Hello everybody,

I have two questions for you regarding the json support in Rails.

Looking in "activesupport-2.3.5/lib/active_support/json/encoding.rb" I
find this comment:

# Hack to load json gem first so we can overwrite its to_json.

Why would the author want to overwrite the json gem's to_json methods if
I've explicitly defined that I want to use the json gem as backend? The
json gem is much faster than Rails' own!
And how can I setup my environment to use the json gem for encoding?
Setting this:

ActiveSupport::JSON.backend = "JSONGem"

... doesn't affect encoding as the to_json methods are still overwritten
(as far as I can see).

I've also tried calling "JSON.fast_generate" directly on the json gem,
but as this method uses the to_json methods (which have been overwritten
by Rails) this has no effect.


Please advice,

Thank you!
Posted by Greg Hazel (ghazel)
on 27.12.2009 06:23
I'm interested in a JSONgem version of encode as well. I ran in to this 
issue:

https://rails.lighthouseapp.com/projects/8994/tickets/3345-to_json-does-not-escape-unicode-characters

which was only recently fixed. I would rather use the json gem than wait 
for Rails to find all the bugs and improve the speed of the home-grown 
json implementation.

Any help?
Posted by Brian Takita (btakita)
on 03.07.2010 00:23
Greg Hazel wrote:
> I'm interested in a JSONgem version of encode as well. I ran in to this 
> issue:
> 
> https://rails.lighthouseapp.com/projects/8994/tickets/3345-to_json-does-not-escape-unicode-characters
> 
> which was only recently fixed. I would rather use the json gem than wait 
> for Rails to find all the bugs and improve the speed of the home-grown 
> json implementation.
> 
> Any help?
I circumvented the Rails hack by making a hack of my own.

http://gist.github.com/461938

Hopefully, the Rails core team does not create more hacks of this 
nature. Better yet, the hack should be removed. I want to choose which 
version of to_json I use (instead of some Rails core dude).