示例#1
0
        // Create a parameter that is ignored at run-time. It uses the same type (typeof(sbyte)) to help
        // prevent issues with unsupported types and helps ensure we don't accidently (de)serialize it.
        public static JsonParameterInfo CreateIgnoredParameterPlaceholder(JsonParameterInfoValues parameterInfo, JsonPropertyInfo matchingProperty)
        {
            JsonParameterInfo jsonParameterInfo = matchingProperty.ConverterBase.CreateJsonParameterInfo();

            jsonParameterInfo.ClrInfo             = parameterInfo;
            jsonParameterInfo.RuntimePropertyType = matchingProperty.RuntimePropertyType !;
            jsonParameterInfo.NameAsUtf8Bytes     = matchingProperty.NameAsUtf8Bytes !;
            jsonParameterInfo.InitializeDefaultValue(matchingProperty);
            return(jsonParameterInfo);
        }
示例#2
0
        public virtual void Initialize(JsonParameterInfoValues parameterInfo, JsonPropertyInfo matchingProperty, JsonSerializerOptions options)
        {
            ClrInfo           = parameterInfo;
            Options           = options;
            ShouldDeserialize = true;

            PropertyType              = matchingProperty.PropertyType;
            NameAsUtf8Bytes           = matchingProperty.NameAsUtf8Bytes !;
            ConverterBase             = matchingProperty.ConverterBase;
            IgnoreDefaultValuesOnRead = matchingProperty.IgnoreDefaultValuesOnRead;
            NumberHandling            = matchingProperty.NumberHandling;
            MatchingPropertyCanBeNull = matchingProperty.PropertyTypeCanBeNull;
        }
示例#3
0
        private static JsonParameterInfo CreateConstructorParameter(
            JsonParameterInfoValues parameterInfo,
            JsonPropertyInfo jsonPropertyInfo,
            bool sourceGenMode,
            JsonSerializerOptions options)
        {
            if (jsonPropertyInfo.IsIgnored)
            {
                return(JsonParameterInfo.CreateIgnoredParameterPlaceholder(parameterInfo, jsonPropertyInfo, sourceGenMode));
            }

            JsonConverter     converter         = jsonPropertyInfo.ConverterBase;
            JsonParameterInfo jsonParameterInfo = converter.CreateJsonParameterInfo();

            jsonParameterInfo.Initialize(parameterInfo, jsonPropertyInfo, options);

            return(jsonParameterInfo);
        }
示例#4
0
        /// <summary>
        /// Create a parameter that is ignored at run time. It uses the same type (typeof(sbyte)) to help
        /// prevent issues with unsupported types and helps ensure we don't accidently (de)serialize it.
        /// </summary>
        public static JsonParameterInfo CreateIgnoredParameterPlaceholder(
            JsonParameterInfoValues parameterInfo,
            JsonPropertyInfo matchingProperty,
            bool sourceGenMode)
        {
            JsonParameterInfo jsonParameterInfo = new JsonParameterInfo <sbyte>();

            jsonParameterInfo.ClrInfo         = parameterInfo;
            jsonParameterInfo.PropertyType    = matchingProperty.PropertyType;
            jsonParameterInfo.NameAsUtf8Bytes = matchingProperty.NameAsUtf8Bytes !;

            // TODO: https://github.com/dotnet/runtime/issues/60082.
            // Default value initialization for params mapping to ignored properties doesn't
            // account for the default value of optional parameters. This should be fixed.

            if (sourceGenMode)
            {
                // The <T> value in the matching JsonPropertyInfo<T> instance matches the parameter type.
                jsonParameterInfo.DefaultValue = matchingProperty.DefaultValue;
            }
            else
            {
                // The <T> value in the created JsonPropertyInfo<T> instance (sbyte)
                // doesn't match the parameter type, use reflection to get the default value.
                Type parameterType = parameterInfo.ParameterType;

                DefaultValueHolder holder;
                if (matchingProperty.Options.TryGetJsonTypeInfo(parameterType, out JsonTypeInfo? typeInfo))
                {
                    holder = typeInfo.DefaultValueHolder;
                }
                else
                {
                    holder = DefaultValueHolder.CreateHolder(parameterInfo.ParameterType);
                }

                jsonParameterInfo.DefaultValue = holder.DefaultValue;
            }

            return(jsonParameterInfo);
        }
示例#5
0
        private static JsonParameterInfoValues[] GetParameterInfoArray(ParameterInfo[] parameters)
        {
            int parameterCount = parameters.Length;

            JsonParameterInfoValues[] jsonParameters = new JsonParameterInfoValues[parameterCount];

            for (int i = 0; i < parameterCount; i++)
            {
                ParameterInfo reflectionInfo = parameters[i];

                JsonParameterInfoValues jsonInfo = new()
                {
                    Name            = reflectionInfo.Name !,
                    ParameterType   = reflectionInfo.ParameterType,
                    Position        = reflectionInfo.Position,
                    HasDefaultValue = reflectionInfo.HasDefaultValue,
                    DefaultValue    = reflectionInfo.GetDefaultValue()
                };

                jsonParameters[i] = jsonInfo;
            }

            return(jsonParameters);
        }
 public override void Initialize(JsonParameterInfoValues parameterInfo, JsonPropertyInfo matchingProperty, JsonSerializerOptions options)
 {
     base.Initialize(parameterInfo, matchingProperty, options);
     InitializeDefaultValue(matchingProperty);
 }