/// <summary> /// Determines if the property or field should not be serialized. /// </summary> /// <param name="objType"></param> /// <param name="member"></param> /// <param name="value"></param> /// <returns></returns> /// <remarks> /// Checks these in order, if any returns true then this is true: /// - is flagged with the JsonIgnoreAttribute property /// - has a JsonSpecifiedProperty which returns false /// </remarks> private bool IsIgnored(Type objType, MemberInfo member, object obj) { if (JsonIgnoreAttribute.IsJsonIgnore(member)) { return(true); } if (this.UseXmlSerializationAttributes) { if (JsonIgnoreAttribute.IsXmlIgnore(member)) { return(true); } PropertyInfo specProp = objType.GetProperty(member.Name + "Specified"); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } return(false); }
internal Dictionary <string, MemberInfo> CreateFieldMemberMap(Type objectType) { if (this.MemberMapCache.ContainsKey(objectType)) { return(this.MemberMapCache[objectType]); } Dictionary <string, MemberInfo> dictionary = new Dictionary <string, MemberInfo>(); foreach (FieldInfo info2 in objectType.GetFields()) { if (info2.IsPublic && !JsonIgnoreAttribute.IsJsonIgnore(info2)) { string str2 = JsonNameAttribute.GetJsonName(info2); if (string.IsNullOrEmpty(str2)) { dictionary[info2.Name] = info2; } else { dictionary[str2] = info2; } } } this.MemberMapCache[objectType] = dictionary; return(dictionary); }
/// <summary> /// Determines if the property or field should not be serialized. /// </summary> /// <param name="objType"></param> /// <param name="member"></param> /// <param name="value"></param> /// <returns></returns> /// <remarks> /// Checks these in order, if any returns true then this is true: /// - is flagged with the JsonIgnoreAttribute property /// - has a JsonSpecifiedProperty which returns false /// </remarks> internal bool IsIgnored(Type objType, MemberInfo member, object obj) { if (JsonIgnoreAttribute.IsJsonIgnore(member)) { return(true); } // Cutting support for this // -- Aron /*string specifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member); * if (!String.IsNullOrEmpty(specifiedProperty)) * { * PropertyInfo specProp = objType.GetProperty(specifiedProperty); * if (specProp != null) * { * object isSpecified = specProp.GetValue(obj, null); * if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) * { * return true; * } * } * }*/ //If the class is specified as opt-in serialization only, members must have the JsonMember attribute if (objType.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0) { if (member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0) { return(true); } } if (UseXmlSerializationAttributes) { if (JsonIgnoreAttribute.IsXmlIgnore(member)) { return(true); } /* * throw new System.Exception ("No longer supporting XML Serialization Attributes"); * * PropertyInfo specProp = objType.GetProperty(member.Name+"Specified"); * if (specProp != null) * { * object isSpecified = specProp.GetValue(obj, null); * if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) * { * return true; * } * }*/ } return(false); }
/// <summary> /// Determines if the property or field should not be serialized. /// </summary> /// <param name="objType"></param> /// <param name="member"></param> /// <param name="value"></param> /// <returns></returns> /// <remarks> /// Checks these in order, if any returns true then this is true: /// - is flagged with the JsonIgnoreAttribute property /// - has a JsonSpecifiedProperty which returns false /// </remarks> private bool IsIgnored(Type objType, MemberInfo member, object obj) { if (JsonIgnoreAttribute.IsJsonIgnore(member)) { return(true); } string specifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member); if (!String.IsNullOrEmpty(specifiedProperty)) { PropertyInfo specProp = objType.GetProperty(specifiedProperty); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } //If the class is specified as opt-in serialization only, members must have the JsonMember attribute if (objType.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0) { if (member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0) { return(true); } } if (this.settings.UseXmlSerializationAttributes) { if (JsonIgnoreAttribute.IsXmlIgnore(member)) { return(true); } PropertyInfo specProp = objType.GetProperty(member.Name + "Specified"); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } return(false); }
protected Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { if (this.MemberMapCache.ContainsKey(objectType)) { return(this.MemberMapCache[objectType]); } Dictionary <string, MemberInfo> dictionary = new Dictionary <string, MemberInfo>(); PropertyInfo[] properties = objectType.GetProperties(); foreach (PropertyInfo propertyInfo in properties) { if (propertyInfo.CanRead && propertyInfo.CanWrite) { if (!JsonIgnoreAttribute.IsJsonIgnore(propertyInfo)) { string jsonName = JsonNameAttribute.GetJsonName(propertyInfo); if (string.IsNullOrEmpty(jsonName)) { dictionary[propertyInfo.Name] = propertyInfo; } else { dictionary[jsonName] = propertyInfo; } } } } FieldInfo[] fields = objectType.GetFields(); foreach (FieldInfo fieldInfo in fields) { if (fieldInfo.IsPublic) { if (!JsonIgnoreAttribute.IsJsonIgnore(fieldInfo)) { string jsonName2 = JsonNameAttribute.GetJsonName(fieldInfo); if (string.IsNullOrEmpty(jsonName2)) { dictionary[fieldInfo.Name] = fieldInfo; } else { dictionary[jsonName2] = fieldInfo; } } } } this.MemberMapCache[objectType] = dictionary; return(dictionary); }
/// <summary> /// Determines if the property or field should not be serialized. /// </summary> /// <param name="objType"></param> /// <param name="member"></param> /// <param name="value"></param> /// <returns></returns> /// <remarks> /// Checks these in order, if any returns true then this is true: /// - is flagged with the JsonIgnoreAttribute property /// - has a JsonSpecifiedProperty which returns false /// </remarks> private bool IsIgnored(Type objType, MemberInfo member, object obj) { if (!member.GetType().IsSerializable || member.IsDefined(typeof(NonSerializedAttribute), true)) { return(true); } if (JsonIgnoreAttribute.IsJsonIgnore(member)) { return(true); } string specifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member); if (!String.IsNullOrEmpty(specifiedProperty)) { PropertyInfo specProp = objType.GetProperty(specifiedProperty); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } if (this.settings.UseXmlSerializationAttributes) { if (JsonIgnoreAttribute.IsXmlIgnore(member)) { return(true); } PropertyInfo specProp = objType.GetProperty(member.Name + "Specified"); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } return(false); }
private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { if (this.MemberMapCache.ContainsKey(objectType)) { return(this.MemberMapCache[objectType]); } Dictionary <string, MemberInfo> dictionary = new Dictionary <string, MemberInfo>(); foreach (PropertyInfo info in objectType.GetProperties()) { if ((info.CanRead && info.CanWrite) && !JsonIgnoreAttribute.IsJsonIgnore(info)) { string jsonName = JsonNameAttribute.GetJsonName(info); if (string.IsNullOrEmpty(jsonName)) { dictionary[info.Name] = info; } else { dictionary[jsonName] = info; } } } foreach (FieldInfo info2 in objectType.GetFields()) { if (info2.IsPublic && !JsonIgnoreAttribute.IsJsonIgnore(info2)) { string str2 = JsonNameAttribute.GetJsonName(info2); if (string.IsNullOrEmpty(str2)) { dictionary[info2.Name] = info2; } else { dictionary[str2] = info2; } } } this.MemberMapCache[objectType] = dictionary; return(dictionary); }
private bool IsIgnored(Type objType, MemberInfo member, object obj) { if (JsonIgnoreAttribute.IsJsonIgnore(member)) { return(true); } string jsonSpecifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member); if (!string.IsNullOrEmpty(jsonSpecifiedProperty)) { PropertyInfo property = objType.GetProperty(jsonSpecifiedProperty); if (property != null) { object value = property.GetValue(obj, null); if (value is bool && !Convert.ToBoolean(value)) { return(true); } } } if (settings.UseXmlSerializationAttributes) { if (JsonIgnoreAttribute.IsXmlIgnore(member)) { return(true); } PropertyInfo property2 = objType.GetProperty(member.Name + "Specified"); if (property2 != null) { object value2 = property2.GetValue(obj, null); if (value2 is bool && !Convert.ToBoolean(value2)) { return(true); } } } return(false); }
internal Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { if (this.MemberMapCache.ContainsKey(objectType)) { // map was stored in cache return(this.MemberMapCache[objectType]); } // create a new map Dictionary <string, MemberInfo> memberMap = new Dictionary <string, MemberInfo>(); // load properties into property map PropertyInfo[] properties = objectType.GetProperties(); foreach (PropertyInfo info in properties) { if (!info.CanRead || !info.CanWrite) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map FieldInfo[] fields = objectType.GetFields(); foreach (FieldInfo info in fields) { if (!info.IsPublic) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // store in cache for repeated usage this.MemberMapCache[objectType] = memberMap; return(memberMap); }
private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { if (this.MemberMapCache.ContainsKey(objectType)) { // map was stored in cache return(this.MemberMapCache[objectType]); } // create a new map Dictionary <string, MemberInfo> memberMap = new Dictionary <string, MemberInfo>(); // load properties into property map //PropertyInfo[] properties = objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); IList <PropertyInfo> properties = GetAllProperties(objectType); foreach (PropertyInfo info in properties) { if (!info.CanRead || !info.CanWrite) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { string name = NameResolver.CSharpToJson(info.Name); memberMap[name] = info; // memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map // FieldInfo[] fields = objectType.GetFields(); // FieldInfo[] fields = objectType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public ); IList <FieldInfo> fields = GetAllFields(objectType); foreach (FieldInfo info in fields) { // if (!info.IsPublic) // { // continue; // } if (info.IsPrivate) { int breakhere = 0; ++breakhere; //continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { string name = NameResolver.CSharpToJson(info.Name); memberMap[name] = info; // memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // store in cache for repeated usage this.MemberMapCache[objectType] = memberMap; return(memberMap); }
/// <summary> /// Determines if the property or field should not be serialized. /// </summary> /// <param name="objType"></param> /// <param name="member"></param> /// <param name="value"></param> /// <returns></returns> /// <remarks> /// Checks these in order, if any returns true then this is true: /// - is flagged with the JsonIgnoreAttribute property /// - has a JsonSpecifiedProperty which returns false /// </remarks> private bool IsIgnored(Type objType, MemberInfo member, object obj, bool isPublic) { if (JsonIgnoreAttribute.IsJsonIgnore(member)) { return(true); } string specifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member); if (!String.IsNullOrEmpty(specifiedProperty)) { PropertyInfo specProp = objType.GetProperty(specifiedProperty); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } // If the class is specified as opt-in serialization only, members must have the JsonMember or JsonName // attribute if (objType.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0) { if (member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0) { return(true); } } // Private variables only with JsonMember attribute will be serialized if (!isPublic && !settings.SearchPrivate && member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0) { return(true); } if (this.settings.UseXmlSerializationAttributes) { if (JsonIgnoreAttribute.IsXmlIgnore(member)) { return(true); } PropertyInfo specProp = objType.GetProperty(member.Name + "Specified"); if (specProp != null) { object isSpecified = specProp.GetValue(obj, null); if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified)) { return(true); } } } // Ignore the auto-implemented properties if (member.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Length != 0) { return(true); } return(false); }
/** Creates a member map for the type */ private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { Dictionary <string, MemberInfo> memberMap; if (this.MemberMapCache.TryGetValue(objectType, out memberMap)) { // map was stored in cache return(memberMap); } // create a new map memberMap = new Dictionary <string, MemberInfo>(); // load properties into property map PropertyInfo[] properties = objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in properties) { if (!info.CanRead || !info.CanWrite) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map FieldInfo[] fields = objectType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo info in fields) { if (!info.IsPublic && info.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // store in cache for repeated usage this.MemberMapCache[objectType] = memberMap; return(memberMap); }
/** Creates a member map for the type */ private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType) { Dictionary <string, MemberInfo> memberMap; if (this.MemberMapCache.TryGetValue(objectType, out memberMap)) { // map was stored in cache return(memberMap); } // create a new map memberMap = new Dictionary <string, MemberInfo>(); // load properties into property map Type tp = objectType; while (tp != null) { PropertyInfo[] properties = TCU.GetTypeInfo(tp).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < properties.Length; i++) { PropertyInfo info = properties [i]; if (!info.CanRead || !info.CanWrite) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } // load public fields into property map FieldInfo[] fields = TCU.GetTypeInfo(tp).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo info in fields) { if (!info.IsPublic && #if WINDOWS_STORE info.GetCustomAttribute <JsonMemberAttribute>(false) == null #else info.GetCustomAttributes(typeof(JsonMemberAttribute), false).Length == 0 #endif ) { continue; } if (JsonIgnoreAttribute.IsJsonIgnore(info)) { continue; } string jsonName = JsonNameAttribute.GetJsonName(info); if (String.IsNullOrEmpty(jsonName)) { memberMap[info.Name] = info; } else { memberMap[jsonName] = info; } } tp = tp.BaseType; } // store in cache for repeated usage this.MemberMapCache[objectType] = memberMap; return(memberMap); }