{"record":{"id":"ccbca34e3bc6ea4d","repo":"JamesNK/Newtonsoft.Json","slug":"error-setting-value-to-0-on-1-ccbca3","errorCode":null,"errorMessage":"Error setting value to '{0}' on '{1}'.","messagePattern":"Error setting value to '(.+?)' on '(.+?)'\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs","lineNumber":88,"sourceCode":"                if (_setter == null)\n                {\n                    _setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(_memberInfo);\n                }\n\n#if DEBUG\n                // dynamic method doesn't check whether the type is 'legal' to set\n                // add this check for unit tests\n                if (value != null && !ReflectionUtils.GetMemberUnderlyingType(_memberInfo).IsAssignableFrom(value.GetType()))\n                {\n                    throw new JsonSerializationException(\"Incompatible value. Cannot set {0} to type {1}.\".FormatWith(CultureInfo.InvariantCulture, _memberInfo, value.GetType()));\n                }\n#endif\n\n                _setter(target, value);\n            }\n            catch (Exception ex)\n            {\n                throw new JsonSerializationException(\"Error setting value to '{0}' on '{1}'.\".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);\n            }\n        }\n\n        /// <summary>\n        /// Gets the value.\n        /// </summary>\n        /// <param name=\"target\">The target to get the value from.</param>\n        /// <returns>The value.</returns>\n        public object? GetValue(object target)\n        {\n            try\n            {\n                if (_getter == null)\n                {\n                    _getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);\n                }\n\n                return _getter(target);","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs#L70-L106","documentation":"Identical semantics to error 220 but emitted by ExpressionValueProvider.SetValue, which uses compiled LINQ expression trees rather than Reflection.Emit dynamic methods. This provider is selected on platforms that support expressions but not full dynamic code (e.g. some .NET Core / NETSTANDARD2_0 configurations where ReflectionDelegateFactory resolves to ExpressionReflectionDelegateFactory). The '{0}' is the member name and '{1}' the target type; the original setter exception is the InnerException.","triggerScenarios":"Same as 220: a member setter throws while assigning a deserialized value, or the value cannot be cast to the member type. Specifically seen on .NET Core / .NET 5+ where the expression-based provider is the default.","commonSituations":"Migrating from .NET Framework (DynamicValueProvider) to .NET Core/.NET 5+ (ExpressionValueProvider) where the same data now surfaces through a different provider; type mismatches; init-only or required-setter failures; trimming/AOT scenarios where the expression cannot bind.","solutions":["Inspect InnerException for the root setter error.","Ensure the member has a public setter compatible with the JSON value type.","Use [JsonConstructor] or a parameterized constructor to populate read-only/init members.","If running under NativeAOT/trimming, register the type with JsonSerializerContext / source generation or avoid reflection-based providers."],"exampleFix":"// before: init-only property fails under expression provider\npublic class Model { public int Id { get; init; } }\nvar m = JsonConvert.DeserializeObject<Model>(\"{\\\"Id\\\":\\\"x\\\"}\");\n// after: parse with constructor\npublic class Model {\n    public Model(int id) => Id = id;\n    public int Id { get; }\n}","handlingStrategy":"try-catch","validationCode":"if (!memberType.IsAssignableFrom(value?.GetType())) throw new ArgumentException(\"value not assignable to \" + memberType);","typeGuard":"static bool CanBind(MemberInfo m, object value) { var t = m is PropertyInfo p ? p.PropertyType : ((FieldInfo)m).FieldType; return value == null ? !t.IsValueType || Nullable.GetUnderlyingType(t)!=null : t.IsAssignableFrom(value.GetType()); }","tryCatchPattern":"try { JsonConvert.DeserializeObject<T>(json); }\ncatch (JsonSerializationException ex) when (ex.InnerException is Exception inner) {\n    logger.Error(inner, \"expression provider setter failed at {Path}\", ex.Path);\n    throw;\n}","preventionTips":["On .NET Core/.NET 5+ ensure members have public setters matching JSON value types.","Prefer [JsonConstructor] over init-only auto-properties.","Test deserialization against real JSON payloads after migrating runtimes.","For AOT/trimmed apps, pre-register types or use source-generated serializers."],"tags":["deserialization","serialization","reflection","expression-value-provider","aot"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}