Пример #1
0
        internal void InitializeForSourceGen(JsonSerializerOptions options, JsonPropertyInfoValues <T> propertyInfo)
        {
            Options = options;
            ClrName = propertyInfo.PropertyName;

            // Property name settings.
            if (propertyInfo.JsonPropertyName != null)
            {
                Name = propertyInfo.JsonPropertyName;
            }
            else if (options.PropertyNamingPolicy == null)
            {
                Name = ClrName;
            }
            else
            {
                Name = options.PropertyNamingPolicy.ConvertName(ClrName);
                if (Name == null)
                {
                    ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(DeclaringType, this);
                }
            }

            SrcGen_IsPublic        = propertyInfo.IsPublic;
            SrcGen_HasJsonInclude  = propertyInfo.HasJsonInclude;
            SrcGen_IsExtensionData = propertyInfo.IsExtensionData;
            PropertyType           = typeof(T);

            JsonTypeInfo propertyTypeInfo = propertyInfo.PropertyTypeInfo;
            Type         declaringType    = propertyInfo.DeclaringType;

            JsonConverter?converter = propertyInfo.Converter;

            if (converter == null)
            {
                converter = propertyTypeInfo.PropertyInfoForTypeInfo.ConverterBase as JsonConverter <T>;
                if (converter == null)
                {
                    throw new InvalidOperationException(SR.Format(SR.ConverterForPropertyMustBeValid, declaringType, ClrName, typeof(T)));
                }
            }

            ConverterBase = converter;

            if (propertyInfo.IgnoreCondition == JsonIgnoreCondition.Always)
            {
                IsIgnored = true;
                Debug.Assert(!ShouldSerialize);
                Debug.Assert(!ShouldDeserialize);
            }
            else
            {
                Get               = propertyInfo.Getter !;
                Set               = propertyInfo.Setter;
                HasGetter         = Get != null;
                HasSetter         = Set != null;
                JsonTypeInfo      = propertyTypeInfo;
                DeclaringType     = declaringType;
                IgnoreCondition   = propertyInfo.IgnoreCondition;
                MemberType        = propertyInfo.IsProperty ? MemberTypes.Property : MemberTypes.Field;
                ConverterStrategy = Converter !.ConverterStrategy;
                NumberHandling    = propertyInfo.NumberHandling;
            }
        }
Пример #2
0
        internal void InitializeForSourceGen(JsonSerializerOptions options, JsonPropertyInfoValues <T> propertyInfo)
        {
            Options = options;
            ClrName = propertyInfo.PropertyName;

            // Property name settings.
            if (propertyInfo.JsonPropertyName != null)
            {
                NameAsString = propertyInfo.JsonPropertyName;
            }
            else if (options.PropertyNamingPolicy == null)
            {
                NameAsString = ClrName;
            }
            else
            {
                NameAsString = options.PropertyNamingPolicy.ConvertName(ClrName);
                if (NameAsString == null)
                {
                    ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(DeclaringType, this);
                }
            }

            NameAsUtf8Bytes ??= Encoding.UTF8.GetBytes(NameAsString !);
            EscapedNameSection ??= JsonHelpers.GetEscapedPropertyNameSection(NameAsUtf8Bytes, Options.Encoder);
            SrcGen_IsPublic        = propertyInfo.IsPublic;
            SrcGen_HasJsonInclude  = propertyInfo.HasJsonInclude;
            SrcGen_IsExtensionData = propertyInfo.IsExtensionData;
            DeclaredPropertyType   = typeof(T);

            JsonTypeInfo propertyTypeInfo = propertyInfo.PropertyTypeInfo;
            Type         declaringType    = propertyInfo.DeclaringType;

            JsonConverter?converter = propertyInfo.Converter;

            if (converter == null)
            {
                converter = propertyTypeInfo.PropertyInfoForTypeInfo.ConverterBase as JsonConverter <T>;
                if (converter == null)
                {
                    throw new InvalidOperationException(SR.Format(SR.ConverterForPropertyMustBeValid, declaringType, ClrName, typeof(T)));
                }
            }

            ConverterBase = converter;

            if (propertyInfo.IgnoreCondition == JsonIgnoreCondition.Always)
            {
                IsIgnored = true;
                Debug.Assert(!ShouldSerialize);
                Debug.Assert(!ShouldDeserialize);
            }
            else
            {
                Get             = propertyInfo.Getter !;
                Set             = propertyInfo.Setter;
                HasGetter       = Get != null;
                HasSetter       = Set != null;
                RuntimeTypeInfo = propertyTypeInfo;
                DeclaringType   = declaringType;
                IgnoreCondition = propertyInfo.IgnoreCondition;
                MemberType      = propertyInfo.IsProperty ? MemberTypes.Property : MemberTypes.Field;

                _converterIsExternalAndPolymorphic = !ConverterBase.IsInternalConverter && DeclaredPropertyType != ConverterBase.TypeToConvert;
                PropertyTypeCanBeNull            = typeof(T).CanBeNull();
                _propertyTypeEqualsTypeToConvert = ConverterBase.TypeToConvert == typeof(T);
                ConverterStrategy   = Converter !.ConverterStrategy;
                RuntimePropertyType = DeclaredPropertyType;
                DetermineIgnoreCondition(IgnoreCondition);
                // TODO: this method needs to also take the number handling option for the declaring type.
                DetermineNumberHandlingForProperty(propertyInfo.NumberHandling, declaringTypeNumberHandling: null);
                DetermineSerializationCapabilities(IgnoreCondition);
            }
        }
