{"record":{"id":"2bf32ec6c07606cd","repo":"cefsharp/CefSharp","slug":"unable-to-convert-enum","errorCode":null,"errorMessage":"Unable to convert Enum","messagePattern":"Unable to convert Enum","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"CefSharp/Internals/Json/JsonEnumConverterFactory.cs","lineNumber":59,"sourceCode":"            return converter;\n        }\n\n        public static object ConvertStringToEnum(string val, Type typeToConvert)\n        {\n            foreach (var name in Enum.GetNames(typeToConvert))\n            {\n                var attribute = typeToConvert.GetField(name)\n                    .GetCustomAttributes(false)\n                    .OfType<JsonPropertyNameAttribute>()\n                    .Single();\n\n                if (attribute.Name == val)\n                {\n                    return Enum.Parse(typeToConvert, name);\n                }\n            }\n\n            throw new JsonException(\"Unable to convert Enum\");\n        }\n\n        public static string ConvertEnumToString(object value)\n        {\n            var type = value.GetType();\n            var name = Enum.GetName(type, value);\n            var attribute = type.GetField(name)\n                .GetCustomAttributes(false)\n                .OfType<JsonPropertyNameAttribute>()\n                .Single();\n\n            return attribute.Name;\n        }\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":75,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp/Internals/Json/JsonEnumConverterFactory.cs#L41-L75","documentation":"Thrown by JsonEnumConverterFactory when deserializing an enum and no enum field's [JsonPropertyName] attribute matches the incoming JSON value. The converter iterates enum fields, reads each field's single JsonPropertyNameAttribute via .Single(), and if none equal the value it throws JsonException. Note .Single() itself throws InvalidOperationException if a field lacks or has multiple such attributes, and GetField(name) can be null.","triggerScenarios":"JSON contains an enum value that does not match any [JsonPropertyName] on the enum's fields; an enum field is missing the attribute; the enum was changed but the JSON sender uses old values; case or whitespace mismatch against the attribute Name.","commonSituations":"Version skew between the enum definition and the producer of the JSON; forgetting to decorate a new enum member with [JsonPropertyName]; sending the raw enum name instead of the JSON attribute name.","solutions":["Ensure every enum member has exactly one [JsonPropertyName] and the JSON sends that exact name.","Add the missing enum member / attribute for the value being sent.","Normalize JSON values (trim, exact casing) before deserialization.","If a fallback is acceptable, provide a JsonConverter that maps unknown values to a default instead of throwing."],"exampleFix":"// before\npublic enum Status { [JsonPropertyName(\"ok\")] Ok, Active } // 'Active' has no attribute; JSON \"active\" fails\n\n// after\npublic enum Status { [JsonPropertyName(\"ok\")] Ok, [JsonPropertyName(\"active\")] Active }","handlingStrategy":"validation","validationCode":"static bool IsValidEnumValue<T>(string json) where T : struct, Enum\n{\n    var valid = typeof(T).GetFields()\n        .Select(f => f.GetCustomAttributes(false).OfType<JsonPropertyNameAttribute>().SingleOrDefault()?.Name)\n        .Where(n => n != null);\n    return valid.Contains(json);\n}","typeGuard":"static bool IsValidEnumValue<T>(string json) where T : struct, Enum\n{\n    return typeof(T).GetFields()\n        .Select(f => f.GetCustomAttributes(false).OfType<JsonPropertyNameAttribute>().SingleOrDefault()?.Name)\n        .Contains(json);\n}","tryCatchPattern":"try { result = JsonSerializer.Deserialize<T>(json, options); }\ncatch (JsonException ex) when (ex.Message.Contains(\"Unable to convert Enum\"))\n{ /* map unknown value to default or reject */ }","preventionTips":["Decorate every enum member with exactly one [JsonPropertyName].","Keep JSON producers in sync with enum changes.","Add a converter that maps unknown values to a default instead of throwing."],"tags":["cefsharp","json","enum","serialization","system-text-json"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}