Hi all, I want to export/unload database content to the client side, so that it could be loaded back later. The content contains many nested associations and zig-zag references via foreign keys, and it is important to preserve the information which foreign keys point to the same object.
The problem: JSON has no notion of a repeating object.
class Content
def initialize name
@name = name
end
end
class Container
attr_accessor :content1, :content2
end
c = Content.new "this is unique"
cer = Container.new
cer.content1 = c
cer.content2 = c
puts cer.to_json
{"content1":{"name":"this is unique"},"content2":{"name":"this is unique"}}
YAML would do it correctly (as would Marshal.dump):
puts cer.to_yaml
--- !ruby/object:Container
content1: &1 !ruby/object:Content
name: this is unique
content2: *1
But both Marshal and YAML are unsafe to load from the client side. Is there another solution?