This is by design.
By default (to save bandwidth) ServiceStack text serializers do not allocate null to the wire. If the generated JSON is not null , then the property will not be set when it is deserialized, so it takes the default value specified in the constructor.
You can enable null values ββwith:
JsConfig.IncludeNullValues = true;
This is a static property (Shared), so setting it must be applied globally in the process.
This test passes when using JsonSerilaizer :
[Test] public void Deserialize_WithNullCollection_CollectionIsNull() { JsConfig.IncludeNullValues = true; var item = new Foo { Strings = null }; var json = JsonSerializer.SerializeToString(item); var result = JsonSerializer.DeserializeFromString<Foo>(json); Assert.IsNull(result.Strings); } public class Foo { public Foo() { Strings = new List<string>(); } public List<string> Strings { get; set; } }
It does not work in JSV format (ie TypeSerializer ), which does not support null values, since it cannot distinguish it from the string literal "null" . Therefore, if you want to use the JSV TypeSerializer, you must assume that null means the default property values ββfor the type (i.e. as specified in the default constructor).
source share