I'm trying to convert an object to JSON and then parse it again. The problem is, when I parse the JSON string I'm left with a Hash and not my original object. I found this simple example at json.rubyforge.com and tried it:
require 'json'
class Range
def to_json(*a)
{
'json_class' => self.class.name,
'data' => [ first, last, exclude_end? ]
}.to_json(*a)
end
def self.json_create(o)
new(*o['data'])
end
end
puts JSON.parse((1..10).to_json) == (1..10)
It fails as well, returning false
. Looking further it doesn't seem that json_create
is being called.
At this point I'm figuring I have to be missing something dead simple or I've run into a bug somewhere. I'm using Ruby 1.9.3. Anyone have any ideas?