将对象序列化为 JSON、XML、YAML?
我之前问过有关序列化和验证的问题。有人提到使用 JSON gem,它允许我告诉我的对象如何使用 to_json 方法进行序列化,但是 Ruby 似乎很容易做很多复杂的事情,但另一方面,一些非常简单的事情似乎相当复杂,序列化就是其中之一。
我想知道是否有办法获得一个干净的对象:
class CleanClass
attr_accessor :variable1
attr_accessor :variable2
attr_accessor :variable3
end
cleanObject = CleanClass.new
理想情况下,我不想弄脏模型,我只想将它传递给某物并告诉它输出类型应该如此,并让它发挥其魔力。
一个例子是这样的:
jsonOutput = MagicSerializer::Json.Serialize(cleanObject)
xmlOutput = MagicSerializer::Xml.Serialize(cleanObject)
yamlOutput = MagicSerializer::Yaml.Serialize(cleanObject)
revertedJsonObject = MagicSerializer::Json.Unserialize(jsonOutput)
revertedXmlObject = MagicSerializer::Xml.Unserialize(xmlOutput)
revertedYamlObject = MagicSerializer::Yaml.Unserialize(yamlOutput)
我想传递一个对象,并获取输出的字符串,然后能够将其转换回来。
我知道 ActiveModel 具有序列化功能,但我需要弄脏我的类才能执行此操作,并且如果可能的话我不想更改模型。 ActiveSupport 似乎满足 JSON 标准,因为我可以调用它,它将获取一个对象并吐出 JSON,但我想支持其他类型。
任何进一步的信息都会很棒!
I asked a previous question about serialization and validation. Someone mentioned using the JSON gem which allows me to tell my object how to serialize with the to_json
method, however Ruby seems to do LOTS of complicated things really easily, however on the flip side some really simple things seem to be quite complicated, Serialization being one of those things.
I want to find out if there is a way to have a clean object:
class CleanClass
attr_accessor :variable1
attr_accessor :variable2
attr_accessor :variable3
end
cleanObject = CleanClass.new
Ideally, I don't want to dirty the model, I just want to pass it to something and tell it what the output type should be and let it work its magic.
An example would be something like:
jsonOutput = MagicSerializer::Json.Serialize(cleanObject)
xmlOutput = MagicSerializer::Xml.Serialize(cleanObject)
yamlOutput = MagicSerializer::Yaml.Serialize(cleanObject)
revertedJsonObject = MagicSerializer::Json.Unserialize(jsonOutput)
revertedXmlObject = MagicSerializer::Xml.Unserialize(xmlOutput)
revertedYamlObject = MagicSerializer::Yaml.Unserialize(yamlOutput)
I want to pass something an object, and get the strings outputted, then be able to convert it back.
I know ActiveModel has serialization functionality but I need to dirty my class to do this, and I don't want to change the model if possible. ActiveSupport seems to satisfy the JSON criteria as I can just call that and it will take an object and spit out the JSON, but I would like to support other types.
Any further information would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ruby 内置了对二进制和 yaml 的自动序列化/反序列化。
亚米尔:
元帅:
Ruby has built-in automagical serialization/deserialization to binary and yaml.
Yaml:
Marshal:
让你神奇的序列化方法为你弄脏对象;修改可以在每个对象的基础上进行。
或者在你的班级水平上很脏。
无论哪种方式,您的主线代码都看不到它。
Have your magic serialization method dirty the object for you; the emdirtering can happen on a per-object basis.
Or dirty at your class level.
Either way, your mainline code doesn't see it.
对于 JSON 和 YAML,这似乎很简单,因为它们只是
to_yaml
和to_json
方法(或YAML.load
和>from_json
分别)对于 JSON,您必须将自己的类包装在 core-Types(或实现 to_json 的其他类型)周围,例如首先实现一个 to_hash 方法,然后可以将其转换为 json。
由于层次结构,XML 更加复杂,您必须对其进行标准化,
但实际上我从你的解释中不明白基本的
to_...
方法有什么问题。这实际上是我们在 Ruby 中使用的约定。如果您担心模型污染,可以将这些方法放入模块中,并将该模块包含在类中。For JSON and YAML it seems pretty easy, since they would only be wrappers for the
to_yaml
andto_json
methods (orYAML.load
andfrom_json
respectively)For JSON you would have to wrap own classes around core-Types (or other types which implement to_json) e.g. first implement a to_hash method which then can be transformed into json.
XML is much more complicated because of the hierarchy-aspect, you'd have to standardize it,
but actually i don't understand from your explanation what is wrong with the basic
to_...
methods. This is actually the convention we use in Ruby. If you're concerned with pollution of models, you could put these methods in a module, and include the module in the class.