public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { JObject inner = GenerateJObjectTreeFromObjectProperties(value); LevelUpSerializableModel attribute = GetSerializableModelAttribute(value.GetType()); if (null == attribute || string.IsNullOrEmpty(attribute.RootTag)) { inner.WriteTo(writer); } else { // Insert a wrapping container JObject since the object's class specifies the LevelUpSerializableModel attribute JObject outer = new JObject(); outer.AddFirst(new JProperty(attribute.RootTag, inner)); outer.WriteTo(writer); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { reader.DateParseHandling = DateParseHandling.None; JObject jsonObject = JObject.Load(reader); // Strip the container JObject if the object's class specifies the LevelUpSerializableModel attribute LevelUpSerializableModel attribute = GetSerializableModelAttribute(objectType); if (null != attribute && !string.IsNullOrEmpty(attribute.RootTag)) { JProperty prop = jsonObject.Property(attribute.RootTag); if (null != prop && prop.Value is JObject) { jsonObject = prop.Value as JObject; } } return(GenerateObjectFromJObjectWithReflection(jsonObject, objectType)); }