Hi all,
I use ServiceStack.Text v3 (latest code as of today) because of the more permissive license. I'd like to ask if some behaviour I see is correct.
So, I have a generic list. When I call myList.ToJson(), I get this:
"[{\"__type\":\"MyNamespace.MyType, MyAssembly\", ... (properties) },
{... (properties)},
{... (properties)},
{... (properties)},
{... (properties)},
]"
That is: only the first element of my list has its type saved. As a result, when I do this:
var deserialisedList = myList.ToJson().FromJson<List<object>>();
I get a list with the first element MyType and the rest strings. It does work with FromJson<List<MyType>>() but I have to use List<object> because I do not know the type at compile type. The reason for this is this block in WriteType<T, TSerializer>.WriteProperties:
if (WriteTypeInfo != null || JsState.IsWritingDynamic)
{
if (JsConfig.PreferInterfaces && TryWriteSelfType(writer)) i++;
else if (TryWriteTypeInfo(writer, value)) i++;
JsState.IsWritingDynamic = false;
}
JsState.IsWritingDynamic is initially true (I don't change it manually, it's either a default value or some code path chanes it), that's why the first element has its type added. So since JsState.IsWritingDynamic is set to false after the first element, the type of subsequent elements is not written. Is that correct behaviour? If so, is there any way of serialising this properly without the overhead of dynamic serialisation (i.e. JsConfig.IncludeTypeInfo)?