I would guess that your type is explicitly setting the string to "" in the parameterless constructor. Could you check?
The way that protobuf-net handles this is:
- for a
null
, nothing is sent (the protobuf wire-format has no way of explicitly expressing a null
, but we can treat it as optional and omit it)
- for a
""
, a 0-length pattern is sent, which should be deserialized as ""
- for a non-empty string, the length and the string is sent, and is deserialized
During deserialization, in the null
case it simply leaves your field/property alone, since it has no data to process. If the type sets the default to ""
it will stay as ""
.
Note that in "v2" (I expect to release this in the next two weeks), you can optionally tell it to use the WCF approach of "don't run any constructor", which will have the effect of leaving it as null
even if the default constructor assigns it.
There are also some tricks you can do (with "v1") to send a bool
flag (as a separate property) to mean null
; let me know if you want an example of this.
Edit: here's an example of a "v1" trick to get around this; the "v2" approach of "ignore the constructor" is probably a better option long-term, though:
[DataContract]
class Test {
public Test() { Value = ""; } // a constructor that assigns the value
[DataMember(Order = 1)]
public string Value { get; set; } // our standard data
[DataMember(Order = 2)]
private bool ValueIsNull { // only exists to indicate explicit null
get { return Value == null; }
set { Value = value ? null : (Value ?? "");}
}
}