/// <summary>
        /// Creates a converter for a specified type.
        /// </summary>
        /// <param name="typeToConvert">The type handled by the converter.</param>
        /// <param name="options">The serialization options to use.</param>
        /// <returns>A converter for which T is compatible with typeToConvert.</returns>
        public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
        {
            Type converterType = TupleReflector.GetTupleConverter(typeToConvert);

            #pragma warning disable CS8600, CS8603
            return((JsonConverter)Activator.CreateInstance(converterType));

            #pragma warning restore CS8600, CS8603
        }
Пример #2
0
        public override ValueTuple <T1, T2, T3, T4, T5, T6, T7, TRest> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            T1    t1;
            T2    t2;
            T3    t3;
            T4    t4;
            T5    t5;
            T6    t6;
            T7    t7;
            TRest tRest;

            if (reader.TokenType != JsonTokenType.StartArray)
            {
                throw new JsonException();
            }

            reader.Read();
            t1 = ReadValue <T1>(ref reader, options);

            reader.Read();
            t2 = ReadValue <T2>(ref reader, options);

            reader.Read();
            t3 = ReadValue <T3>(ref reader, options);

            reader.Read();
            t4 = ReadValue <T4>(ref reader, options);

            reader.Read();
            t5 = ReadValue <T5>(ref reader, options);

            reader.Read();
            t6 = ReadValue <T6>(ref reader, options);

            reader.Read();
            t7 = ReadValue <T7>(ref reader, options);

            Type[]   restTypes  = typeof(TRest).GetGenericArguments();
            object[] restValues = new object[restTypes.Length];
            for (var i = 0; i < restTypes.Length; i++)
            {
                reader.Read();
                Type restType = restTypes[i];
                restValues[i] = JsonSerializer.Deserialize(ref reader, restType, options);
            }
            tRest = TupleReflector.GenerateTuple <TRest>(restValues);

            if (!reader.Read() || reader.TokenType != JsonTokenType.EndArray)
            {
                throw new JsonException();
            }

            return(new ValueTuple <T1, T2, T3, T4, T5, T6, T7, TRest>(t1, t2, t3, t4, t5, t6, t7, tRest));
        }