Esempio n. 1
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (objectType is null)
            {
                throw new ArgumentNullException(nameof(objectType));
            }

            if (serializer is null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            var dynamicConstructor = FastActivator.GetDynamicConstructor <IEnumerable <object> >(objectType);

            if (reader.TokenType == JsonToken.StartArray)
            {
                var items = new List <object>();

                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.EndArray)
                    {
                        break;
                    }

                    if (reader.TokenType == JsonToken.Null)
                    {
                        continue;
                    }

                    var item = ProcessToken(reader, objectType.GenericTypeArguments, serializer);
                    items.Add(item);
                }

                return(dynamicConstructor(items));
            }
            else if (reader.TokenType != JsonToken.Null)
            {
                var item = ProcessToken(reader, objectType.GenericTypeArguments, serializer);
                return(dynamicConstructor(new[] { item }));
            }

            return(default);
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="Utf8JsonReader"/> to read from.</param>
        /// <param name="typeToConvert">Type of the object.</param>
        /// <param name="options">The serializer options.</param>
        /// <returns>The object value.</returns>
        public override IValues Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (typeToConvert is null)
            {
                throw new ArgumentNullException(nameof(typeToConvert));
            }

            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var dynamicConstructor = FastActivator.GetDynamicConstructor <IEnumerable <object> >(typeToConvert);

            if (reader.TokenType == JsonTokenType.StartArray)
            {
                var items = new List <object>();

                while (reader.Read())
                {
                    if (reader.TokenType == JsonTokenType.EndArray)
                    {
                        break;
                    }

                    if (reader.TokenType == JsonTokenType.Null)
                    {
                        continue;
                    }

                    var item = ProcessToken(ref reader, typeToConvert.GenericTypeArguments, options);
                    items.Add(item);
                }

                return((IValues)dynamicConstructor(items));
            }
            else if (reader.TokenType != JsonTokenType.Null)
            {
                var item = ProcessToken(ref reader, typeToConvert.GenericTypeArguments, options);
                return((IValues)dynamicConstructor(new[] { item }));
            }

            return(default);