I am having trouble passing data between 2 processes that are using different .net versions (net8 and net48)
The nature of the data is such that the data type is define as ‘object’ so that different data can be passed in different cases (this is something I cannot change). I need to use the serialization options ensure that type information is passed with the json.
Simple example of the problem, build using VS2022 and using Newtonsoft 13.0.3
This raises an exception
Newtonsoft.Json.JsonSerializationException
HResult=0x80131500
Message=Error resolving type specified in JSON ‘System.Double[], System.Private.CoreLib’.
JsonSerializationException: Could not load assembly ‘System.Private.CoreLib’.
TestData (netstandard2.0)
using System;
namespace Data
{
using System.Collections.Generic;
public class TestData
{
public object[] data;
}
}
WriteJson (net8)
namespace WriteJson
{
using Data;
using Newtonsoft.Json;
internal class Program
{
static void Main(string[] args)
{
var testData = new TestData()
{
data = new object[]
{
new[] { 1.0, 2.0, 3.0 },
new[] { 4.0, 5.0, 6.0 },
}
};
var serSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto
};
var serializer = JsonSerializer.Create(serSettings);
var fileName = @"C:TEMPTestData.json";
using (var fs = new StreamWriter(fileName, false))
using (var writer = new JsonTextWriter(fs))
{
serializer.Serialize(writer, testData);
writer.Close();
fs.Close();;
}
}
}
}
ReadJson (net48)
namespace ReadJson
{
using Newtonsoft.Json;
using System.IO;
using Data;
internal class Program
{
static void Main(string[] args)
{
var serializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
var fileName = @"C:TEMPTestData.json";
using (var reader = new StreamReader(fileName))
{
var serializer = JsonSerializer.Create(serializerSettings);
var result = (TestData)serializer.Deserialize(reader, typeof(TestData));
}
}
}
}
JSON
{"data":
[{"$type":"System.Double[], System.Private.CoreLib","$values":[1.0,2.0,3.0]},
{"$type":"System.Double[], System.Private.CoreLib","$values":[4.0,5.0,6.0]}]
}
I would hope to be able to serialize/deserialize between the different versions of .NET but I get the exception presumably because System.Provate.CoreLib which contains the type in net8 is not understood by net48
James is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.