{"record":{"id":"33888ecd8d702514","repo":"reactiveui/refit","slug":"unable-to-convert-value-to-typeof-tenum","errorCode":null,"errorMessage":"Unable to convert '{value}' to {typeof(TEnum)}.","messagePattern":"Unable to convert '(.+?)' to (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Refit/CamelCaseStringEnumConverter.cs","lineNumber":221,"sourceCode":"            if (reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName)\n            {\n                var value = reader.GetString();\n                if (value is null || string.IsNullOrWhiteSpace(value))\n                {\n                    throw new JsonException($\"Cannot convert an empty value to {typeof(TEnum)}.\");\n                }\n\n                if (_namesToValues.TryGetValue(value!, out var namedValue))\n                {\n                    return namedValue;\n                }\n\n                if (_namesToValuesIgnoreCase.TryGetValue(value!, out var namedValueIgnoreCase))\n                {\n                    return namedValueIgnoreCase;\n                }\n\n                throw new JsonException($\"Unable to convert '{value}' to {typeof(TEnum)}.\");\n            }\n\n            if (reader.TokenType == JsonTokenType.Number)\n            {\n                return EnumHelpers.Info<TEnum>.ReadJsonNumericValue(ref reader);\n            }\n\n            throw new JsonException($\"Unexpected token {reader.TokenType} when parsing {typeof(TEnum)}.\");\n        }\n    }\n\n    /// <summary>A strongly-typed JSON converter for nullable enums that maps values to and from camelCase names.</summary>\n    /// <typeparam name=\"TEnum\">The underlying enum type.</typeparam>\n    internal sealed class NullableEnumConverter<\n        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum> : JsonConverter<TEnum?>\n        where TEnum : struct, Enum\n    {\n        /// <summary>The underlying non-nullable enum converter that performs the name/value mapping.</summary>","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/CamelCaseStringEnumConverter.cs#L203-L239","documentation":"Thrown by CamelCaseStringEnumConverter when the JSON string token's value does not match any enum field name (case-sensitively or insensitively) and is not numeric. The converter tried both the exact-name and ignore-case maps and found nothing, so it cannot produce a valid enum value and raises a JsonException naming the bad value.","triggerScenarios":"Deserializing an enum from a JSON string whose name doesn't exist, e.g. enum Status { Active, Inactive } and JSON `{ \"status\": \"Actv\" }` (typo) or `{ \"status\": \"PENDING\" }` (wrong casing/variant). The camelCase mapping also matters: names are matched against the camelCased enum field names.","commonSituations":"Server/client enum naming drift (e.g. server sends snake_case or SCREAMING_CASE while converter expects camelCase); typos; a new enum value the client doesn't have yet (version skew); sending a display label instead of the code name.","solutions":["Align the JSON value with a valid enum field name, respecting the camelCase naming the converter expects (or configure the converter's naming policy).","If the client may receive unknown values, deserialize into the enum backed by JsonStringEnumConverter with a fallback, or use a string property plus manual parsing with Enum.TryParse.","Resolve version skew by adding the missing enum member or normalizing the source value."],"exampleFix":"// before — enum mismatch / typo\npublic enum Status { Active, Inactive }\n// JSON: { \"status\": \"actv\" }  -> throws\n\n// after — correct camelCase name (or add [JsonStringEnumName])\n// JSON: { \"status\": \"active\" }\n// or parse defensively:\nvar raw = JsonDocument.Parse(json).RootElement.GetProperty(\"status\").GetString();\nif (Enum.TryParse<Status>(raw, ignoreCase: true, out var s)) { /* use s */ }","handlingStrategy":"validation","validationCode":"// Validate a value maps to the enum before deserializing the whole payload.\nstatic bool IsKnownEnumName<TEnum>(string? name) where TEnum : struct, Enum =>\n    name is not null && Enum.TryParse<TEnum>(name, ignoreCase: true, out _);\n\n// or: keep a whitelist of valid camelCase names\nstatic readonly HashSet<string> Valid = new(Enum.GetNames<Status>().Select(CamelCase));","typeGuard":"// Loose-parse guard using the underlying enum.\nstatic bool TryReadEnum<TEnum>(string? raw, out TEnum value) where TEnum : struct, Enum =>\n    Enum.TryParse(raw, ignoreCase: true, out value);","tryCatchPattern":"try { return JsonSerializer.Deserialize<Payload>(json); }\ncatch (JsonException ex) when (ex.Message.Contains(\"Unable to convert\"))\n{ /* unknown enum value — log and use a default/Unknown member */ }","preventionTips":["Align client and server enum naming; respect the converter's camelCase policy.","Reserve an explicit Unknown/default enum member for unrecognized values when the source is untrusted.","Parse defensively into a string then Enum.TryParse when you must tolerate drift."],"tags":["json","enum","deserialization","system-text-json"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}