但是,其中一些对象无法反序列化.具体来说,我得到一些System.Reflection.RuntimePropertyInfo类型.我想以标准化的方式处理这些问题,因为我不知道反序列化时的目标类型.只要输出对象类型正确,我也不在乎.
我已尝试将CustomCreationConverter类型转换为在JsonSerializerSettings中定义的PropertyInfo.但是,即使CanConvert()返回true,也不会使用CustomCreationConverter的ReadJson().
最终结果就像我从未使用过CustomCreationConverter一样:
ISerializable type ‘System.Reflection.RuntimePropertyInfo’ does not
have a valid constructor. To correctly implement ISerializable a
constructor that takes SerializationInfo and StreamingContext
parameters should be present.
我需要CustomCreationConverter来处理ReadJson,以便我可以自己手动搜索PropertyInfo.
经过更多调查后,似乎我正在添加到JsonSerializerSettings的转换器根本没有被使用.如果我使用包含JsonConverter的Type和集合的DeserializeObject重载,则将使用Converter.我不确定转换器提供给JsonSerializerSettings的用途是什么,但我希望它们可以按照我打算在这种情况下工作.
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.Serialization.Formatters; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; namespace Json { class Program { static void Main(string[] args) { var jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All,TypeNameAssemblyFormat = FormatterassemblyStyle.Simple,Converters = new JsonConverter[] { new PropertyInfoConverter(),},}; var propertyInfo = typeof(Test).GetProperty("Name"); var serialized = JsonConvert.SerializeObject(propertyInfo,jsonSerializerSettings); var deserialized = JsonConvert.DeserializeObject(serialized,jsonSerializerSettings); } } public class Test { public string Name { get; set; } } public class PropertyInfoConverter : CustomCreationConverter<PropertyInfo> { public override bool CanConvert(Type objectType) { return typeof(PropertyInfo).IsAssignableFrom(objectType); } public override PropertyInfo Create(Type objectType) { return null; } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { return null; // This is never invoked,but is where I would attempt to find the PropertyInfo via Reflection searching. } } }
解决方法
private class PropertyInfoData { public string TypeName { get; set; } public string PropertyName { get; set; } public static PropertyInfoData FromProperty(PropertyInfo p) { return new PropertyInfoData() { TypeName = p.DeclaringType.AssemblyQualifiedname,PropertyName = p.Name,}; } public PropertyInfo Toproperty() { return Type.GetType(this.TypeName).GetProperty(this.PropertyName); } }
然后你可以像这样对它进行反序列化:
var jsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All,//Converters = new JsonConverter[] { new PropertyInfoConverter(),}; var propertyInfo = typeof(Test).GetProperty("Name"); var serialized = JsonConvert.SerializeObject(PropertyInfoData.FromProperty(propertyInfo),jsonSerializerSettings); var deserialized = ((PropertyInfoData)JsonConvert.DeserializeObject(serialized,jsonSerializerSettings)).Toproperty();
public class PropertyInfoConverter : JsonConverter { public override bool CanWrite { get { return false; } } public override bool CanConvert(Type objectType) { return typeof(PropertyInfo).IsAssignableFrom(objectType); } public override object ReadJson(JsonReader reader,JsonSerializer serializer) { string propertyName = null; string assemblyName = null; string typeName = null; while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName) { string value = reader.Value.ToString(); switch (reader.Value.ToString()) { case "Name": if (reader.Read()) { propertyName = reader.Value.ToString(); } break; case "AssemblyName": if (reader.Read()) { assemblyName = reader.Value.ToString(); } break; case "ClassName": if (reader.Read()) { typeName = reader.Value.ToString(); } break; } } } return Type.GetType(typeName + "," + assemblyName).GetProperty(propertyName); } /// <inheritdoc /> public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { // When the property "CanWrite" returns false this method is never expected to be called. throw new NotImplementedException(); }
}
注意,为了强制deserialize方法使用自定义转换器,你应该像这样调用它的泛型版本:
var deserialized = JsonConvert.DeserializeObject<PropertyInfo>(serialized,jsonSerializerSettings);
在你的例子中也是如此,这就是为什么你没有达到你的断点.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。