The behavior of TypeSerializer.SerializeToStream(object, Type, Stream) with regards the inclusion of type information ("__type") appears to be dependent on whether or not SerializeToStream<T>(T, stream) has been called first.
Using the latest ServiceStack.Text NuGet version (v5.11.0)
Setup code:
public interface IFoo
{
string Property { get; }
}
public class FooA : IFoo
{
public string Property { get; set; } = "A";
}
public class Test
{
// Serialize using TypeSerializer.SerializeToStream<T>(T, Stream)
public static void SerializeToStream1(IFoo item)
{
using (var stream = new MemoryStream())
{
TypeSerializer.SerializeToStream(item, stream);
Console.WriteLine("SerializeToStream<T>(T, Stream):\n{0}\n", Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length));
}
}
// Serialize using TypeSerializer.SerializeToStream(object, Type, Stream)
public static void SerializeToStream2(IFoo item)
{
using (var stream = new MemoryStream())
{
TypeSerializer.SerializeToStream(item, item.GetType(), stream);
Console.WriteLine("SerializeToStream(object, Type, Stream):\n{0}\n", Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length));
}
}
}
Given the above setup code, I would expect the output of SerializeToStream1 and SerializeToStream2 to be consistent when called on a given instance, however that is not the case:
Calling:
var a = new FooA();
Test.SerializeToStream1(a);
Test.SerializeToStream2(a);
The above shows that TypeSerializer.SerializeToStream(object, Type, Stream) only includes type information if TypeSerializer.SerializeToStream<T>(T, Stream) is called first, which does not seem correct.
Thanks for reporting @cosborne83 , I used your code to replicate the issue in a test and applied a fix. This should be resolved with the latest release of v5.11.1 that's now available on MyGet.
The behavior of
TypeSerializer.SerializeToStream(object, Type, Stream)
with regards the inclusion of type information ("__type") appears to be dependent on whether or notSerializeToStream<T>(T, stream)
has been called first.Using the latest ServiceStack.Text NuGet version (v5.11.0)
Setup code:
Given the above setup code, I would expect the output of
SerializeToStream1
andSerializeToStream2
to be consistent when called on a given instance, however that is not the case:Calling:
Outputs:
But calling:
Outputs:
The above shows that
TypeSerializer.SerializeToStream(object, Type, Stream)
only includes type information ifTypeSerializer.SerializeToStream<T>(T, Stream)
is called first, which does not seem correct.