JamesNK/Newtonsoft.Json · error · JsonSerializationException
Error setting value to '{0}' on '{1}'.
Error message
Error setting value to '{0}' on '{1}'. What it means
Thrown by ReflectionValueProvider.SetValue when ReflectionUtils.SetMemberValue throws while assigning a value to a member (field or property) of the target. '{0}' is the member name, '{1}' is the target runtime type; the original exception is the InnerException. ReflectionValueProvider is the fallback provider used when neither dynamic-method nor expression providers are available (e.g. limited trust / restricted reflection).
Source
Thrown at Src/Newtonsoft.Json/Serialization/ReflectionValueProvider.cs:63
{
ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo));
_memberInfo = memberInfo;
}
/// <summary>
/// Sets the value.
/// </summary>
/// <param name="target">The target to set the value on.</param>
/// <param name="value">The value to set on the target.</param>
public void SetValue(object target, object? value)
{
try
{
ReflectionUtils.SetMemberValue(_memberInfo, 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
{
// https://github.com/dotnet/corefx/issues/26053
if (_memberInfo is PropertyInfo propertyInfo && propertyInfo.PropertyType.IsByRef)
{
throw new InvalidOperationException("Could not create getter for {0}. ByRef return values are not supported.".FormatWith(CultureInfo.InvariantCulture, propertyInfo));
}
View on GitHub (pinned to 4f73e74372)
Solutions
- Inspect InnerException to identify the underlying setter/reflection failure.
- Ensure the member has an accessible setter and the JSON value type is assignable to it.
- Use a parameterized constructor/[JsonConstructor] to supply read-only members.
- Register a custom JsonConverter to coerce the value to the correct type before assignment.
Example fix
// before: readonly field cannot be set via reflection
public class Model { public readonly int Id; }
JsonConvert.DeserializeObject<Model>("{\"Id\":5}");
// after: settable property or constructor
public class Model {
public Model(int id) { Id = id; }
public int Id { get; }
} Defensive patterns
Strategy: try-catch
Validate before calling
var prop = target.GetType().GetProperty(memberName); if (prop != null && !prop.CanWrite) throw new InvalidOperationException(memberName + " is read-only");
Try / catch
try { JsonConvert.DeserializeObject<T>(json); }
catch (JsonSerializationException ex) when (ex.Message.Contains("Error setting value")) {
logger.Error(ex.InnerException, "reflection setter failed at {Path}", ex.Path); throw;
} Prevention
- Ensure all serialized members have accessible setters.
- Use [JsonConstructor] for read-only/init-only members.
- Avoid readonly fields for deserialized data.
- Register a custom JsonConverter for members needing coercion.
When it happens
Trigger: Deserializing JSON into a member via plain reflection where SetMemberValue fails: type mismatch, setter throws ArgumentException, attempt to set a const/read-only/initializer-only field, value is incompatible with the member's underlying type.
Common situations: Running under partial trust or restricted reflection where DynamicValueProvider/ExpressionValueProvider are unavailable; setting a readonly field; assigning a derived type to a property whose setter validates the value; version where ReflectionValueProvider is the active provider.
Related errors
- Property does not have a setter.
- Error setting value to '{0}' on '{1}'.
- Error setting value to '{0}' on '{1}'.
- Could not create getter for {0}. ByRef return values are not
- Error getting value from '{0}' on '{1}'.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/52f62c9180386797.
Report an issue: GitHub.