Extension method .ToJsv() from ServiceStack.Text ignores null values in collections

44 views Asked by At

My test class:

public class TestA
{
    public IEnumerable<string> Collection { get; set; }
}

When I call extension method .ToJsv() from ServiceStack.Text on TestA object with property Collection which is an array with some null values, the result does not contain null values. Even setting JsConfig.IncludeNullValues = true; does not bring the solution.

Test code:

var obj = new TestA { Collection = new[] { "T", null } };
var result = obj.ToJsv(); //here I get {Collection:[T,]} instead of {Collection:[T,null]}

Thanks for any suggestions.

1

There are 1 answers

0
mythz On

Given its format JSV can’t serialize null values as they’d be indistinguishable from the ‘null’ literal string so it’s lack of a value is what indicates a property is null, but that’s no something that’s you’ll be able to rely on if you want to send null items in a collection in which case you’d need to special casing like maintaining which items are null in a different property or you’d need to use a different serializer.