Owez
Owez
CC#
Created by Owez on 6/30/2023 in #help
Implementing JsonConverter to simplify ser/de
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
};
}
}
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!!"}
{"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? I need the generics to always work and this Snapshot will be used as an attribute of a parent class I've wrote this so far which feels like it's going in the right direction, but I'm not sure if the JsonSerializer line here is right and I'm not sure about how to implement Read:
public class SnapshotConverter<T> : JsonConverter<Snapshot<T>>
{
public override Snapshot<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not supported.");
}

public override void Write(Utf8JsonWriter writer, Snapshot<T> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName(value.Taken.ToString("s"));
JsonSerializer.Serialize(writer, value.Data, options);
writer.WriteEndObject();
}
}
public class SnapshotConverter<T> : JsonConverter<Snapshot<T>>
{
public override Snapshot<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not supported.");
}

public override void Write(Utf8JsonWriter writer, Snapshot<T> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName(value.Taken.ToString("s"));
JsonSerializer.Serialize(writer, value.Data, options);
writer.WriteEndObject();
}
}
48 replies
CC#
Created by Owez on 6/29/2023 in #help
Implementing JsonConverter to simplify Ser/De
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
};
}
}
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!!"}
{"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? I need the generics to always work and this Snapshot will be used as an attribute of a parent class I've wrote this so far which feels like it's going in the right direction, but I'm not sure if the JsonSerializer line here is right and I'm not sure about how to implement Read:
public class SnapshotConverter<T> : JsonConverter<Snapshot<T>>
{
public override Snapshot<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not supported.");
}

public override void Write(Utf8JsonWriter writer, Snapshot<T> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName(value.Taken.ToString("s"));
JsonSerializer.Serialize(writer, value.Data, options);
writer.WriteEndObject();
}
}
public class SnapshotConverter<T> : JsonConverter<Snapshot<T>>
{
public override Snapshot<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not supported.");
}

public override void Write(Utf8JsonWriter writer, Snapshot<T> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName(value.Taken.ToString("s"));
JsonSerializer.Serialize(writer, value.Data, options);
writer.WriteEndObject();
}
}
4 replies
CC#
Created by Owez on 6/27/2023 in #help
❔ 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
};
}
}
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!!"}
{"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?
3 replies