protected virtual KeyValuePair <string, object> GetSerializedProperty(object obj, PropertyInfo property) { var result = new KeyValuePair <string, object>(); if (property == null || !property.CanRead) { return(result); } object value = property.GetValue(obj); JsonClassAttribute jsonClassAttribute = GetJsonClassAttribute(obj); bool ignoreNull = true; if (jsonClassAttribute != null) { ignoreNull = !jsonClassAttribute.SerializeNullValues; } if (value == null && ignoreNull) { return(result); } JsonNameAttribute jsonPropertyAttribute = GetJsonPropertyAttribute(property); if (jsonPropertyAttribute == null || jsonPropertyAttribute.Ignored) { return(result); } bool ignoreDefault = true; if (jsonClassAttribute != null) { ignoreDefault = !jsonClassAttribute.SerializeDefaultValues; } if (IsNullOrDefault(value) && ignoreDefault) { return(result); } if (property.PropertyType == typeof(DateTime)) { value = ((DateTime)value).ToString("G", DateTimeFormatInfo.InvariantInfo); } string name = jsonPropertyAttribute.PropertyName; return(new KeyValuePair <string, object>(name, value)); }
/// <summary> /// Get the Class Attribute e.g. [JsonClass(SerializeNullValues=true)] /// </summary> /// <param name="property"></param> /// <returns></returns> protected JsonClassAttribute GetJsonClassAttribute(object obj) { JsonClassAttribute jsonClassAttribute = null; if (obj == null) { return(jsonClassAttribute); } object[] attributes = obj.GetType().GetCustomAttributes(true); foreach (object attribute in attributes) { if (attribute is JsonClassAttribute) { jsonClassAttribute = (JsonClassAttribute)attribute; } } return(jsonClassAttribute); }