自定义to_yaml和domain_type
我需要定义用于序列化/反序列化对象的自定义方法。我想做如下的事情。
class Person
def to_yaml_type
"!example.com,2010-11-30/Person"
end
def to_yaml
"string representing person"
end
def from_yaml(yaml)
Person.load_from(yaml)
end
end
声明序列化/反序列化的正确方法是什么?
I need to define custom methods for serializing/deserializing an object. I want to do something like the following.
class Person
def to_yaml_type
"!example.com,2010-11-30/Person"
end
def to_yaml
"string representing person"
end
def from_yaml(yaml)
Person.load_from(yaml)
end
end
What's the correct way to declare the serialization/deserialization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这就是我的想法
OK, here's what I came up with
如果您只想序列化属性的子集,而不是全部,则可能需要使用
to_yaml_properties
。If you just want to serialize only a subset of properties, not all of them, you may want to use
to_yaml_properties
.