I am working to create a JsonConverter and a ContractResolver that I can use to manage property and type serialization without using NewtonSoft attributes.
p.s., I am surprised these features are not already created somewhere
I've created a Contract Resolver that I think will work for the serialization step. But am stuck getting a converter that will work for deserialization. I can't seem to get the values from jProperty or JToken
internal class JsonPropertyConverter : JsonConverter
{
private readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>();
// allows caller to specify name mappings
public void RenameProperty(string oldName, string newName)
{
if (_propertyMappings.ContainsKey(oldName)) return;
_propertyMappings.Add(oldName, newName);
}
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException();
public override bool CanConvert(Type objectType)
{
return objectType.GetTypeInfo().IsClass;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
string clrName;
string jsonName;
PropertyInfo currentProp = null;
// create a blank instance to set the values on
object instance = Activator.CreateInstance(objectType);
// get the properties from the object to be filled
IEnumerable<PropertyInfo> props = objectType.GetTypeInfo().DeclaredProperties.ToList();
JObject jObj = JObject.Load(reader);
List<JProperty> jProperties = jObj.Properties().ToList();
foreach (JProperty jp in jProperties)
{
// set the json and clr names
jsonName = jp.Name;
clrName = jp.Name;
if (_propertyMappings.ContainsValue(jsonName)) clrName = _propertyMappings.GeyKeyFromValue(jsonName);
// get the property
PropertyInfo prop = props.FirstOrDefault(pi => pi.CanWrite && pi.Name == clrName);
JToken tok = jObj.GetValue(jsonName);
// this is where I am stuck
// prop?.SetValue(instance, jp.Value.ToObject(prop.PropertyType, serializer));
}
return instance;
}
// was running into a recursive call to jp.Value.ToObject(prop.PropertyType, serializer) that threw errors when the type was a string
private bool RequiresSeriailzation(Type t)
{
if (t.FullName == typeof(string).FullName) return false;
if (t.GetTypeInfo().IsValueType) return false;
return true;
}
}
During serialization
clrProp: Nickname => jsonProp: nick_name (this is working in the ContractResolver)
During Deserialization
jsonProp: nick_name => clrProp: Nickname (the class above)
p.s. - can we just use the converter for both directions?