#1412 ✓resolved

to_json crashes with has of collections

Reported by Gibheer | September 16th, 2010 @ 02:47 PM

I tried to generate a json_file of two datamapper collections, but it crashed with the message "#<NoMethodError: undefined method merge' for #<JSON::Ext::Generator::State:0x00000801e8c4a8>>"

Here is a little testcase for showing the problem. The problem lies in opts, which sometimes is a JSON::Status object, which can't be converted to a propert hash.

edit: the file for the fix is dm-serializer/lib/dm-serializer/to_json.rb

require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-serializer'

DataMapper.setup(:default, 'sqlite://:memory:')

class A
include DataMapper::Resource property :id, Serial has n, :bs end

class B
include DataMapper::Resource property :id, Serial belongs_to :a end


p "without fix, it crashes"
{:b => B.all, :a => B.all.as}.to_json rescue NoMethodError => e
p e.inspect end

p "now the fix"
module DataMapper
class Collection

def to_json(*args)
  opts = args.first || {}
  # the fix
  opts = {} unless opts.respond_to? :merge
  options = opts.merge(:to_json => false)
  collection = map { |e| e.to_json(options) }

  # default to making JSON
  if opts.fetch(:to_json, true)

end end

{:b => B.all, :a => B.all.as}.to_json p "works"

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Referenced by