I need to serialize many different objects using Json.NET. I really do not control the objects provided, so I generalize and de-serialize with TypeNameHandling.All .
However, some of these objects cannot be de-serialized. In particular, I get some types of System.Reflection.RuntimePropertyInfo. I would like to handle them in a standardized way, since I did not know about the type of target during de-serialization. I also don't care if the type of output object is correct.
I tried to execute a CustomCreationConverter introduced in PropertyInfo, which is defined in JsonSerializerSettings . However, although CanConvert () returns true, CustomCreationConverter ReadJson () is never used.
The end result is the same as if I had never used a CustomCreationConverter:
The ISerializable type 'System.Reflection.RuntimePropertyInfo' does not support have a valid constructor. To correctly implement ISerializable, a constructor that accepts SerializationInfo and StreamingContext parameters must be present.
I need a CustomCreationConverter to handle ReadJson so that I can manually manually search for PropertyInfo.
, , JsonSerializerSettings, . DeserializeObject, JsonConverter, . , , 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;
}
}
}