Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Weird error in Hash.to_json

#995

{}.to_json() => "{}" {}.to_json({})

TypeError: wrong argument type Hash (expected Data)

    from (irb):4:in `to_json'
    from (irb):4

This is in Ruby 1.8.6, Rails 2.1.1...but for some reason reloading the source fixes it:

load '/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/json/encoders/hash.rb' => [] {}.to_json({}) => "{}"

I'm baffled...what's a "Data" and why should to_json be expecting one?

Ah, this is fixed by including the line: require 'json/pure'

So I imagine this is something to do with the FFI between the regular json gem and its C code.

Reported by mt · September 8th, 2008 @ 06:38 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung
    • State changed from new to wontfix

    The problem is that both json/pure and activesupport define a to_json method on objects. with slightly different semantics. loading those various files overwrites all such methods one way or another.

    See also this thread on rails core. Hopefully a resolution will emerge from that. Until then let's close this ticket

    December 10th, 2008 @ 04:51 PM