Skip to content

how to deserialize null LocalDate from csv data #8

@githubfanster

Description

@githubfanster

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; }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions