C#C
C#3y ago
Owez

❔ Implementing JsonConverter to simplify input/output

I've been trying to implemement a JsonConverter for a simple class to simplify the default C# tagging using JsonSerializer. Here's the class I'm trying to convert:
public class Snapshot<T>
{
    public DateTime Taken { get; set; }
    public T Data { get; set; }

    public static Snapshot<T> NewEmpty()
    {
        return new Snapshot<T>
        {
            Taken = DateTime.MinValue,
            Data = default
        };
    }
}

And here's an example serialized output for what I need:
{"2020-06-21T00:00:00":"this is some data here!!"}

(FYI the default serializer outputs {"Taken":"2020-06-21T00:00:00","Data":"this is some data here!!"}). How do I implement a JsonConverter to Read & Write to my desired format?
Was this page helpful?