AnthonyCarl / NodaTime.Serialization.ServiceStackText

ServiceStack.Text support for NodaTime.
BSD 2-Clause "Simplified" License
18 stars 4 forks source link

how to deserialize null LocalDate from csv data #8

Open githubfanster opened 6 years ago

githubfanster commented 6 years ago

Hello!

I'm just trying to get a feel for this library. I need some guidance on how to get the marked code below to not throw exception when deserializing null data to a null LocalDate. what might be the easiest workaround?? thank you for helping!

static void NodaTimeModelCsvTest() { //one-time setup to register NodaTime serializers with ServiceStack DateTimeZoneProviders.Tzdb .CreateDefaultSerializersForNodaTime() .ConfigureSerializersForNodaTime();

        var nodatimeModels = new List<NodaTimeModel>();

        var data = new List<NodaTimeModel>()
        {
            new NodaTimeModel { CreatedAt = LocalDate.FromDateTime(DateTime.Now), CreatedBy = "one"},
            new NodaTimeModel { CreatedAt = LocalDate.FromDateTime(DateTime.Now), CreatedBy = "two"},
            new NodaTimeModel { CreatedAt = null, CreatedBy = "evan"}
        };
        var str = CsvSerializer.SerializeToCsv<NodaTimeModel>(data);
        nodatimeModels = str.FromCsv<List<NodaTimeModel>>();  //****this throws cannot be null exception; would like it to be ok to deserialize into a null LocalDate
    }
}

public class NodaTimeModel
{
    public int Id { get; set; }
    public LocalDate? CreatedAt { get; set; }
    public string CreatedBy { get; set; }
}
tvjames commented 6 years ago

Looks like the behaviour of SS.Text might have changed a little, according to this comment: https://github.com/AnthonyCarl/NodaTime.Serialization.ServiceStackText/blob/dadc41a53c1309a42e804d61d8ebc57b7f18e033/src/NodaTime.Serialization.ServiceStackText/Extensions.cs#L178-L179 That said, it looks like it's working as expected if serialising to JSON.