How can I implement partial serialization?

Recently, I have done a lot for serialization, mainly for sending objects via sockets, but I had an interesting question: is it possible to send only some of the properties of an object through a serializer?

My supposed scenario is this: you have some kind of "state" object for each client, consisting of many properties (strings, ints, bools, etc.). When your client first connects, the entire state object is serialized via Xml or a binary serializer and sent through the socket to be recreated from the other side. Both the client and server now have identical state objects. Then your server needs to change state and do this by simply setting one of the properties of the state object. A socket (either connected to state events, or part of the state object itself) can synchronize two states by reinitializing the entire object, but it looks like a single property change object will do this.

Obviously, this can be implemented manually. But it seems that the serializer should be able to serialize only one property and apply it as a patch on the other hand. Does anyone know if this is possible, or will I have to write everything from scratch?

+3
source share
2 answers

C XmlSerializer(and protobuf-net, for the binary equivalent, since protobuf-net accepts most patterns XmlSerializer), you can do this using the method:

public bool SouldSerializeFoo() {
    return fooIsDirty;
}
public string Foo {get;set;}

Foo - ", " (, set). ; , - PITA. , [XmlIgnore] public bool FooSpecified {get{...} set{...}} , , ShouldSerialize* .

+4
+1

Source: https://habr.com/ru/post/1749949/


All Articles