Re: #to_json incompatibilities between ActiveSupport & JSON gems

Kenneth Kalmer
2008-11-26T14:24:32Z

On Wed, Nov 26, 2008 at 4:15 PM, Michael Koziarski
<michael@koziarski.com>wrote:

>
> > Hi all
> >
> > Just spotted a issue when using the json & activesupport gems in
the same
> > project. Below is a simple irb session showing the effect:
> >
> > irb(main):001:0> require ''json''
> > => true
> > irb(main):002:0> Time.now.to_json( nil, 1 )
> > => "\"Wed Nov 26 15:56:02 +0200 2008\""
> > irb(main):003:0> require ''activesupport''
> > => true
> > irb(main):004:0> Time.now.to_json( nil, 1 )
> > ArgumentError: wrong number of arguments (2 for 1)
> >         from (irb):4:in `to_json''
> >         from (irb):4
> >
> > Searching through the json_pure-1.1.3 code it seems the #to_json
> signature
> > is "def to_json(*)", and in activesupport''s Time
it''s def to_json(options
> > > nil). Looking through the other activesupport extensions for JSON
it
> seems
> > to be the same case.
> >
> > Any suggestions? Any objections to me diving in and fixing it?
>
> What would the fix look like?   what are nil and 1 in the context above?
>

On the nil & 1, looking at line 237 in
(/usr/lib/ruby/gems/1.8/gems/)json_pure-1.1.3/lib/json/pure/generator.rb
you''ll see this:

  value.to_json(state, depth + 1)

state can either be nil (as in my tests), or something unknown to me at this
stage. And depth it seems is 0, so I tested with 0 + 1. This is inline with
the json gems take on "def to_json(*a)".

As for the fix, just changing the argument lists to use a bang parameter and
then appropriate extracting what activesupport expects from the argument
array...