Пример #3
0
        /// <summary>
        /// Creates metadata for a property or field.
        /// </summary>
        /// <typeparam name="T">The type that the converter for the property returns or accepts when converting JSON data.</typeparam>
        /// <param name="options">The <see cref="JsonSerializerOptions"/> to initialize the metadata with.</param>
        /// <param name="propertyInfo">Provides serialization metadata about the property or field.</param>
        /// <returns>A <see cref="JsonPropertyInfo"/> instance intialized with the provided metadata.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
        public static JsonPropertyInfo CreatePropertyInfo <T>(JsonSerializerOptions options, JsonPropertyInfoValues <T> propertyInfo)
        {
            if (options is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(options));
            }
            if (propertyInfo is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(propertyInfo));
            }

            Type?declaringType = propertyInfo.DeclaringType;

            if (declaringType == null)
            {
                throw new ArgumentException(nameof(propertyInfo.DeclaringType));
            }

            JsonTypeInfo?propertyTypeInfo = propertyInfo.PropertyTypeInfo;

            if (propertyTypeInfo == null)
            {
                throw new ArgumentException(nameof(propertyInfo.PropertyTypeInfo));
            }

            string?propertyName = propertyInfo.PropertyName;

            if (propertyName == null)
            {
                throw new ArgumentException(nameof(propertyInfo.PropertyName));
            }

            JsonConverter?converter = propertyInfo.Converter;

            if (converter == null)
            {
                converter = propertyTypeInfo.PropertyInfoForTypeInfo.ConverterBase as JsonConverter <T>;
                if (converter == null)
                {
                    throw new InvalidOperationException(SR.Format(SR.ConverterForPropertyMustBeValid, declaringType, propertyName, typeof(T)));
                }
            }

            if (!propertyInfo.IsProperty && propertyInfo.IsVirtual)
            {
                throw new InvalidOperationException(SR.Format(SR.FieldCannotBeVirtual, nameof(propertyInfo.IsProperty), nameof(propertyInfo.IsVirtual)));
            }

            JsonPropertyInfo <T> jsonPropertyInfo = new JsonPropertyInfo <T>();

            jsonPropertyInfo.InitializeForSourceGen(options, propertyInfo);
            return(jsonPropertyInfo);
        }
Пример #4
0
        /// <summary>
        /// Creates metadata for a property or field.
        /// </summary>
        /// <typeparam name="T">The type that the converter for the property returns or accepts when converting JSON data.</typeparam>
        /// <param name="options">The <see cref="JsonSerializerOptions"/> to initialize the metadata with.</param>
        /// <param name="propertyInfo">Provides serialization metadata about the property or field.</param>
        /// <returns>A <see cref="JsonPropertyInfo"/> instance intialized with the provided metadata.</returns>
        /// <remarks>This API is for use by the output of the System.Text.Json source generator and should not be called directly.</remarks>
        public static JsonPropertyInfo CreatePropertyInfo <T>(JsonSerializerOptions options, JsonPropertyInfoValues <T> propertyInfo)
        {
            if (options is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(options));
            }
            if (propertyInfo is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(propertyInfo));
            }

            Type?declaringType = propertyInfo.DeclaringType;

            if (declaringType == null)
            {
                throw new ArgumentException(nameof(propertyInfo.DeclaringType));
            }

            string?propertyName = propertyInfo.PropertyName;

            if (propertyName == null)
            {
                throw new ArgumentException(nameof(propertyInfo.PropertyName));
            }

            if (!propertyInfo.IsProperty && propertyInfo.IsVirtual)
            {
                throw new InvalidOperationException(SR.Format(SR.FieldCannotBeVirtual, nameof(propertyInfo.IsProperty), nameof(propertyInfo.IsVirtual)));
            }

            JsonPropertyInfo <T> jsonPropertyInfo = new JsonPropertyInfo <T>(parentTypeInfo: null);

            jsonPropertyInfo.InitializeForSourceGen(options, propertyInfo);
            return(jsonPropertyInfo);
        }