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!