1

I have an Object "information" like this:

public class information
{
    [JsonProperty]
    public DateOnly date;
    [JsonProperty]
    public string summary;
    [JsonProperty]
    public TimeOnly endTime;
    [JsonProperty]
    public TimeOnly startTime;


}

now I want to serialize this object, but however I cannot serialze DateOnly and TimeOnly. This is how I serialize

List<information>? liste = new List<information>();
        


 private static void WriteFile(DateOnly date, string summary, TimeOnly startTime, TimeOnly endTime)
    {
        



        information jsonnAppointment = new information()
        {
            date = date,
            summary = summary,
            startTime= startTime,
            endTime = endTime
        };
    
        List<information> liste = GetJsonInfo();
        liste.Add(jsonnAppointment);
        
        
        using (FileStream fs = System.IO.File.Open(_appointmentPath + "appointment.json", FileMode.Open))
        {
            byte[] info = new UTF8Encoding(true).GetBytes(JsonConvert.SerializeObject(liste));
            fs.Write(info, 0, info.Length);
        }

I tried to install other packages from Newtonsoft.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Does DateOnly and TimeOnly exist in your codebase? – Mernayi Nov 07 '22 at 14:52
  • how is your appointment.json looks like? and include class of DateOnly and TimeOnly – Krishna Varma Nov 07 '22 at 14:52
  • 2
    @Mernayi [DateOnly](https://learn.microsoft.com/en-us/dotnet/api/system.dateonly?view=net-6.0) and [TimeOnly](https://learn.microsoft.com/en-us/dotnet/api/system.timeonly?view=net-6.0) – ProgrammingLlama Nov 07 '22 at 15:03
  • 2
    Support for these types [was added in 13.0.2 beta](https://github.com/JamesNK/Newtonsoft.Json/issues/2521). You can either use the beta version or create custom converters. Right now the latest NuGet package is [13.0.2 beta 2](https://www.nuget.org/packages/Newtonsoft.Json/13.0.2-beta2) – Panagiotis Kanavos Nov 07 '22 at 15:04

1 Answers1

1

You should create your own serializers

public class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
    private const string DateFormat = "yyyy-MM-dd";

    public override DateOnly ReadJson(JsonReader reader, Type objectType, DateOnly existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        return DateOnly.ParseExact((string)reader.Value, DateFormat, CultureInfo.InvariantCulture);
    }

    public override void WriteJson(JsonWriter writer, DateOnly value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString(DateFormat, CultureInfo.InvariantCulture));
    }
}

public class TimeOnlyJsonConverter : JsonConverter<TimeOnly>
{
    private const string TimeFormat = "HH:mm:ss.FFFFFFF";

    public override TimeOnly ReadJson(JsonReader reader, Type objectType, TimeOnly existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        return TimeOnly.ParseExact((string)reader.Value, TimeFormat, CultureInfo.InvariantCulture);
    }

    public override void WriteJson(JsonWriter writer, TimeOnly value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString(TimeFormat, CultureInfo.InvariantCulture));
    }
}

and pass them as options

var serializeOptions = new JsonSerializerOptions
{
    WriteIndented = true,
    Converters =
    {
        new DateOnlyJsonConverter (),
        new TimeOnlyJsonConverter (),
    }
};

jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions);
Lonli-Lokli
  • 3,357
  • 1
  • 24
  • 40