2

We need to migrate from dotnet core 2.2 to dotnet core 3.1. We have an object which has a property of class System.Version. We didn't use VersionConverter while serialization and de-serialization with dotnet core 2.2. So the serialized object with dot net core 2.2 had serialized version output in the form {"Major":2,"Minor":0,"Build":20,"Revision":0,"MajorRevision":0,"MinorRevision":0} without the use of VersionConverter.

With dotnet core 3.1 the default serialization (Without the use of VersionConverter) is 2.0.20.0

We have certain metadata already stored in the former format and other components of the system are also relying on it. Is there a way to continue with the former format for serialization and de-serialization (using newtonsoft) with the new dotnet core version 3.1 ? Currently we want to make the components across the system backward compatible and then onboard to the serialization and de-serialization with VersionConverter.

vasari
  • 21
  • 4
  • https://stackoverflow.com/questions/55666826/where-did-imvcbuilder-addjsonoptions-go-in-net-core-3-0 – Daniel A. White Jan 08 '20 at 14:42
  • We still want to use Newtonsoft.Json for serialization and deserialization – vasari Jan 08 '20 at 14:53
  • the main answer there covers how to get it back. – Daniel A. White Jan 08 '20 at 14:54
  • We are using JsonConvert.SerializeObject(version). This is accessible in both older and recent version. This serialization is giving different outputs for .NET core 2.2 and .NET core 3.1 2.2:`{"Major":2,"Minor":0,"Build":20,"Revision":0,"MajorRevision":0,"MinorRevision":0}` 3.1: `2.0.20.0` – vasari Jan 08 '20 at 14:59
  • there must be a converter registered in thye global configuration. you shouldn't depend on that global behavior. – Daniel A. White Jan 08 '20 at 15:00
  • True we shouldn't depend on that and should have used `JsonConvert.SerializeObject(version, new VersionConverter())` to begin with. But now that we have landed in this position what is the possible way out. – vasari Jan 08 '20 at 15:02
  • You have two options. (1) add the `VersionConverter` to the global configuration and use Newtonsoft globally, or (2) add `VersionConverter` to the local configuration for one call to `SerializeObject`. It's unclear to me what the issue is. –  Jan 08 '20 at 15:48
  • Do you want to revert back to [tag:json.net], or use the new [tag:system.text.json]? In your question you ask, *Is there a way to continue with the former format for serialization and de-serialization (using newtonsoft) with the new dotnet core version 3.1 ?* but in comments you state, *We still want to use Newtonsoft.Json for serialization and deserialization*. The answer is completely different depending upon what you want. – dbc Jan 08 '20 at 17:35

1 Answers1

2

We had the same de-serialization exception on System.Version types on upgrading from .NET Core 2.1 to 3.1.

We created a converter:

public class VersionConverter : JsonConverter<Version>
{
    public override void WriteJson(JsonWriter writer, Version value, JsonSerializer serializer)
    {
    }

    public override Version ReadJson(JsonReader reader, Type objectType, Version existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        var dict = serializer.Deserialize<Dictionary<string, int>>(reader);
        var version = new Version(dict["Major"], dict["Minor"], dict["Build"], dict["Revision"]);

        return version;
    }

    public override bool CanWrite => false;
}

Then we use the converter to de-serialize:

var result = JsonConvert.DeserializeObject<SomeTypeWithVersionProperty>(serializedValue, new VersionConverter());