protected virtual JsonMemberMapping CreateMemberMapping(MemberSerialization memberSerialization, MemberInfo member) { JsonPropertyAttribute propertyAttribute = ReflectionUtils.GetAttribute <JsonPropertyAttribute>(member, true); bool hasIgnoreAttribute = member.IsDefined(typeof(JsonIgnoreAttribute), true); string mappedName = (propertyAttribute != null && propertyAttribute.PropertyName != null) ? propertyAttribute.PropertyName : member.Name; string resolvedMappedName = ResolveMappingName(mappedName); bool required = (propertyAttribute != null) ? propertyAttribute.IsRequired : false; bool ignored = (hasIgnoreAttribute || (memberSerialization == MemberSerialization.OptIn && propertyAttribute == null)); bool readable = ReflectionUtils.CanReadMemberValue(member); bool writable = ReflectionUtils.CanSetMemberValue(member); JsonConverter memberConverter = JsonTypeReflector.GetConverter(member, ReflectionUtils.GetMemberUnderlyingType(member)); DefaultValueAttribute defaultValueAttribute = ReflectionUtils.GetAttribute <DefaultValueAttribute>(member, true); object defaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null; return(new JsonMemberMapping(resolvedMappedName, member, ignored, readable, writable, memberConverter, defaultValue, required)); }
/// <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 && !SILVERLIGHT && !MONO 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 && !SILVERLIGHT && !MONO 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 && !SILVERLIGHT && !MONO else if (dataMemberAttribute != null) { property.Required = dataMemberAttribute.IsRequired; } #endif else { property.Required = false; } property.Ignored = (hasIgnoreAttribute || (contract.MemberSerialization == MemberSerialization.OptIn && propertyAttribute == null #if !PocketPC && !SILVERLIGHT && !MONO && 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); }
private void SetPropertyValue(JsonProperty property, JsonReader reader, object target) { if (property.Ignored) { reader.Skip(); return; } // get the member's underlying type Type memberType = ReflectionUtils.GetMemberUnderlyingType(property.Member); object currentValue = null; bool useExistingValue = false; if ((Serializer.ObjectCreationHandling == ObjectCreationHandling.Auto || Serializer.ObjectCreationHandling == ObjectCreationHandling.Reuse) && (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject) && property.Readable) { currentValue = ReflectionUtils.GetMemberValue(property.Member, target); useExistingValue = (currentValue != null && !memberType.IsArray && !ReflectionUtils.InheritsGenericDefinition(memberType, typeof(ReadOnlyCollection <>))); } if (!property.Writable && !useExistingValue) { reader.Skip(); return; } object value = CreateValue(reader, memberType, (useExistingValue) ? currentValue : null, JsonTypeReflector.GetConverter(property.Member, memberType)); if (!useExistingValue && ShouldSetPropertyValue(property, value)) { ReflectionUtils.SetMemberValue(property.Member, target, value); } }