I'm trying to replace our existing serialization with Proto-buf. Problem is we currently use ISerializable to check whether data has changed and only serialize the original value if the data has changed. This is done by using two nullable variables and only adding the original value in ISerializable.GetObjectData to the info object if the value has changed.
When deserializing, in the ISerializable constructor, I read the SerializationInfo to find which members were serialized and which ones were not. If an original value was not serialized, it's value is set to the current value. (therefore saving resources as it wasn't serialized).
Is there a way in Protobuf-net for me to find out what fields were deserialized? I am using the ShouldSerialize pattern to not send the original value as I explained above, but when I get to the other end I need to know what fields were serialized to be able to set the original value.
Edit: More Details, here is a sample class.
[Serializable()]
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class SomeClass : ISerializable
{
internal int? _key;
internal int? _originalKey;
internal bool ShouldSerialize_key()
{
return _key.HasValue;
}
[NonSerialized]
public bool _keySpecified;
internal bool ShouldSerialize_originalKey()
{
return _key != _originalKey;
}
[NonSerialized]
public bool _originalKeySpecified;
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
// Use this to set the _originalKey if it hasn't been specified.
if (!_originalKeySpecified)
{
_originalKey = _key;
}
}
}
As you can see, _originalKey is not serialized if it has the same value as the _key. When the object is Deserialized, I want to know if _originalKey was Deserialized or not. I thought your answer of _originalKeySpecified would work, but in the class above, once I Deserialize, the _originalKeySpecified is always false. Does the Protobuf deserialization process set the value? (Note that I can't use the ShouldSerialize property to decide whether to set the _originalKey when deserialized as it may have been changed from null to another value, which I need to know when saving in the data store.
_originalKeySpecifiedis not a bool property, so does not match the pattern it is looking for. Nor is_keySpecifieda property, note. - Marc Gravell*Specifiedproperty, then it looks for aShouldSerialize*method, then it stops. So basically you would also move some of your other logic to*Specified. This order is because*Specifiedallows bothgetandset(separately);ShouldSerialize*is only effectively aget, so is a second choice. - Marc GravellShouldSerialize*does (I've just tested it) - I'll take a look at why that isn't working later - Marc Gravell