public override void Read(JsonTokenType tokenType, JsonSerializerOptions options, ref ReadStack state, ref Utf8JsonReader reader) { Debug.Assert(ShouldDeserialize); if (ElementClassInfo != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.ReadEnumerable(tokenType, options, ref state, ref reader); } else { if (ValueConverter != null && ValueConverter.TryRead(RuntimePropertyType, ref reader, out TRuntimeProperty value)) { if (state.Current.ReturnValue == null) { state.Current.ReturnValue = value; } else { // Null values were already handled. Debug.Assert(value != null); Set(state.Current.ReturnValue, value); } return; } ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.PropertyPath); } }
internal override void Read(JsonTokenType tokenType, JsonSerializerOptions options, ref ReadStack state, ref Utf8JsonReader reader) { if (ElementClassInfo != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.ReadEnumerable(tokenType, options, ref state, ref reader); } else if (ShouldDeserialize) { if (ValueConverter != null) { if (ValueConverter.TryRead(s_underlyingType, ref reader, out TProperty value)) { if (state.Current.ReturnValue == null) { state.Current.ReturnValue = value; } else { Set((TClass)state.Current.ReturnValue, value); } return; } } ThrowHelper.ThrowJsonReaderException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state); } }
private void SetPropertyInfoForObjectElement() { if (_elementPropertyInfo == null && ElementClassInfo.PolicyProperty == null) { _elementPropertyInfo = ElementClassInfo.CreateRootProperty(Options); } }
public override void Read(JsonTokenType tokenType, JsonSerializerOptions options, ref ReadObjectState current, ref Utf8JsonReader reader) { if (ElementClassInfo != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.ReadEnumerable(tokenType, options, ref current, ref reader); } else if (HasSetter) { if (ValueConverter != null) { Type propertyType = PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = Nullable.GetUnderlyingType(propertyType); } if (!ValueConverter.TryRead(propertyType, ref reader, out TValue value)) { throw new JsonReaderException("todo: unable to read value (propertypath)", 0, 0); } if (value != null || !IgnoreNullPropertyValueOnRead(options)) { SetValueAsObject(current.ReturnValue, value, options); } } else { if (this is IJsonValueConverter <TValue> converter) { if (!converter.TryRead(PropertyType, ref reader, out TValue value)) { throw new JsonReaderException("todo: unable to read value (propertypath)", 0, 0); } if (current.ReturnValue == null) { current.ReturnValue = value; } else { if (value != null || !IgnoreNullPropertyValueOnRead(options)) { Set(current.ReturnValue, value); } } } else { throw new InvalidOperationException($"todo: there is no converter for {PropertyType}"); } } } }
public override void Read(JsonSerializerOptions options, ref ReadObjectState current, ref Utf8JsonReader reader) { if (ElementClassInfo != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.ReadEnumerable(options, ref current, ref reader); } else { if (ValueConverter != null) { Type propertyType = PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = Nullable.GetUnderlyingType(propertyType); } object value = ValueConverter.GetRead(ref reader, propertyType); if (value != null || !SkipNullValuesOnRead(options)) { SetValueAsObject(current.ReturnValue, value, options); } } else { if (this is IJsonSerializerInternal <TValue> converter) { TValue value = converter.Read(ref reader); if (current.ReturnValue == null) { current.ReturnValue = value; } else { if (value != null || !SkipNullValuesOnRead(options)) { Set(current.ReturnValue, value); } } } else { throw new InvalidOperationException($"todo: there is no converter for {PropertyType}"); } } } }
public void Write(ref WriteStack state, Utf8JsonWriter writer) { Debug.Assert(ShouldSerialize); if (state.Current.Enumerator != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.OnWriteEnumerable(ref state.Current, writer); } else { int originalDepth = writer.CurrentDepth; OnWrite(ref state.Current, writer); VerifyWrite(originalDepth, ref state, ref writer); } }
public void Read(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader) { Debug.Assert(ShouldDeserialize); if (ElementClassInfo != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.OnReadEnumerable(tokenType, ref state, ref reader); } else { GetOriginalValues(ref reader, out JsonTokenType originalTokenType, out int originalDepth); OnRead(tokenType, ref state, ref reader); VerifyRead(originalTokenType, originalDepth, ref state, ref reader); } }
public override void Write(JsonSerializerOptions options, ref WriteStackFrame current, Utf8JsonWriter writer) { Debug.Assert(ShouldSerialize); if (current.Enumerator != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.WriteEnumerable(options, ref current, writer); } else { TProperty?value; if (IsPropertyPolicy) { value = (TProperty?)current.CurrentValue; } else { value = Get((TClass)current.CurrentValue); } if (value == null) { Debug.Assert(EscapedName != null); if (!IgnoreNullValues) { writer.WriteNull(EscapedName); } } else if (ValueConverter != null) { if (EscapedName != null) { ValueConverter.Write(EscapedName, value.GetValueOrDefault(), writer); } else { ValueConverter.Write(value.GetValueOrDefault(), writer); } } } }
// todo: have the caller check if current.Enumerator != null and call WriteEnumerable of the underlying property directly to avoid an extra virtual call. internal override void Write(JsonSerializerOptions options, ref WriteStackFrame current, ref Utf8JsonWriter writer) { if (current.Enumerator != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.WriteEnumerable(options, ref current, ref writer); } else if (ShouldSerialize) { TRuntimeProperty value; if (_isPropertyPolicy) { value = (TRuntimeProperty)current.CurrentValue; } else { value = (TRuntimeProperty)Get((TClass)current.CurrentValue); } if (value == null) { if (_escapedName == null) { writer.WriteNullValue(); } else if (!IgnoreNullValues) { writer.WriteNull(_escapedName); } } else if (ValueConverter != null) { if (_escapedName != null) { ValueConverter.Write(_escapedName, value, ref writer); } else { ValueConverter.Write(value, ref writer); } } } }
internal override void Read(JsonTokenType tokenType, JsonSerializerOptions options, ref ReadStack state, ref Utf8JsonReader reader) { if (ElementClassInfo != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.ReadEnumerable(tokenType, options, ref state, ref reader); } else if (HasSetter) { if (ValueConverter != null) { Type propertyType = PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = Nullable.GetUnderlyingType(propertyType); } if (ValueConverter.TryRead(propertyType, ref reader, out TProperty value)) { if (state.Current.ReturnValue == null) { state.Current.ReturnValue = value; } else { if (value != null || !IgnoreNullPropertyValueOnRead(options)) { Set((TClass)state.Current.ReturnValue, value); } } return; } } ThrowHelper.ThrowJsonReaderException_DeserializeUnableToConvertValue(PropertyType, reader, state); } }
// todo: have the caller check if current.Enumerator != null and call WriteEnumerable of the underlying property directly to avoid an extra virtual call. public override void Write(JsonSerializerOptions options, ref WriteObjectState current, ref Utf8JsonWriter writer) { if (current.Enumerator != null) { // Forward the setter to the value-based JsonPropertyInfo. JsonPropertyInfo propertyInfo = ElementClassInfo.GetPolicyProperty(); propertyInfo.WriteEnumerable(options, ref current, ref writer); } else { if (ValueConverter != null) { TValue value = Get(current.CurrentValue); if (value == null) { if (Name == null) { writer.WriteNullValue(); } else if (!SkipNullValuesOnWrite(options)) { writer.WriteNull(Name); } } else { ValueConverter.SetWrite(ref writer, Name, value); } } else { TValue value = Get(current.CurrentValue); if (value == null) { if (Name == null) { writer.WriteNullValue(); } else if (!SkipNullValuesOnWrite(options)) { writer.WriteNull(Name); } } else { if (this is IJsonSerializerInternal <TValue> converter) { if (Name == null) { converter.Write(ref writer, value); } else if (!SkipNullValuesOnWrite(options)) { converter.Write(ref writer, Name, value); } } else { throw new InvalidOperationException($"todo: there is no converter for {PropertyType}"); } } } } }