c# - Using ServiceStack's JsonSerializer to serialize arrays containing some null items -
in servicestack 3.9, when deserializing json array contains nulls, null values deserialized nulls, expected. however, when serialize same array json again, nulls turn empty objects.
public class myclass { public string foo { get; set; } } [fact] public void test() { var originaljson = "[{\"foo\":\"bar\"},null]"; var arr = servicestack.text.jsonserializer.deserializefromstring<myclass[]>(originaljson); var result = servicestack.text.jsonserializer.serializetostring(arr); // output actually: [{"foo":"bar"},{}] assert.equal(originaljson, result); // fails }
is expected behavior? or there way serialize array containing nulls, , have null items appear in json nulls rather empty objects?
note when serializing array of literals, strings, null values returned null values, , not objects.
i had problem too. found if array cast object[]
before calling serializetostring()
null
values output expected.
var result = servicestack.text.jsonserializer.serializetostring((object[])arr); // result correct [{"foo":"bar"}, null]
you can globally set serialization of myclass
using jsconfig
:
jsconfig<myclass[]>.rawserializefn = (obj) => ((object[])obj).tojson();
Comments
Post a Comment