EricLarch
EricLarch

Reputation: 5773

JSON incorrect escaping Ruby 1.9.3 Rails 3.1.4

I have a problem of JSON encoding on my rails application:

h = {:status=>200, :promotions=>[{:id=>719788, :title=>"test"}]}

and result of

puts h.to_json

is

{"status":200,"promotions":{"{\"id\"=>719788, \"title\"=>\"test\"}":null}}

Which is not the expected result!

This is the correct result:

{"promotions":[{"title":"test","id":719788}],"status":200}

What could generate such error in JSON generation?

ruby -v
ruby 1.9.3p194 (2012-04-20) [x86_64-linux]
rails -v
Rails 3.1.4
gem list ==> json (1.6.6, 1.5.4)

Upvotes: 1

Views: 160

Answers (1)

EricLarch
EricLarch

Reputation: 5773

Ok, this has nothing to do with a configuration of rails or ruby... One of the engineer added this into core_extensions for Array

def to_hash # Recursively convert array to hash 
 inject({}) do |hash, (key, value)|
   value = value.to_hash if value.kind_of?(Array)
   hash.merge!({key => value})
 end
end

I guess I can delete this question tomorrow

Upvotes: 1

Related Questions