private bool ShouldSetMappingValue(JsonMemberMapping memberMapping, object value) { if (_nullValueHandling == NullValueHandling.Ignore && value == null) return false; if (_defaultValueHandling == DefaultValueHandling.Ignore && Equals(value, memberMapping.DefaultValue)) return false; if (!memberMapping.Writable) return false; return true; }
private void WriteMemberInfoProperty(JsonWriter writer, object value, JsonMemberMapping memberMapping) { MemberInfo member = memberMapping.Member; string propertyName = memberMapping.MappingName; JsonConverter memberConverter = memberMapping.MemberConverter; object defaultValue = memberMapping.DefaultValue; if (!ReflectionUtils.IsIndexedProperty(member)) { object memberValue = ReflectionUtils.GetMemberValue(member, value); if (_nullValueHandling == NullValueHandling.Ignore && memberValue == null) return; if (_defaultValueHandling == DefaultValueHandling.Ignore && object.Equals(memberValue, defaultValue)) return; if (writer.SerializeStack.IndexOf(memberValue) != -1) { switch (_referenceLoopHandling) { case ReferenceLoopHandling.Error: throw new JsonSerializationException("Self referencing loop"); case ReferenceLoopHandling.Ignore: // return from method return; case ReferenceLoopHandling.Serialize: // continue break; default: throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(CultureInfo.InvariantCulture, _referenceLoopHandling)); } } writer.WritePropertyName(propertyName ?? member.Name); SerializeValue(writer, memberValue, memberConverter); } }
private void SetMappingValue(JsonMemberMapping memberMapping, JsonReader reader, object target) { if (memberMapping.Ignored) { reader.Skip(); return; } // get the member's underlying type Type memberType = ReflectionUtils.GetMemberUnderlyingType(memberMapping.Member); object currentValue = null; bool useExistingValue = false; if ((_objectCreationHandling == ObjectCreationHandling.Auto || _objectCreationHandling == ObjectCreationHandling.Reuse) && (reader.TokenType == JsonToken.StartArray || reader.TokenType == JsonToken.StartObject)) { currentValue = ReflectionUtils.GetMemberValue(memberMapping.Member, target); useExistingValue = (currentValue != null && !memberType.IsArray && !ReflectionUtils.InheritsGenericDefinition(memberType, typeof(ReadOnlyCollection<>))); } if (!memberMapping.Writable && !useExistingValue) { reader.Skip(); return; } object value = CreateObject(reader, memberType, (useExistingValue) ? currentValue : null, JsonTypeReflector.GetConverter(memberMapping.Member, memberType)); if (!useExistingValue && ShouldSetMappingValue(memberMapping, value)) ReflectionUtils.SetMemberValue(memberMapping.Member, target, value); }