dotnetcore/CAP · error · NotSupportedException
Type is not of type JsonElement
Error message
Type is not of type JsonElement
What it means
A type guard in the JsonUtf8 serializer's Deserialize(object value, Type valueType) overload: this implementation can only rehydrate values that arrived as System.Text.Json JsonElement nodes (the shape produced when CAP deserializes the message body with JsonUtf8). Passing a string, byte[], JObject, or any other runtime type reaches the guard and fails with NotSupportedException; the faulty input is the boxed 'value' argument, not the target valueType.
Solutions
- Convert the input to a JsonElement first (e.g. parse a JSON string with JsonSerializer.Deserialize<JsonElement>(json)) before calling this overload.
- If you need to deserialize from string or bytes, call the string-based Deserialize(json) overload instead.
- When substituting serializers in CAP options, ensure producer and consumer both use the same serializer so bodies arrive as JsonElement.
- Use a custom ISerializer implementation matching your wire format (registered in AddCAP) instead of relying on JsonUtf8Serializer for non-JsonElement inputs.
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs:56 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14).
Data as JSON: /api/errors/fcc1502be73aa8af.
Report an issue: GitHub.
Appendix: source
Thrown at src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs:56
return new ValueTask<Message>(new Message(transportMessage.Headers, obj));
}
public string Serialize(Message message)
{
return JsonSerializer.Serialize(message, _jsonSerializerOptions);
}
public Message? Deserialize(string json)
{
return JsonSerializer.Deserialize<Message>(json, _jsonSerializerOptions);
}
public object? Deserialize(object value, Type valueType)
{
if (value is JsonElement jsonElement) return jsonElement.Deserialize(valueType, _jsonSerializerOptions);
throw new NotSupportedException("Type is not of type JsonElement");
}
public bool IsJsonType(object jsonObject)
{
return jsonObject is JsonElement;
}
}View on GitHub (pinned to e52b8508e5)