我想知道如何为以下类实现自定义序列化程序/反序列化程序:
[JsonConverter(typeof(UnderlyingTypeConverter))]
public class ZoneProgramInput
{
public string Name { get; set; }
public Subject<object> InputSubject { get; }
private IDisposable InputDisposable { get; set; }
public Type Type { get; set; }
public object Value { get; set; }
}
要求是我想使用属性Value
中存储的类型序列化/反序列化属性Type
(类型为对象),而不是类型object
。如果我有以下代码:
var zpi = new ZoneProgramInput() { Type = typeof(System.Drawing.Color), Value = System.Drawing.Color.Red };
var serializedZpi = JsonConvert.SerializeObject(zpi);
var deserializedZpi = JsonConvert.DeserializeObject<ZoneProgramInput>(serializedZpi);
变量deserializedZpi包含反序列化的zpi实例,deserialized.Value应为System.Drawing.Color
类型。如果没有自定义转换器,则会将其反序列化为字符串而不是System.Drawing.Color
。作为一个注释,我只是随意选择了System.Drawing.Color。这种类型可以是任何类型。
我有一个名为UnderlyingTypeConverter的转换器类(在上面的代码中设置为ZoneProgramInput的转换器):
public class UnderlyingTypeConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ZoneProgramInput);
}
}
我应该在ReadJson / WriteJson方法中填写什么,以确保Value
属性使用Type
属性中存储的类型序列化和反序列化?我是尝试浏览Google和StackOverflow以获取ReadJson / WriteJson的示例,但我没有找到任何可以帮助我以这种方式找到类型的内容。感谢您的帮助。
Value
仍然将Value
序列化/反序列化为字符串。我也尝试使用C#dynamic
关键字,结果相同。另外,TypeNameHandling显然不适用于定义为对象类型的东西。它只是将它们序列化为字符串而不是对象。
答案 0 :(得分:1)
你需要做到这一点真是太遗憾了,但我真的没有办法解决这个问题。这是一个应该工作的转换器:
public class UnderlyingTypeConverter : JsonConverter
{
public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
var result = new ZoneProgramInput();
// Deserialize into a temporary JObject
JObject obj = serializer.Deserialize<JObject>(reader);
// Populate the ZoneProgramInput object with the contents
serializer.Populate(obj.CreateReader(), result);
// Overwrite the "Value" property with the correct value based on the
// "Type" property.
result.Value =
obj.GetValue("value", StringComparison.OrdinalIgnoreCase)
.ToObject(result.Type, serializer);
return result;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ZoneProgramInput);
}
public override bool CanWrite
{
get { return false; }
}
}
答案 1 :(得分:0)
我正在使用这种方式来序列化System.Object,它可以是不同的类型(可以对其进行修改以支持对象的组合)。
请注意,如果该System.Object中可能存在的所有对象都有某些基类/接口,请使用它代替对象并启用此选项:https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm
这里是针对System.Object的情况:
public class SerializeMe
{
[JsonConverter(typeof(ObjectJsonConverter))]
public JsoSerializableObjectContainer VisualData { get; set; } = new JsoSerializableObjectContainer();
}
public class JsoSerializableObjectContainer
{
public string ObjectTypeAssemblyName { get; set; }
public string ObjectTypeName { get; set; }
public string ObjectTypeData { get; set; }
[JsonIgnore]
public object Data { get; set; }
}
public class ObjectJsonConverter : JsonConverter
{
private static Assembly[] _assemblies;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dataObj = (JsoSerializableObjectContainer) value;
if (dataObj?.Data == null)
return;
var objType = dataObj.Data.GetType();
dataObj.ObjectTypeName = objType.FullName;
dataObj.ObjectTypeAssemblyName = objType.Assembly.FullName;
dataObj.ObjectTypeData = JsonConvert.SerializeObject(dataObj.Data);
var ser = JsonSerializer.Create();
ser.Serialize(writer, dataObj);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonSerializer = JsonSerializer.Create();
var container = jsonSerializer.Deserialize<JsoSerializableObjectContainer>(reader);
if (container != null)
{
if (_assemblies == null)
{
_assemblies = AppDomain.CurrentDomain.GetAssemblies();
}
var assembly = _assemblies.Single(t => t.FullName == container.ObjectTypeAssemblyName);
var deserializationType = assembly.GetType(container.ObjectTypeName);
if (deserializationType == null)
{
throw new JsonException(
$"Can't find type for object deserialization {container.ObjectTypeName}"); //Probably type was deleted from code
}
var myObject = JsonConvert.DeserializeObject(container.ObjectTypeData, deserializationType);
container.Data = myObject;
return container;
}
return null;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(JsoSerializableObjectContainer);
}
}
测试代码:
var ser = new SerializeMe();
ser.VisualData.Data = (int)1;
var serializationResult = JsonConvert.SerializeObject(ser);
var deserializedObject = JsonConvert.DeserializeObject<SerializeMe>(serializationResult);
ser.VisualData.Data = new System.Drawing.Rectangle(0, 1, 2, 3);
serializationResult = JsonConvert.SerializeObject(ser);
deserializedObject = JsonConvert.DeserializeObject<SerializeMe>(serializationResult);
注意:只能使用objType.AssemblyQualifiedName中的第二个,而不是ObjectTypeAssemblyName和ObjectTypeName,但是它可能会失败,因为它还会在搜索类型时检查程序集版本。并且在更新软件包或dll版本更改后,将无法找到类型。