Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: Even with Rails 3.2, the same problem remains unfixed. The nasty hack to forcibly load the json gem and overwrite it, that is.</p> <p>Eventually I ended up with the following code, to entirely bypass ActiveSupport's <code>to_json</code> completely. Put it in <code>config/initializers/patches.rb</code>, and you can do <code>{}.jsonize</code> or <code>[].jsonize</code> to generate JSON string. No conflicts with anything, guaranteed.</p> <pre><code># Undo the effect of 'active_support/core_ext/object/to_json' require 'json' [Object, Array, Hash].each do |klass| klass.class_eval &lt;&lt;-RUBY, __FILE__, __LINE__ def jsonize(options = nil) ::JSON.generate self, :quirks_mode =&gt; true end RUBY end </code></pre> <p>The 8 lines of code make your app <strong>50 times</strong> faster for JSON encoding. Probably you want to do the same. :)</p> <hr> <p>I've been having a similar problem up until Rails 2.3.8.</p> <p>The problem is that <code>ActiveSupport::JSON.backend = 'JSONGem'</code> is a half-assed solution and you still need to overwrite some encoders yourself. (<strong>WARNING</strong>: for Rails 3.x, which uses MultiJson, it must be <code>ActiveSupport::JSON.backend = :json_gem</code> at least, or it will be silently no-op.)</p> <p>In my case, I needed to overwrite <code>String#to_json</code> because JSON gem 1.4.3 is better in that it doesn't blindly encode non-ascii-but-valid-UTF8 characters in the form of <code>"\uXXXX"</code> where it's not necessary, so you get shorter bytes (good for serialization) and easy-to-read results (<code>"日本語"</code> looks much sexier to my eyes than <code>"\u65e5\u672c\u8a9e"</code>).</p> <p>Here's the monkey patch that I've been using - put the following code in <code>config/initializers/patches.rb</code></p> <pre><code>module ActiveSupport module JSON module Encoding class &lt;&lt; self def escape(string) ::JSON.generate([string])[1..-2] end end end end end </code></pre> <p>and you're free to use <code>to_json</code> on anything - String, Array and Hash.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload