internal abstract void InitializeForTypeInfo( Type declaredType, JsonTypeInfo runtimeTypeInfo, JsonConverter converter, JsonSerializerOptions options);
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); } }
/// <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="isProperty">Whether the CLR member is a property or field.</param> /// <param name="isPublic">Whether the CLR member is public.</param> /// <param name="isVirtual">Whether the CLR member is a virtual property.</param> /// <param name="declaringType">The declaring type of the property or field.</param> /// <param name="propertyTypeInfo">The <see cref="JsonTypeInfo"/> info for the property or field's type.</param> /// <param name="converter">A <see cref="JsonConverter"/> for the property or field, specified by <see cref="JsonConverterAttribute"/>.</param> /// <param name="getter">Provides a mechanism to get the property or field's value.</param> /// <param name="setter">Provides a mechanism to set the property or field's value.</param> /// <param name="ignoreCondition">Specifies a condition for the property to be ignored.</param> /// <param name="numberHandling">If the property or field is a number, specifies how it should processed when serializing and deserializing.</param> /// <param name="hasJsonInclude">Whether the property was annotated with <see cref="JsonIncludeAttribute"/>.</param> /// <param name="propertyName">The CLR name of the property or field.</param> /// <param name="jsonPropertyName">The name to be used when processing the property or field, specified by <see cref="JsonPropertyNameAttribute"/>.</param> /// <returns>A <see cref="JsonPropertyInfo"/> instance intialized with the provided metadata.</returns> public static JsonPropertyInfo CreatePropertyInfo <T>( JsonSerializerOptions options, bool isProperty, bool isPublic, bool isVirtual, Type declaringType, JsonTypeInfo propertyTypeInfo, JsonConverter <T>?converter, Func <object, T>?getter, Action <object, T>?setter, JsonIgnoreCondition?ignoreCondition, bool hasJsonInclude, JsonNumberHandling?numberHandling, string propertyName, string?jsonPropertyName) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (declaringType == null) { throw new ArgumentNullException(nameof(declaringType)); } if (propertyTypeInfo == null) { throw new ArgumentNullException(nameof(propertyTypeInfo)); } if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } 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 (!isProperty && isVirtual) { throw new InvalidOperationException(SR.Format(SR.FieldCannotBeVirtual, nameof(isProperty), nameof(isVirtual))); } JsonPropertyInfo <T> jsonPropertyInfo = new JsonPropertyInfo <T>(); jsonPropertyInfo.InitializeForSourceGen( options, isProperty, isPublic, declaringType, propertyTypeInfo, converter, getter, setter, ignoreCondition, hasJsonInclude, numberHandling, propertyName, jsonPropertyName); return(jsonPropertyInfo); }
internal void InitializeForSourceGen( JsonSerializerOptions options, bool isProperty, Type declaringType, JsonTypeInfo typeInfo, JsonConverter <T> converter, Func <object, T>?getter, Action <object, T>?setter, JsonIgnoreCondition ignoreCondition, JsonNumberHandling numberHandling, string propertyName, JsonEncodedText jsonPropertyName) { Options = options; ClrName = propertyName; byte[] encodedName = jsonPropertyName._utf8Value; string encodedNameAsStr = jsonPropertyName._value; // Property name settings. if (encodedName != null && options.PropertyNamingPolicy == null && options.Encoder == null) { NameAsString = encodedNameAsStr; NameAsUtf8Bytes = encodedName; int nameLength = encodedName.Length; EscapedNameSection = new byte[nameLength + 3]; EscapedNameSection[0] = (byte)'"'; encodedName.CopyTo(EscapedNameSection, 1); EscapedNameSection[nameLength - 2] = (byte)'"'; EscapedNameSection[nameLength - 1] = (byte)':'; } else { if (encodedNameAsStr != null) { NameAsString = encodedNameAsStr; } 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); } if (ignoreCondition == JsonIgnoreCondition.Always) { IsIgnored = true; Debug.Assert(!ShouldSerialize); Debug.Assert(!ShouldDeserialize); } else { Get = getter; Set = setter; HasGetter = Get != null; HasSetter = Set != null; ConverterBase = converter; RuntimeTypeInfo = typeInfo; DeclaredPropertyType = typeof(T); DeclaringType = declaringType; IgnoreCondition = ignoreCondition; MemberType = isProperty ? MemberTypes.Property : MemberTypes.Field; _converterIsExternalAndPolymorphic = !converter.IsInternalConverter && DeclaredPropertyType != converter.TypeToConvert; PropertyTypeCanBeNull = typeof(T).CanBeNull(); _propertyTypeEqualsTypeToConvert = converter.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(numberHandling, declaringTypeNumberHandling: null); DetermineSerializationCapabilities(IgnoreCondition); } }
/// <summary> /// Creates a JsonPropertyInfo whose property type matches the type of this JsonTypeInfo instance. /// </summary> private protected abstract JsonPropertyInfo CreateJsonPropertyInfo(JsonTypeInfo declaringTypeInfo, JsonSerializerOptions options);
internal void InitializeForSourceGen( JsonSerializerOptions options, bool isProperty, bool isPublic, Type declaringType, JsonTypeInfo typeInfo, JsonConverter <T> converter, Func <object, T>?getter, Action <object, T>?setter, JsonIgnoreCondition?ignoreCondition, bool hasJsonInclude, JsonNumberHandling?numberHandling, string propertyName, string?jsonPropertyName) { Options = options; ClrName = propertyName; // Property name settings. if (jsonPropertyName != null) { NameAsString = 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 = isPublic; SrcGen_HasJsonInclude = hasJsonInclude; DeclaredPropertyType = typeof(T); ConverterBase = converter; if (ignoreCondition == JsonIgnoreCondition.Always) { IsIgnored = true; Debug.Assert(!ShouldSerialize); Debug.Assert(!ShouldDeserialize); } else { Get = getter; Set = setter; HasGetter = Get != null; HasSetter = Set != null; RuntimeTypeInfo = typeInfo; DeclaringType = declaringType; IgnoreCondition = ignoreCondition; MemberType = isProperty ? MemberTypes.Property : MemberTypes.Field; _converterIsExternalAndPolymorphic = !converter.IsInternalConverter && DeclaredPropertyType != converter.TypeToConvert; PropertyTypeCanBeNull = typeof(T).CanBeNull(); _propertyTypeEqualsTypeToConvert = converter.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(numberHandling, declaringTypeNumberHandling: null); DetermineSerializationCapabilities(IgnoreCondition); } }