0

Suppose I have a simple class

class Person
  attr_accessor :name
  def say
    puts name
  end
end

Is there a way to serialize it to JSON and back and get instance of the same class? For example I would like to have a code like

p = Person.new
p.name = 'bob'
json = JSON.serialize p
# json should be smth. containing { 'name' : 'bob' } 
# and maybe some additional information required for later deserialization
p2 = JSON.deserialize
p2.say
# should output 'bob'

I tried as_json (from ActiveSupport I guess), but result is {'name': 'bob'} and obviously type information is lost and after deserialization I just have a hash, not a Person instance.

ironic
  • 8,368
  • 7
  • 35
  • 44

1 Answers1

1

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>]  
Bill Dueber
  • 2,706
  • 17
  • 16