예제 #1
0
        // If this method is changed, also change JsonPropertyInfoNullable.ReadEnumerable and JsonSerializer.ApplyObjectToEnumerable
        public override void ReadEnumerable(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader)
        {
            Debug.Assert(ShouldDeserialize);

            // We need a property in order to a apply a value in a dictionary.
            if (state.Current.KeyName == null && (state.Current.IsProcessingDictionary || state.Current.IsProcessingImmutableDictionary))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            // We need an initialized array in order to store the values.
            if (state.Current.IsProcessingEnumerable && state.Current.TempEnumerableValues == null && state.Current.ReturnValue == null)
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            if (ValueConverter == null || !ValueConverter.TryRead(RuntimePropertyType, ref reader, out TRuntimeProperty value))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            JsonSerializer.ApplyValueToEnumerable(ref value, ref state, ref reader);
        }
예제 #2
0
        // If this method is changed, also change JsonPropertyInfoNullable.ReadEnumerable and JsonSerializer.ApplyObjectToEnumerable
        public override void ReadEnumerable(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader)
        {
            Debug.Assert(ShouldDeserialize);

            if (state.Current.KeyName == null && (state.Current.IsProcessingDictionary || state.Current.IsProcessingIDictionaryConstructibleOrKeyValuePair))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            // We need an initialized array in order to store the values.
            if (state.Current.IsProcessingEnumerable && state.Current.TempEnumerableValues == null && state.Current.ReturnValue == null)
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            if (ValueConverter == null || !ValueConverter.TryRead(RuntimePropertyType, ref reader, out TRuntimeProperty value))
            {
                if (state.Current.IsProcessingKeyValuePair && state.Current.KeyName == "Key")
                {
                    // Handle the special case where the input KeyValuePair is of form {"Key": "MyKey", "Value": 1}
                    // (as opposed to form {"MyKey": 1}) and the value type is not string.
                    // If we have one, the ValueConverter failed to read the current token because it should be of type string
                    // (we only support string keys) but we initially tried to read it as type TRuntimeProperty.
                    // We have TRuntimeProperty not string because for deserialization, we parse the KeyValuePair as a
                    // dictionary before creating a KeyValuePair instance in a converter-like manner with the parsed values.
                    // Because it's dictionary-like parsing, we set the element type of the dictionary earlier on to the KeyValuePair's value
                    // type, which led us here.
                    // If there's no ValueConverter, the runtime type of the KeyValuePair's value is probably an object, dictionary or enumerable.
                    JsonValueConverter <string> stringConverter = DefaultConverters <string> .s_converter;
                    if (!stringConverter.TryRead(typeof(string), ref reader, out string strValue))
                    {
                        ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(typeof(string), reader, state.JsonPath);
                    }

                    object objValue = strValue;

                    JsonSerializer.ApplyValueToEnumerable(ref objValue, ref state, ref reader);
                    return;
                }

                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            if (state.Current.IsProcessingKeyValuePair)
            {
                // The value is being applied to a Dictionary<string, object>, so we need to cast to object here.
                object objValue = value;
                JsonSerializer.ApplyValueToEnumerable(ref objValue, ref state, ref reader);
                return;
            }

            JsonSerializer.ApplyValueToEnumerable(ref value, ref state, ref reader);
        }
예제 #3
0
        protected override void OnReadEnumerable(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader)
        {
            if (Converter == null)
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
            }

            TProperty value         = Converter.Read(ref reader, s_underlyingType, Options);
            TProperty?nullableValue = new TProperty?(value);

            JsonSerializer.ApplyValueToEnumerable(ref nullableValue, ref state, ref reader);
        }
예제 #4
0
        public override void ReadEnumerable(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader)
        {
            Debug.Assert(ShouldDeserialize);

            if (ValueConverter == null || !ValueConverter.TryRead(typeof(TProperty), ref reader, out TProperty value))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            TProperty?nullableValue = new TProperty?(value);

            JsonSerializer.ApplyValueToEnumerable(ref nullableValue, ref state, ref reader);
        }
예제 #5
0
        // If this method is changed, also change JsonPropertyInfoNullable.ReadEnumerable and JsonSerializer.ApplyObjectToEnumerable
        public override void ReadEnumerable(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader)
        {
            Debug.Assert(ShouldDeserialize);

            if (state.Current.KeyName == null && (state.Current.IsProcessingDictionary || state.Current.IsProcessingImmutableDictionary))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            if (ValueConverter == null || !ValueConverter.TryRead(RuntimePropertyType, ref reader, out TRuntimeProperty value))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath);
                return;
            }

            JsonSerializer.ApplyValueToEnumerable(ref value, ref state, ref reader);
        }
예제 #6
0
        protected override void OnReadEnumerable(JsonTokenType tokenType, ref ReadStack state, ref Utf8JsonReader reader)
        {
            if (Converter == null)
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath());
            }

            if (state.Current.KeyName == null && (state.Current.IsProcessingDictionary || state.Current.IsProcessingIDictionaryConstructible))
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath());
                return;
            }

            // We need an initialized array in order to store the values.
            if (state.Current.IsProcessingEnumerable && state.Current.TempEnumerableValues == null && state.Current.ReturnValue == null)
            {
                ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(RuntimePropertyType, reader, state.JsonPath());
                return;
            }

            TConverter value = Converter.Read(ref reader, RuntimePropertyType, Options);

            JsonSerializer.ApplyValueToEnumerable(ref value, ref state, ref reader);
        }