protected override void InitializeConstructorArgumentCaches(ref ReadStack state, JsonSerializerOptions options) { JsonTypeInfo typeInfo = state.Current.JsonTypeInfo; if (typeInfo.CreateObjectWithArgs == null) { typeInfo.CreateObjectWithArgs = options.MemberAccessorStrategy.CreateParameterizedConstructor <T, TArg0, TArg1, TArg2, TArg3>(ConstructorInfo !); } var arguments = new Arguments <TArg0, TArg1, TArg2, TArg3>(); List <KeyValuePair <string, JsonParameterInfo> > cache = typeInfo.ParameterCache !.List; for (int i = 0; i < typeInfo.ParameterCount; i++) { JsonParameterInfo parameterInfo = cache[i].Value; // We can afford not to set default values for ctor arguments when we should't deserialize because the // type parameters of the `Arguments` type provide default semantics that work well with value types. if (parameterInfo.ShouldDeserialize) { int position = parameterInfo.ClrInfo.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; }
private static bool TryRead <TArg>( ref ReadStack state, ref Utf8JsonReader reader, JsonParameterInfo jsonParameterInfo, out TArg arg) { Debug.Assert(jsonParameterInfo.ShouldDeserialize); Debug.Assert(jsonParameterInfo.Options != null); var info = (JsonParameterInfo <TArg>)jsonParameterInfo; var converter = (JsonConverter <TArg>)jsonParameterInfo.ConverterBase; bool success = converter.TryRead(ref reader, info.PropertyType, info.Options !, ref state, out TArg? value); arg = value == null && jsonParameterInfo.IgnoreDefaultValuesOnRead ? (TArg?)info.DefaultValue ! // Use default value specified on parameter, if any. : value !; return(success); }
protected sealed override bool ReadAndCacheConstructorArgument(ref ReadStack state, ref Utf8JsonReader reader, JsonParameterInfo jsonParameterInfo) { Debug.Assert(jsonParameterInfo.ShouldDeserialize); Debug.Assert(jsonParameterInfo.Options != null); bool success = jsonParameterInfo.ConverterBase.TryReadAsObject(ref reader, jsonParameterInfo.Options !, ref state, out object?arg); if (success && !(arg == null && jsonParameterInfo.IgnoreDefaultValuesOnRead)) { ((object[])state.Current.CtorArgumentState !.Arguments)[jsonParameterInfo.ClrInfo.Position] = arg !;
protected override bool ReadAndCacheConstructorArgument(ref ReadStack state, ref Utf8JsonReader reader, JsonParameterInfo jsonParameterInfo) { Debug.Assert(state.Current.CtorArgumentState !.Arguments != null); var arguments = (Arguments <TArg0, TArg1, TArg2, TArg3>)state.Current.CtorArgumentState.Arguments; bool success; switch (jsonParameterInfo.Position) { case 0: success = ((JsonParameterInfo <TArg0>)jsonParameterInfo).ReadJsonTyped(ref state, ref reader, out TArg0 arg0); if (success) { arguments.Arg0 = arg0; } break; case 1: success = ((JsonParameterInfo <TArg1>)jsonParameterInfo).ReadJsonTyped(ref state, ref reader, out TArg1 arg1); if (success) { arguments.Arg1 = arg1; } break; case 2: success = ((JsonParameterInfo <TArg2>)jsonParameterInfo).ReadJsonTyped(ref state, ref reader, out TArg2 arg2); if (success) { arguments.Arg2 = arg2; } break; case 3: success = ((JsonParameterInfo <TArg3>)jsonParameterInfo).ReadJsonTyped(ref state, ref reader, out TArg3 arg3); if (success) { arguments.Arg3 = arg3; } break; default: Debug.Fail("This should never happen."); throw new InvalidOperationException(); } return(success); }
protected override bool ReadAndCacheConstructorArgument(ref ReadStack state, ref Utf8JsonReader reader, JsonParameterInfo jsonParameterInfo) { bool success = jsonParameterInfo.ReadJson(ref state, ref reader, out object?arg0); if (success) { ((object[])state.Current.CtorArgumentState !.Arguments !)[jsonParameterInfo.Position] = arg0 !;
public ParameterRef(ulong key, JsonParameterInfo info) { Key = key; Info = info; }
public ParameterRef(ulong key, JsonParameterInfo info, byte[] nameFromJson) { Key = key; Info = info; NameFromJson = nameFromJson; }