Ejemplo n.º 1
0
        protected override void InitializeConstructorArgumentCaches(ref ReadStack state, JsonSerializerOptions options)
        {
            JsonClassInfo classInfo = state.Current.JsonClassInfo;

            if (classInfo.CreateObjectWithArgs == null)
            {
                classInfo.CreateObjectWithArgs =
                    options.MemberAccessorStrategy.CreateParameterizedConstructor <T, TArg0, TArg1, TArg2, TArg3>(ConstructorInfo !);
            }

            var arguments = new Arguments <TArg0, TArg1, TArg2, TArg3>();

            foreach (JsonParameterInfo parameterInfo in classInfo.ParameterCache !.Values)
            {
                if (parameterInfo.ShouldDeserialize)
                {
                    int position = parameterInfo.Position;

                    switch (position)
                    {
                    case 0:
                        arguments.Arg0 = ((JsonParameterInfo <TArg0>)parameterInfo).TypedDefaultValue !;
                        break;

                    case 1:
                        arguments.Arg1 = ((JsonParameterInfo <TArg1>)parameterInfo).TypedDefaultValue !;
                        break;

                    case 2:
                        arguments.Arg2 = ((JsonParameterInfo <TArg2>)parameterInfo).TypedDefaultValue !;
                        break;

                    case 3:
                        arguments.Arg3 = ((JsonParameterInfo <TArg3>)parameterInfo).TypedDefaultValue !;
                        break;

                    default:
                        Debug.Fail("More than 4 params: we should be in override for LargeObjectWithParameterizedConstructorConverter.");
                        throw new InvalidOperationException();
                    }
                }
            }

            state.Current.CtorArgumentState !.Arguments = arguments;
        }
Ejemplo n.º 2
0
        protected override void InitializeConstructorArgumentCaches(ref ReadStack state, JsonSerializerOptions options)
        {
            JsonClassInfo classInfo = state.Current.JsonClassInfo;

            if (classInfo.CreateObjectWithArgs == null)
            {
                classInfo.CreateObjectWithArgs = options.MemberAccessorStrategy.CreateParameterizedConstructor<T>(ConstructorInfo!);
            }

            object[] arguments = ArrayPool<object>.Shared.Rent(classInfo.ParameterCount);
            foreach (JsonParameterInfo jsonParameterInfo in classInfo.ParameterCache!.Values)
            {
                if (jsonParameterInfo.ShouldDeserialize)
                {
                    arguments[jsonParameterInfo.Position] = jsonParameterInfo.DefaultValue!;
                }
            }

            state.Current.CtorArgumentState!.Arguments = arguments;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Lookup the constructor parameter given its name in the reader.
        /// </summary>
        protected override bool TryLookupConstructorParameter(
            ref ReadStack state,
            ref Utf8JsonReader reader,
            JsonSerializerOptions options,
            out JsonParameterInfo?jsonParameterInfo)
        {
            JsonClassInfo classInfo = state.Current.JsonClassInfo;
            ArgumentState?argState  = state.Current.CtorArgumentState;

            Debug.Assert(classInfo.ClassType == ClassType.Object);
            Debug.Assert(argState != null);
            Debug.Assert(_keyName != null);
            Debug.Assert(_valueName != null);

            bool caseInsensitiveMatch = options.PropertyNameCaseInsensitive;

            string propertyName = reader.GetString() !;

            state.Current.JsonPropertyNameAsString = propertyName;

            if (!argState.FoundKey &&
                FoundKeyProperty(propertyName, caseInsensitiveMatch))
            {
                jsonParameterInfo = classInfo.ParameterCache ![_keyName];