Calling out the tech: C#
, Newtonsoft.Json
.
We have a class which contains a property named SchemaVersion
that we use to inform of the Json document version once that object is ultimately serialized in to our CosmosDb.
Currently this property is set in the object class constructor, and for simplicity assume it's "1.0"
So far we've only added to our class & incremented "1.0"
to "2.0"
, and so when we deserialize document not of latest SchemaVersion
always want to 'upgrade' it and this is done by simply checking the value of that property..
private string _schemaVersion;
public string SchemaVersion
{
get => _schemaVersion != "2.0" ? "2.0" : _schemaVersion;
set => _schemaVersion = value;
}
New objects still have their SchemaVersion
set with the object constructor. This feels a bit clunky to me.. but it does the job.
I know though, that there will come a point where we need to refactor the class for some as yet unknown reason. We'll try to avoid this as much as possible.. but i can see it happening.
The challenge then becomes how do we maintain conversions between
"1.0"
-->"x.0"
"2.0"
-->"x.0"
"3.0"
-->"x.0"
"4.0"
-->"x.0"
Are there any recommended ways in which this could best be handled? The best idea that i have right now is a custom JsonConverter, which i have written before to deserialize unknown Json documents to a specific object based on a property within that document... but i wonder if that is over-complicated as we will always be deserializing into a latest known shape...
I fear that this could get complex quickly if we end up with many versions, and their conversions to the latest version all need to be maintained separately..