Ruby serializing object to Json, Xml, Yaml?

Ruby has built-in automagical serialization/deserialization to binary and yaml Yaml: require 'yaml' serialized = CleanClass.new. To_yaml object = YAML. Load(serialized) Marshal: serialized = Marshal.

Dump(CleanClass. New) object = Marshal. Load(serialized).

Ruby has built-in automagical serialization/deserialization to binary and yaml. Yaml: require 'yaml' serialized = CleanClass.new. To_yaml object = YAML.

Load(serialized) Marshal: serialized = Marshal. Dump(CleanClass. New) object = Marshal.

Load(serialized).

Yep, I have seen these, but Json is most important currently for me, then Xml, then Yaml. So I was hoping there would be a gem or library that does everything from on simplified interface. – Grofit Oct 27 at 13:09 The JSON format is too limited to capture the state of an entire ruby object.It only has primitives, so you would need to customize your JSON to include type information of the instance variables.

Furthermore, if the goal is serialization so the object can be read back somewhere else, you would probably need to adhere to an API. You could say a class's responsibility is converting itself to an external format, and reading that format back in. – Ewout Oct 27 at 13:26 Like I said in the response to Dave above, I don't see why you need to tell ruby how to serialize these classes (using the assumptions above).

All they really need is a recursive method to go through each instance of an object, until they reach another object, then go down further into the tree, I am sure all objects end up as a model of simple types at the end. The only time I could see the user needing to input any serialization logic themselves is if they want to change the name of the keys, i. E attr_accessor :Surname to be serialized to lastname or something similar.

– Grofit Oct 27 at 14:28 First, you don't want only the "public" instance variables to be serialized. If you need to construct the object back, you also need the private variables. Second, there's no such thing as public instance variables.

Attr_accessor is actually just defining a getter and a setter for the always-private instance variables. What you want could be done, but it is not trivial and I don't see why you would need it anyway. If you don't care about the serialization format, just use YAML for portable output or Marshal.

If you do care, use something like ActiveModel. – Ewout Oct 27 at 15:10 Sorry if I was vauge, I want to adhere to a standard, be it Json, Yaml, Xml. I however want an object agnostic of either of them to be converted into and back out of the said format.

I thought Ruby still had the idea of public and private, just expressed it differently, either way I will rephrase, I would assume only variables be serialized, methods and constants needn't be. Doesn't every ruby object have an instance_variables method which gives you an array of every variable, with that array you can serialize and unserialize using instance_variable_get/instance_variable_set. – Grofit Oct 27 at 15:48.

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) 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.

Care to elaborate?

When you are trying to use to_xxxx with an instance of one of your own classes you have to write a to_xxxx method within your class to tell it how to serialize (from what I have currently read, or it comes out like # instead of {something:blah}). That is the problem I don't want to have to pollute the model with serialization concerns. – Grofit Oct 27 at 13:08 I wouldn't call it polluting, since that is kind of the way you work normally in ruby: directly on the object instead of functionally – robustus Oct 27 at 13:58.

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.

Could you elaborate more, I kinda know you can add to classes after the declaration, which is maybe what you mean. However even if I wrote some code outside of that class to tell it how to serialize, thats more code I have to maintain within my project, and would have to be written per class type. I.

E if a Person class has a firstname and surname, then a business class has name, address fields then you would need to write 2 separate bits of serialization logic to deal with them, with complex object graphs this would begin to become unmaintainable. – Grofit Oct 27 at 13:13 Existing serialization code isn't told how to write out an object, at least for simple properties, it interrogates the object. If you have complex serialization needs you'll have to tell it how /somewhere/.

You're basically asking for two things: I want complete control of how my class serialized, but I don't want that information in my class, and I want it handled automatically. It doesn't work like that-you need to define how it will be serialized /somewhere/. – Dᴀᴠᴇ Nᴇᴡᴛá´?

É´ Oct 27 at 13:22 Let me just clarify, I am not asking for complete control of how my class is serialized. I just want it serialized, the following assumptions can be made of any class that is auto serialized without any effort on the users part. Only publicly accessible fields/vars are serialized, and they are used as a key value pair UNLESS the value is another class, in which you use recursion to keep going down the tree until all simple types have been serialized.

I think they are fair assumptions and (although I don't know ruby that well) should be easy enough to get access to. – Grofit Oct 27 at 14:23 What happens when your object has a reference to itself as an attribute, or a reference to another object which in turn contains a reference back to your object as one of its attributes? Or any other kind of cyclic references for that matter.

– Lars Haugseth Oct 27 at 17:34 Then you shouldn't be serializing it... just joking, Let me just clarify, I am NOT AGAINST people manually configuring certain aspects of how to serialize their objects. I just think that a good deal if not all of common scenarios can be auto serialized. I didn't want to bring other languages into this, but coming from .

Net I can easily serialize models to json, xml, binary or pretty much any other format, Java is pretty much the same and im sure Python is able to do the sort of thing I am talking about, but I think they call it pickling. I just think that 99% of serialization needs are simple – Grofit Oct 27 at 18:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions