将对象序列化为JSON,XML,YAML?

时间:2022-09-15 12:58:36

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.

我问了一个关于序列化和验证的先前问题。有人提到使用JSON gem,它允许我告诉我的对象如何使用to_json方法进行序列化,但Ruby似乎很容易做很多复杂的事情,但另一方面,一些非常简单的东西似乎相当复杂,序列化正在其中一件事。

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.

我知道ActiveModel具有序列化功能,但我需要弄脏我的类来执行此操作,并且如果可能的话我不想更改模型。 ActiveSupport似乎满足JSON标准,因为我可以调用它,它将需要一个对象并吐出JSON,但我想支持其他类型。

Any further information would be great!

任何进一步的信息都会很棒!

3 个解决方案

#1


11  

Ruby has built-in automagical serialization/deserialization to binary and yaml.

Ruby内置了二进制和yaml的自动序列化/反序列化。

Yaml:

YAML:

require 'yaml'
serialized = CleanClass.new.to_yaml
object = YAML.load(serialized)

Marshal:

元帅:

serialized = Marshal.dump(CleanClass.new)
object = Marshal.load(serialized)

#2


1  

Have your magic serialization method dirty the object for you; the emdirtering can happen on a per-object basis.

让你的魔法序列化方法为你弄脏对象; emdirtering可以基于每个对象发生。

Or dirty at your class level.

或者在你的班级肮脏。

Either way, your mainline code doesn't see it.

无论哪种方式,您的主线代码都看不到它。

#3


1  

For JSON and YAML it seems pretty easy, since they would only be wrappers for the to_yaml and to_json methods (or YAML.load and from_json respectively)

对于JSON和YAML,它看起来很简单,因为它们只是to_yaml和to_json方法的包装器(或者分别是YAML.load和from_json)

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.

对于JSON,您必须围绕core-Types(或实现to_json的其他类型)包装自己的类,例如首先实现一个to_hash方法,然后可以将其转换为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.

由于层次结构方面,XML要复杂得多,你必须对它进行标准化,但实际上我不能从你的解释中理解基本的_...方法有什么问题。这实际上是我们在Ruby中使用的约定。如果您担心模型的污染,可以将这些方法放在一个模块中,并将该模块包含在类中。

module Foo
  def to_serialized_type
   ...
  end
end
class CleanClass
  include Foo
end

#1


11  

Ruby has built-in automagical serialization/deserialization to binary and yaml.

Ruby内置了二进制和yaml的自动序列化/反序列化。

Yaml:

YAML:

require 'yaml'
serialized = CleanClass.new.to_yaml
object = YAML.load(serialized)

Marshal:

元帅:

serialized = Marshal.dump(CleanClass.new)
object = Marshal.load(serialized)

#2


1  

Have your magic serialization method dirty the object for you; the emdirtering can happen on a per-object basis.

让你的魔法序列化方法为你弄脏对象; emdirtering可以基于每个对象发生。

Or dirty at your class level.

或者在你的班级肮脏。

Either way, your mainline code doesn't see it.

无论哪种方式,您的主线代码都看不到它。

#3


1  

For JSON and YAML it seems pretty easy, since they would only be wrappers for the to_yaml and to_json methods (or YAML.load and from_json respectively)

对于JSON和YAML,它看起来很简单,因为它们只是to_yaml和to_json方法的包装器(或者分别是YAML.load和from_json)

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.

对于JSON,您必须围绕core-Types(或实现to_json的其他类型)包装自己的类,例如首先实现一个to_hash方法,然后可以将其转换为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.

由于层次结构方面,XML要复杂得多,你必须对它进行标准化,但实际上我不能从你的解释中理解基本的_...方法有什么问题。这实际上是我们在Ruby中使用的约定。如果您担心模型的污染,可以将这些方法放在一个模块中,并将该模块包含在类中。

module Foo
  def to_serialized_type
   ...
  end
end
class CleanClass
  include Foo
end