{"record":{"id":"4f72618895fb23f6","repo":"reactiveui/refit","slug":"unexpected-token-reader-tokentype-when-parsing","errorCode":null,"errorMessage":"Unexpected token {reader.TokenType} when parsing {typeof(TEnum)}.","messagePattern":"Unexpected token (.+?) when parsing (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Refit/CamelCaseStringEnumConverter.cs","lineNumber":229,"sourceCode":"                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>\n        private readonly EnumConverter<TEnum> _inner = new();\n\n        /// <inheritdoc/>\n        public override TEnum? Read(\n            ref Utf8JsonReader reader,\n            Type typeToConvert,\n            JsonSerializerOptions options) =>\n            IsNullOrEmptyString(ref reader) ? null : _inner.Read(ref reader, typeof(TEnum), options);","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/CamelCaseStringEnumConverter.cs#L211-L247","documentation":"Thrown by CamelCaseStringEnumConverter.ReadValue when the current JSON token is neither a string/property-name nor a number — e.g. true/false, null, start of object/array. The converter only knows how to map strings (names) and numbers (underlying values) to enum fields, so any other token type is an error.","triggerScenarios":"The JSON for an enum field is a boolean, null, object, or array rather than a string or number, e.g. `{ \"status\": true }` or `{ \"status\": null }` (for a non-nullable enum) or `{ \"status\": {} }`.","commonSituations":"Server changes a field's type (e.g. from a string code to a boolean flag); a non-nullable enum receiving JSON null; malformed/inconsistent payloads; schema drift after an API change.","solutions":["Correct the JSON to send a valid enum name (string) or numeric value for the field.","If the field can legitimately be absent, make the property a nullable enum (MyEnum?) so null is tolerated by the nullable converter.","If the source genuinely sends a non-string/number, change the receiving type (e.g. bool) or pre-transform the JSON."],"exampleFix":"// before — non-nullable enum receives a boolean token\npublic sealed record Payload(Status Status);\n// JSON: { \"status\": true }  -> throws Unexpected token True\n\n// after — correct the payload type, or make nullable if absent is valid\npublic sealed record Payload(Status? Status);\n// and send { \"status\": \"active\" } or { \"status\": 1 }","handlingStrategy":"validation","validationCode":"// Validate the JSON token type for enum fields before deserializing.\nusing var doc = JsonDocument.Parse(json);\nif (doc.RootElement.TryGetProperty(\"status\", out var el)\n    && el.ValueKind is not (JsonValueKind.String or JsonValueKind.Number))\n{\n    throw new InvalidDataException(\"status must be a string or number.\");\n}","typeGuard":null,"tryCatchPattern":"try { return JsonSerializer.Deserialize<Payload>(json); }\ncatch (JsonException ex) when (ex.Message.Contains(\"Unexpected token\"))\n{ /* wrong token type for enum — fix payload or DTO type */ }","preventionTips":["Make enum properties nullable so JSON null doesn't break a non-nullable enum.","Contract-test that enum fields arrive as string names or numbers, never bools/objects.","When the source changes a field's type, update the DTO type rather than forcing it through the enum converter."],"tags":["json","enum","deserialization","system-text-json","token-type"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}