/// <summary> /// Gets the serializable members for the type. /// </summary> /// <param name="objectType">The type to get serializable members for.</param> /// <returns>The serializable members for the type.</returns> protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { #if !PocketPC DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); #endif List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags); List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); List <MemberInfo> serializableMembers = new List <MemberInfo>(); foreach (MemberInfo member in allMembers) { if (defaultMembers.Contains(member)) { serializableMembers.Add(member); } else { if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null) { serializableMembers.Add(member); } #if !PocketPC else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member) != null) { serializableMembers.Add(member); } #endif } } return(serializableMembers); }
/// <summary> /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>. /// </summary> /// <param name="contract">The member's declaring types <see cref="JsonObjectContract"/>.</param> /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param> /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns> protected virtual JsonProperty CreateProperty(JsonObjectContract contract, MemberInfo member) { JsonProperty property = new JsonProperty(); property.Member = member; #if !PocketPC DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null) { dataMemberAttribute = JsonTypeReflector.GetAttribute <DataMemberAttribute>(member); } else { dataMemberAttribute = null; } #endif JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member); bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(member) != null); string mappedName; if (propertyAttribute != null && propertyAttribute.PropertyName != null) { mappedName = propertyAttribute.PropertyName; } #if !PocketPC else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { mappedName = dataMemberAttribute.Name; } #endif else { mappedName = member.Name; } property.PropertyName = ResolvePropertyName(mappedName); if (propertyAttribute != null) { property.Required = propertyAttribute.IsRequired; } #if !PocketPC else if (dataMemberAttribute != null) { property.Required = dataMemberAttribute.IsRequired; } #endif else { property.Required = false; } property.Ignored = (hasIgnoreAttribute || (contract.MemberSerialization == MemberSerialization.OptIn && propertyAttribute == null #if !PocketPC && dataMemberAttribute == null #endif )); property.Readable = ReflectionUtils.CanReadMemberValue(member); property.Writable = ReflectionUtils.CanSetMemberValue(member); property.MemberConverter = JsonTypeReflector.GetConverter(member, ReflectionUtils.GetMemberUnderlyingType(member)); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(member); property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null; property.NullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null; property.DefaultValueHandling = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null; property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null; property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null; return(property); }