Reputation: 617
I am attempting to serialise some records into CSV using the ServiceStack.Text library.
I am using inheritance, specifically abstract classes and the properties on the child types are not being output. Yes I know this is a bad idea but as I have no need to deserialise the types and I'm not making a public API. Regardless, this scenario seems to be supported by the docs.
For this example:
public abstract class ResultBase
{
public int MinuteOffset { get; set; }
public double SegmentDuration { get; set; }
}
public class EventIndex : IndexBase
{
public int EventsTotal { get; set; }
public int EventsTotalThresholded { get; set; }
}
And the code to serialise:
var destination = new FileInfo("C:\\somefile.txt")
using (var stream = destination.CreateText())
{
JsvStringSerializer s = new JsvStringSerializer();
var o = s.SerializeToString(results);
stream.Write(o);
CsvSerializer.SerializeToWriter(results, stream);
}
The CSV serialiser outputs this (not what I want):
MinuteOffset,SegmentDuration
0,0
But, the JSV serialiser seems to behave as expected:
[{__type:"AnalysisBase.EventIndex, AnalysisBase",EventsTotal:0,EventsTotalThresholded:0,MinuteOffset:0,SegmentDuration:0}]
Why are there differences in the fields output, is there anyway I can get the CSV serialiser to output all child properties?
Upvotes: 2
Views: 537
Reputation: 9319
The CsvSerializer.SerializeToWriter is a generic method which is not operating on the runtime type of the object. If you are calling for serialization through a base type of the current instance then the serializer will not now any other properties then the base one's.
public static void SerializeToWriter<T>(T value, TextWriter writer)
{
if (value == null) return;
if (typeof(T) == typeof(string))
{
writer.Write(value);
return;
}
CsvSerializer<T>.WriteObject(writer, value);
}
Upvotes: 1