Esempio n. 1
0
        private static JsonParameterInfo AddConstructorParameter(
            ParameterInfo parameterInfo,
            JsonPropertyInfo jsonPropertyInfo,
            JsonSerializerOptions options)
        {
            string matchingPropertyName = jsonPropertyInfo.NameAsString !;

            if (jsonPropertyInfo.IsIgnored)
            {
                return(JsonParameterInfo.CreateIgnoredParameterPlaceholder(matchingPropertyName, parameterInfo, options));
            }

            JsonConverter converter = jsonPropertyInfo.ConverterBase;

            JsonParameterInfo jsonParameterInfo = converter.CreateJsonParameterInfo();

            jsonParameterInfo.Initialize(
                matchingPropertyName,
                jsonPropertyInfo.DeclaredPropertyType,
                jsonPropertyInfo.RuntimePropertyType !,
                parameterInfo,
                converter,
                options);

            return(jsonParameterInfo);
        }
Esempio n. 2
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(
            ParameterInfo parameterInfo,
            JsonPropertyInfo matchingProperty,
            JsonSerializerOptions options)
        {
            JsonParameterInfo jsonParameterInfo = new JsonParameterInfo <sbyte>();

            jsonParameterInfo.Options           = options;
            jsonParameterInfo.ParameterInfo     = parameterInfo;
            jsonParameterInfo.ShouldDeserialize = false;

            jsonParameterInfo.DetermineParameterName(matchingProperty);

            return(jsonParameterInfo);
        }
Esempio n. 3
0
        private static JsonParameterInfo AddConstructorParameter(
            ParameterInfo parameterInfo,
            JsonPropertyInfo jsonPropertyInfo,
            JsonSerializerOptions options)
        {
            if (jsonPropertyInfo.IsIgnored)
            {
                return(JsonParameterInfo.CreateIgnoredParameterPlaceholder(jsonPropertyInfo, options));
            }

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

            jsonParameterInfo.Initialize(
                jsonPropertyInfo.RuntimePropertyType !,
                parameterInfo,
                jsonPropertyInfo,
                options);

            return(jsonParameterInfo);
        }
Esempio n. 4
0
        private void InitializeConstructorParameters(Dictionary <string, JsonPropertyInfo> propertyCache, ConstructorInfo constructorInfo)
        {
            ParameterInfo[] parameters = constructorInfo !.GetParameters();
            Dictionary <string, JsonParameterInfo> parameterCache = CreateParameterCache(parameters.Length, Options);

            foreach (ParameterInfo parameterInfo in parameters)
            {
                PropertyInfo?firstMatch = null;
                bool         isBound    = false;

                foreach (JsonPropertyInfo jsonPropertyInfo in PropertyCacheArray !)
                {
                    // This is not null because it is an actual
                    // property on a type, not a "policy property".
                    PropertyInfo propertyInfo = jsonPropertyInfo.PropertyInfo !;

                    string camelCasePropName = JsonNamingPolicy.CamelCase.ConvertName(propertyInfo.Name);

                    if (parameterInfo.Name == camelCasePropName &&
                        parameterInfo.ParameterType == propertyInfo.PropertyType)
                    {
                        if (isBound)
                        {
                            Debug.Assert(firstMatch != null);

                            // Multiple object properties cannot bind to the same
                            // constructor parameter.
                            ThrowHelper.ThrowInvalidOperationException_MultiplePropertiesBindToConstructorParameters(
                                Type,
                                parameterInfo,
                                firstMatch,
                                propertyInfo,
                                constructorInfo);
                        }

                        JsonParameterInfo jsonParameterInfo = AddConstructorParameter(parameterInfo, jsonPropertyInfo, Options);

                        // One object property cannot map to multiple constructor
                        // parameters (ConvertName above can't return multiple strings).
                        parameterCache.Add(jsonPropertyInfo.NameAsString !, jsonParameterInfo);

                        // Remove property from deserialization cache to reduce the number of JsonPropertyInfos considered during JSON matching.
                        propertyCache.Remove(jsonPropertyInfo.NameAsString !);

                        isBound    = true;
                        firstMatch = propertyInfo;
                    }
                }
            }

            // It is invalid for the extension data property to bind with a constructor argument.
            if (DataExtensionProperty != null &&
                parameterCache.ContainsKey(DataExtensionProperty.NameAsString !))
            {
                ThrowHelper.ThrowInvalidOperationException_ExtensionDataCannotBindToCtorParam(DataExtensionProperty.PropertyInfo !, Type, constructorInfo);
            }

            ParameterCache = parameterCache;
            ParameterCount = parameters.Length;

            PropertyCache = propertyCache;
        }