JamesNK/Newtonsoft.Json · error · JsonSerializationException
Error setting value to '{0}' on '{1}'.
Error message
Error setting value to '{0}' on '{1}'. What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Serialization/ExpressionValueProvider.cs:88
if (_setter == null)
{
_setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(_memberInfo);
}
#if DEBUG
// dynamic method doesn't check whether the type is 'legal' to set
// add this check for unit tests
if (value != null && !ReflectionUtils.GetMemberUnderlyingType(_memberInfo).IsAssignableFrom(value.GetType()))
{
throw new JsonSerializationException("Incompatible value. Cannot set {0} to type {1}.".FormatWith(CultureInfo.InvariantCulture, _memberInfo, value.GetType()));
}
#endif
_setter(target, value);
}
catch (Exception ex)
{
throw new JsonSerializationException("Error setting value to '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
}
}
/// <summary>
/// Gets the value.
/// </summary>
/// <param name="target">The target to get the value from.</param>
/// <returns>The value.</returns>
public object? GetValue(object target)
{
try
{
if (_getter == null)
{
_getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);
}
return _getter(target);View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before: init-only property fails under expression provider
public class Model { public int Id { get; init; } }
var m = JsonConvert.DeserializeObject<Model>("{\"Id\":\"x\"}");
// after: parse with constructor
public class Model {
public Model(int id) => Id = id;
public int Id { get; }
} Defensive patterns
Strategy: try-catch
Validate before calling
if (!memberType.IsAssignableFrom(value?.GetType())) throw new ArgumentException("value not assignable to " + memberType); Type guard
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()); } Try / catch
try { JsonConvert.DeserializeObject<T>(json); }
catch (JsonSerializationException ex) when (ex.InnerException is Exception inner) {
logger.Error(inner, "expression provider setter failed at {Path}", ex.Path);
throw;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Error setting value to '{0}' on '{1}'.
- Error getting value from '{0}' on '{1}'.
- Error getting value from '{0}' on '{1}'.
- Error setting value to '{0}' on '{1}'.
- Could not create getter for {0}. ByRef return values are not
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/ccbca34e3bc6ea4d.
Report an issue: GitHub.