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:
And here's an example serialized output for what I need:
(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
:
2 Replies
This doesn't necessarily answer your question since you specifically asked about JsonConverter.. But one way to go about it is to use a Dictionary, e.g. implement a method like this in your Snapshot class:
Then you can call it like this:
Perhaps the same approach could be used inside your SnapshotConverter
It wont work because I'm not calling serialize specifically on Snapshot, im doing it in a class with contains another class which contains a Snapshot
I'll repost because its been many hours on this thread :)