JSON.generate raises exception in v3.0.2 + json_pure => '~>1.4.3' #762

lighthouse-import opened this Issue May 16, 2011 · 7 comments


None yet

2 participants


Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6047
Created by Ken Mayer - 2011-02-17 07:38:09 UTC

When using the json_pure gem, this fails in v3.0.2, but succeeds in v3.0.0. It does not fail with json 1.4.6 with native extensions.

Here's the commit the created the bug (determined by git bisect start v3.0.2 v3.0.0):


How to reproduce

You'll need the following Gemfile in a test rails application:

source 'http://rubygems.org'
gem 'rails', :path => '../rails/'
gem 'json_pure'

Then run this on the command line:

bundle exec rails runner 'JSON.generate([0])'

Here's a stack trace:

/Users/kmayer/Documents/rails/railties/lib/rails/commands/runner.rb:50: undefined method `merge' for #<JSON::Pure::Generator::State:0x102181890> (NoMethodError)
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:46:in `encode'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:45:in `encode'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:30:in `encode'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/core_ext/object/to_json.rb:15:in `to_json'
    from /Users/kmayer/.rvm/gems/ruby-1.8.7-head@jsonbug/gems/json_pure-1.4.6/lib/json/pure/generator.rb:228:in `generate'
    from /Users/kmayer/.rvm/gems/ruby-1.8.7-head@jsonbug/gems/json_pure-1.4.6/lib/json/common.rb:210:in `generate'
    from (eval):1
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands.rb:39:in `eval'
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands/runner.rb:50
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands.rb:39:in `require'
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands.rb:39
    from script/rails:6:in `require'
    from script/rails:6

Imported from Lighthouse.
Comment by Ken Mayer - 2010-12-02 20:14:43 UTC

Patch file with test & fix...


Imported from Lighthouse.
Comment by Ken Mayer - 2010-12-02 20:40:20 UTC

From f52f3295196d48015c1bd5f337a5978e14c3acd6 Mon Sep 17 00:00:00 2001
From: Kenneth Mayer <kmayer@bitwrangler.com>
Date: Thu, 2 Dec 2010 09:11:08 -0800
Subject: [PATCH] bug: JSON::Pure reuses a State object for options

Fixes [#6047 state:resolved]

An earlier commit (33b95) tries to merging encoding
options (active_support/json/encoding.rb:#options_for)
would raise a method_missing error -- but only
if you were using json_pure.

This converts State objects back to an options hash
via :to_h
 activesupport/lib/active_support/json/encoding.rb |    4 ++--
 activesupport/test/json/encoding_test.rb          |    5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index c8cac52..e8d14b3 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -36,8 +36,8 @@ module ActiveSupport
       class Encoder
         attr_reader :options

-        def initialize(options = nil)
-          @options = options
+        def initialize(options_or_state = nil)
+          @options = options_or_state.respond_to?(:to_h) ? options_or_state.to_h : options_or_state
           @seen = []

diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index e0494de..622eca2 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -125,6 +125,11 @@ class TestJSONEncoding < Test::Unit::TestCase
     a[:sub] << a
     assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) }
+  def test_no_exception_raised_with_json_pure
+    require 'json/pure/generator'
+    assert_equal "[]", JSON::Pure::Generator::State.new.generate([])
+  end

   def test_hash_key_identifiers_are_always_quoted
     values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}

Imported from Lighthouse.
Comment by dvdplm - 2010-12-28 18:05:01 UTC

Confirmed in rails 3.0.3


Imported from Lighthouse.
Comment by Josh Kalderimis - 2011-02-11 13:49:12 UTC

Hi Ken,

I have tried replicating this issue but don't see any errors like you mention.

I am pretty sure that this is more of a json_pure issue than a rails issue.

For example

bundle exec rails runner 'JSON.generate([0])'

is not testing how active support helps generate json, it is only running the generate json_pure method within a rails environment.

Also, your test is not testing rails at all but only json_pure.

Have you tried updating your json_pure version?




Attachments saved to Gist: http://gist.github.com/971731


I'm experiencing this issue with version 3.0.10. The proposed patch works for me.


I don't have permissions to re-open this ticket, but I think it should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment