Introduction
Last week, one of my colleagues wanted to serialize an object along with custom property attribute to JSON. Interesting… JSON.NET, by default, will not allow serializing custom property attribute. So, you have to write custom JSON converter to fix this. Let’s take a look at how you can solve this problem.
Suppose you have following custom attribute class:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ComplexAttribute : System.Attribute
{
public string Type { get; set; }
public string DisplayName { get; set; }
}
Moreover, you use it as below:
[JsonConverter(typeof(ComplexTypeConverter))]
public class Blog
{
[ComplexAttribute(Type = "String", DisplayName = "Blog Title")]
public string Title { get; set; }
[ComplexAttribute(Type = "HTML")]
public string Content { get; set; }
}
So, you are expecting below JSON serializing output:
{
"Title":{"type":"String","displayname":"Blog Title","value":"Attribute To JSON"},
"Content":{"type":"HTML","displayname":"Content",
"value":"<p>This blog is still not implemented</p>"}
}
Custom JsonConverter
JsonConverter
is an abstract
class provides with JSON.NET that allows you to convert an object to and from JSON. By inheriting that, you can customize default serialization and deserialization behavior as you want.
Reading Attributes With Reflection
Attributes and reflection go hand in hand. So, when you override WriteJson
method to create your custom serialization, you can use the reflection to read the attributes value. When it desterilize back to the object, we can use reflection same way to set the property value.
public class ComplexTypeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (typeof(iComplexType).IsAssignableFrom(objectType));
}
public override object ReadJson
(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
object rootObject = Activator.CreateInstance(objectType);
JToken objJSON = JToken.ReadFrom(reader);
foreach (var token in objJSON)
{
PropertyInfo propInfo = rootObject.GetType().GetProperty
(token.Path, BindingFlags.IgnoreCase | BindingFlags.Public |
BindingFlags.Instance);
if (propInfo.CanWrite)
{
var tk = token as JProperty;
if (tk.Value is JObject)
{
JValue val = tk.Value.SelectToken("value") as JValue;
propInfo.SetValue(rootObject, Convert.ChangeType
(val.Value, propInfo.PropertyType.UnderlyingSystemType), null);
}
else
{
propInfo.SetValue(rootObject, Convert.ChangeType
(tk.Value, propInfo.PropertyType.UnderlyingSystemType), null);
}
}
}
return rootObject;
}
public override void WriteJson
(JsonWriter writer, object value, JsonSerializer serializer)
{
var jo = new JObject();
var type = value.GetType();
foreach (PropertyInfo propInfo in type.GetProperties())
{
if (propInfo.CanRead)
{
object propVal = propInfo.GetValue(value, null);
var cutomAttribute = propInfo.GetCustomAttribute<ComplexAttribute>();
if (cutomAttribute != null)
{
jo.Add(propInfo.Name, JToken.FromObject(new
{ type = cutomAttribute.Type,
displayname = cutomAttribute.DisplayName ?? propInfo.Name,
value = propVal ?? string.Empty }, serializer));
}
else
{
jo.Add(propInfo.Name, JToken.FromObject
(propVal ?? string.Empty, serializer));
}
}
}
jo.WriteTo(writer);
}
}
Try yourself with dot net fiddle:
Filed under: C#, CodeProject, JSON
Tagged: C#, deserialization, JSON, jsonconvert, reflection, serialization