internal ReflectionJsonTypeInfo(JsonConverter converter, JsonSerializerOptions options)
            : base(converter, options)
        {
            NumberHandling = GetNumberHandlingForType(Type);
            PopulatePolymorphismMetadata();
            MapInterfaceTypesToCallbacks();

            if (PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object)
            {
                AddPropertiesAndParametersUsingReflection();
            }

            Func <object>?createObject = Options.MemberAccessorStrategy.CreateConstructor(typeof(T));

            if (converter.UsesDefaultConstructor)
            {
                SetCreateObject(createObject);
            }

            CreateObjectForExtensionDataProperty = createObject;

            // Plug in any converter configuration -- should be run last.
            converter.ConfigureJsonTypeInfo(this, options);
            converter.ConfigureJsonTypeInfoUsingReflection(this, options);
        }
Exemple #2
0
        internal virtual void Configure()
        {
            Debug.Assert(Monitor.IsEntered(_configureLock), "Configure called directly, use EnsureConfigured which locks this method");
            JsonConverter converter = PropertyInfoForTypeInfo.ConverterBase;

            Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == PropertyInfoForTypeInfo.ConverterBase.ConverterStrategy,
                         $"ConverterStrategy from PropertyInfoForTypeInfo.ConverterStrategy ({PropertyInfoForTypeInfo.ConverterStrategy}) does not match converter's ({PropertyInfoForTypeInfo.ConverterBase.ConverterStrategy})");

            converter.ConfigureJsonTypeInfo(this, Options);
            PropertyInfoForTypeInfo.DeclaringTypeNumberHandling = NumberHandling;
            PropertyInfoForTypeInfo.EnsureConfigured();

            // Source gen currently when initializes properties
            // also assigns JsonPropertyInfo's JsonTypeInfo which causes SO if there are any
            // cycles in the object graph. For that reason properties cannot be added immediately.
            // This is a no-op for ReflectionJsonTypeInfo
            LateAddProperties();

            DataExtensionProperty?.EnsureConfigured();

            if (converter.ConverterStrategy == ConverterStrategy.Object && PropertyCache != null)
            {
                foreach (var jsonPropertyInfoKv in PropertyCache.List)
                {
                    JsonPropertyInfo jsonPropertyInfo = jsonPropertyInfoKv.Value !;
                    jsonPropertyInfo.DeclaringTypeNumberHandling = NumberHandling;
                    jsonPropertyInfo.EnsureConfigured();
                }

                if (converter.ConstructorIsParameterized)
                {
                    InitializeConstructorParameters(GetParameterInfoValues(), sourceGenMode: Options.JsonSerializerContext != null);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates serialization metadata for a type using a simple converter.
        /// </summary>
        public SourceGenJsonTypeInfo(JsonConverter converter, JsonSerializerOptions options)
            : base(converter, options)
        {
            PopulatePolymorphismMetadata();
            MapInterfaceTypesToCallbacks();

            // Plug in any converter configuration -- should be run last.
            converter.ConfigureJsonTypeInfo(this, options);
        }