# Using json gem's encode instead of Rails'

**URL:** <https://www.ruby-forum.com/t/using-json-gems-encode-instead-of-rails/179706>\
**Category:** Rails\
**Created:** [December 2, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/using-json-gems-encode-instead-of-rails/179706 "2009-12-02T12:08:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [December 2, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/using-json-gems-encode-instead-of-rails/179706/1 "2009-12-02T12:08:18Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [December 27, 2009, 6:23am UTC](https://www.ruby-forum.com/t/using-json-gems-encode-instead-of-rails/179706/2 "2009-12-27T06:23:35Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![rasmus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@rasmus](https://www.ruby-forum.com/u/rasmus)\
**Post date:** [July 3, 2010, 12:23am UTC](https://www.ruby-forum.com/t/using-json-gems-encode-instead-of-rails/179706/3 "2010-07-03T00:23:29Z")

</div>

Greg Hazel wrote:

> I’m interested in a JSONgem version of encode as well. I ran in to this  
> issue:
> 
> [#3345 to\_json does not escape unicode characters - Ruby on Rails - rails](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.

> <https://gist.github.com/honkster/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).
