示例#1
0
        /// <summary>
        /// Creates serialization metadata for a collection.
        /// </summary>
        public JsonTypeInfoInternal(
            JsonSerializerOptions options,
            JsonCollectionInfoValues <T> collectionInfo,
            Func <JsonConverter <T> > converterCreator,
            object?createObjectWithArgs = null,
            object?addFunc = null)
            : base(typeof(T), options)
        {
            if (collectionInfo == null)
            {
                throw new ArgumentNullException(nameof(collectionInfo));
            }

            ConverterStrategy strategy  = collectionInfo.KeyInfo == null ? ConverterStrategy.Enumerable : ConverterStrategy.Dictionary;
            JsonConverter <T> converter = new JsonMetadataServicesConverter <T>(converterCreator, strategy);

            KeyType                 = converter.KeyType;
            ElementType             = converter.ElementType;
            KeyTypeInfo             = collectionInfo.KeyInfo;
            ElementTypeInfo         = collectionInfo.ElementInfo ?? throw new ArgumentNullException(nameof(collectionInfo.ElementInfo));
            NumberHandling          = collectionInfo.NumberHandling;
            PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, options);
            SerializeHandler        = collectionInfo.SerializeHandler;
            CreateObjectWithArgs    = createObjectWithArgs;
            AddMethodDelegate       = addFunc;
            SetCreateObjectFunc(collectionInfo.ObjectCreator);
        }
示例#2
0
        /// <summary>
        /// Creates serialization metadata for an object.
        /// </summary>
        public JsonTypeInfoInternal(JsonSerializerOptions options, JsonObjectInfoValues <T> objectInfo) : base(typeof(T), options)
        {
#pragma warning disable CS8714
            // The type cannot be used as type parameter in the generic type or method.
            // Nullability of type argument doesn't match 'notnull' constraint.
            JsonConverter converter;

            if (objectInfo.ObjectWithParameterizedConstructorCreator != null)
            {
                converter = new JsonMetadataServicesConverter <T>(
                    () => new LargeObjectWithParameterizedConstructorConverter <T>(),
                    ConverterStrategy.Object);
                CreateObjectWithArgs = objectInfo.ObjectWithParameterizedConstructorCreator;
                CtorParamInitFunc    = objectInfo.ConstructorParameterMetadataInitializer;
            }
            else
            {
                converter = new JsonMetadataServicesConverter <T>(() => new ObjectDefaultConverter <T>(), ConverterStrategy.Object);
                SetCreateObjectFunc(objectInfo.ObjectCreator);
            }
#pragma warning restore CS8714

            PropInitFunc            = objectInfo.PropertyMetadataInitializer;
            SerializeHandler        = objectInfo.SerializeHandler;
            PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, Options);
            NumberHandling          = objectInfo.NumberHandling;
        }
示例#3
0
        /// <summary>
        /// Creates serialization metadata for an object.
        /// </summary>
        public JsonTypeInfoInternal(
            JsonSerializerOptions options,
            Func <T>?createObjectFunc,
            Func <JsonSerializerContext, JsonPropertyInfo[]>?propInitFunc,
            JsonNumberHandling numberHandling,
            Action <Utf8JsonWriter, T>?serializeFunc
            ) : base(typeof(T), options, ConverterStrategy.Object)
        {
            if (propInitFunc == null && serializeFunc == null)
            {
                ThrowHelper.ThrowInvalidOperationException_PropInitAndSerializeFuncsNull();
            }

#pragma warning disable CS8714
            // The type cannot be used as type parameter in the generic type or method.
            // Nullability of type argument doesn't match 'notnull' constraint.
            JsonConverter converter = new JsonMetadataServicesConverter <T>(() => new ObjectDefaultConverter <T>(), ConverterStrategy.Object, keyType: null, elementType: null);
#pragma warning restore CS8714

            PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, Options);
            NumberHandling          = numberHandling;
            PropInitFunc            = propInitFunc;
            Serialize = serializeFunc;
            SetCreateObjectFunc(createObjectFunc);
        }
示例#4
0
        /// <summary>
        /// Creates serialization metadata for a <see cref="ConverterStrategy.Dictionary"/>.
        /// </summary>
        public JsonTypeInfoInternal(
            JsonSerializerOptions options,
            Func <T>?createObjectFunc,
            Func <JsonConverter <T> > converterCreator,
            JsonTypeInfo?keyInfo,
            JsonTypeInfo?valueInfo,
            JsonNumberHandling numberHandling,
            Action <Utf8JsonWriter, T>?serializeFunc,
            Type keyType,
            Type elementType,
            object?createObjectWithArgs = null)
            : base(typeof(T), options, ConverterStrategy.Dictionary)
        {
            JsonConverter <T> converter = new JsonMetadataServicesConverter <T>(converterCreator, ConverterStrategy.Dictionary, keyType, elementType);

            KeyType                 = converter.KeyType;
            ElementType             = converter.ElementType;
            KeyTypeInfo             = keyInfo ?? throw new ArgumentNullException(nameof(keyInfo));
            ElementType             = converter.ElementType;
            ElementTypeInfo         = valueInfo ?? throw new ArgumentNullException(nameof(valueInfo));
            NumberHandling          = numberHandling;
            PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, options);
            Serialize               = serializeFunc;
            CreateObjectWithArgs    = createObjectWithArgs;
            SetCreateObjectFunc(createObjectFunc);
        }
        /// <summary>
        /// Creates serialization metadata for a <see cref="ConverterStrategy.Enumerable"/>.
        /// </summary>
        public JsonTypeInfoInternal(
            JsonSerializerOptions options,
            Func <T>?createObjectFunc,
            Func <JsonConverter <T> > converterCreator,
            JsonTypeInfo?elementInfo,
            JsonNumberHandling numberHandling,
            Action <Utf8JsonWriter, T>?serializeFunc,
            Type elementType) : base(typeof(T), options, ConverterStrategy.Enumerable)
        {
            JsonConverter <T> converter = new JsonMetadataServicesConverter <T>(converterCreator, ConverterStrategy.Enumerable, keyType: null, elementType);

            ElementType             = converter.ElementType;
            ElementTypeInfo         = elementInfo ?? throw new ArgumentNullException(nameof(elementInfo));
            NumberHandling          = numberHandling;
            PropertyInfoForTypeInfo = JsonMetadataServices.CreateJsonPropertyInfoForClassInfo(typeof(T), this, converter, options);
            Serialize = serializeFunc;
            SetCreateObjectFunc(createObjectFunc);
        }