I asked the previous question about serialization and validation. Someone mentioned the use of the JSON pearl, which allows me to tell my object how to serialize using the to_json method, however, Ruby seems to make a lot of complicated things very easy, however, on the other hand, some really simple things seem pretty complicated. Serialization is 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 pollute the model, I just want to pass it on to something and tell her what the type of output should be, and let it work with my 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 to an object and get the lines printed, and then return it back.
I know that ActiveModel has serialization functions, but I need to foul 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 it and it will take an object and spit out JSON, but I would like to support other types.
Any additional information would be great!
source share