Vs Non Serializable objects

What are different serializable objects and non-serializable objects, and what makes an object serializable? And what serializable objects are related to xml?

thanks

+4
source share
4 answers

what are different serializable objects and non-serializable objects

A serializable object can be transformed into another representation, such as text, so that it can be easily transferred across the boundaries of the process, while an object that is not related to serialization cannot be.

And what makes an object serializable

In .NET, depending on the serializer that you decide to use, the object must meet certain requirements. For example, if you use BinaryFormatter , your object should be decorated with [Serializable] .

And that serializable objects are xml related

An object can be serialized in XML. In .NET, this can be achieved, for example, using the XmlSerializer class, but also with the DataContractSerializer .

+5
source

The problem is almost never related to serializing an object. The real problem is often: can you restore a useful and verbatim copy of an object when you deserialize it.

A good example is the Windows Forms control. A control cannot exist without a parent, for example, the form on which it is placed. De-serializing a control gives you a control without a parent, you must serialize the entire graph of objects to make it meaningful. Then there are properties that have a run-time value that will not be reproduced during de-serialization. For example, the Handle property. Windows will not play the same handle. Either the short-circuit key indicator is underlined or not (press the Alt key). This critically depends on the entire state of the program. Accordingly, the control does not have the [Serializable] attribute.

+3
source

The answers here are correct, but attack the concept from a different angle and hopefully increase your understanding, I suggest the following:

Think of the word serializable as convertible. Therefore, when a class is marked as serializable, it is converted to any type of view that you are looking for. A serializer (for example, BinaryFormatter or XmlSerializer) can be considered as a "converter".

With this, so to speak, you would use a serializer to convert something that can be converted (serializable). The class itself does not change, but it adds functionality to the class.

Examples:

  • Converting an object to a binary representation (serialization using binary formatting) is usually used if you want to easily write it to disk or send it to another application via a socket.
  • Another common use is to convert an object to an xml view (as shown in RandomNoob's answer) if you want to send it to a web service.

There are some caveats to consider when serializing (for example, you don’t want to serialize events in .NET at all and therefore mark them with the [field: non-serialized] attribute), so a little more research is needed in this regard, but conceptually, try to think about it as about transforming an object for storage or transfer.

+2
source

Objects that can be serialized can be “broken”, transmitted over various channels, and “restored” at the end of the receiving channel, which can be a completely different place in the exact state in which it was “broken”. You may have heard the xml mentioned in the talk about serialization, because xml provides a mechanism for this.

Consider the following object:

 Person p = new Person(); p.Age = 33; p.Name = "Magni"; 

If you want to keep this object in its current state, you could effectively present this as:

 <Person> <Name>Magni</Name> <Age>33</Age> </Person> 

This XML can then be sent all over the wire, and the original Person object can be restored or used by another object or service when using different platforms.

This view of 30,000 feet, serialization is often complicated, but I tried to answer your question in the broadest sense.

+1
source

All Articles