Is it possible to conditionally serialize a property from an object using protobuf-net?
Asked
Active
Viewed 335 times
2
-
I think you need to elaborate A LOT on what you want. Under which conditions? (Most likely, the answer is 'no' - how would the client know whether or not the server sent it or not). You can create a two classes - one with all the mandatory properties, and one with the property that is only occasionally there. – Troels Larsen Feb 17 '16 at 23:01
1 Answers
3
protobuf-net supports the standard conditional serializers patterns, so for property Foo
, a method like bool ShouldSerializeFoo()
or a property bool FooSpecified
(with at least a getter) should work fine. These same techniques are supported my multiple serialization and UI frameworks.
Or more simply, for value-type properties, you can just use Nullable<T>
. A null value will not be serialized.

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900