Ruby's JSON library supports the Marshal interface. Short answer: you need to define #to_json
and self#json_create
in your class.
The trick is that you need to store the name of the class you want to round-trip back to in the json itself; the default place to do this is as the value of the key json_class
and there's likely no reason to change it.
Here's a ridiculously simple example:
require 'json'
class A
attr_accessor :a,:b
def initialize(a,b)
@a = a
@b = b
end
def to_json(*a)
{
"json_class" => self.class.name,
"data" => {:a => @a, :b=>@b}
}.to_json(*a)
end
def self.json_create(h)
self.new(h["data"]["a"], h["data"]["b"])
end
end
Then you can round-trip it with JSON.generate
and JSON.load
. Note that JSON.parse
will not work; it'll just give you back the expected hash.
[29] pry(main)> x = A.new(1,2)
=> #<A:0x007fbda457efe0 @a=1, @b=2>
[30] pry(main)> y = A.new(3,4)
=> #<A:0x007fbda456ea78 @a=3, @b=4>
[31] pry(main)> str = JSON.generate(x)
=> "{\"json_class\":\"A\",\"data\":{\"a\":1,\"b\":2}}"
[32] pry(main)> z = JSON.load(str)
=> #<A:0x007fbda43fc050 @a=1, @b=2>
[33] pry(main)> arr = [x,y,z]
=> [#<A:0x007fbda457efe0 @a=1, @b=2>, #<A:0x007fbda456ea78 @a=3, @b=4>, #<A:0x007fbda43fc050 @a=1, @b=2>]
[34] pry(main)> str = JSON.generate(arr)
=> "[{\"json_class\":\"A\",\"data\":{\"a\":1,\"b\":2}},{\"json_class\":\"A\",\"data\":{\"a\":3,\"b\":4}},{\"json_class\":\"A\",\"data\":{\"a\":1,\"b\":2}}]"
[35] pry(main)> arr2 = JSON.load(str)
=> [#<A:0x007fbda4120a48 @a=1, @b=2>, #<A:0x007fbda4120700 @a=3, @b=4>, #<A:0x007fbda4120340 @a=1, @b=2>]