{"record":{"id":"003f5f1fe715418f","repo":"ElectronNET/Electron.NET","slug":"expected-string-enum-value","errorCode":null,"errorMessage":"Expected string enum value","messagePattern":"Expected string enum value","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/ElectronNET.API/Converter/ModifierTypeListConverter.cs","lineNumber":30,"sourceCode":"public class ModifierTypeListConverter : JsonConverter<List<ModifierType>>\n{\n    public override List<ModifierType> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)\n    {\n        if (reader.TokenType == JsonTokenType.Null)\n        {\n            return null;\n        }\n\n        var list = new List<ModifierType>();\n        if (reader.TokenType != JsonTokenType.StartArray)\n        {\n            throw new JsonException(\"Expected array for ModifierType list\");\n        }\n\n        while (reader.Read())\n        {\n            if (reader.TokenType == JsonTokenType.EndArray) break;\n            if (reader.TokenType != JsonTokenType.String) throw new JsonException(\"Expected string enum value\");\n            var s = reader.GetString();\n            list.Add((ModifierType)Enum.Parse(typeof(ModifierType), s, ignoreCase: true));\n        }\n\n        return list;\n    }\n\n    public override void Write(Utf8JsonWriter writer, List<ModifierType> value, JsonSerializerOptions options)\n    {\n        writer.WriteStartArray();\n        foreach (var modifier in value)\n        {\n            writer.WriteStringValue(modifier.ToString().ToLowerInvariant());\n        }\n\n        writer.WriteEndArray();\n    }\n}","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/ElectronNET/Electron.NET/blob/87cc6f98b6a9c5968d7846c0e775ea713bd0d7b2/src/ElectronNET.API/Converter/ModifierTypeListConverter.cs#L12-L48","documentation":"Inside ModifierTypeListConverter.Read, after confirming a JSON array, each element must be a string that is parsed via Enum.Parse into ModifierType (case-insensitive). If an element is not a JSON string (e.g. a number or object), a JsonException \"Expected string enum value\" is thrown, since numeric or structural tokens are not valid ModifierType names here.","triggerScenarios":"Deserializing a ModifierType array containing a non-string element, e.g. \"modifiers\": [1, 2] or [null] or [{...}], when the converter expects [\"ctrl\", \"alt\"].","commonSituations":"JS side sent numeric keyCode/bitmask values instead of modifier names; null entries in the array; a schema/contract mismatch where the sender assumed numeric enum serialization while the converter demands string names.","solutions":["Send modifier names as strings: [\"ctrl\", \"shift\"], not numbers.","Convert numeric values to their enum names on the sender side before IPC.","If numeric values must be supported, extend the converter to accept numbers and map them via (ModifierType)value.","Filter out null/undefined entries from the array on the JS side."],"exampleFix":"// before\n{ \"modifiers\": [1, 2] }\n// after\n{ \"modifiers\": [\"ctrl\", \"shift\"] }","handlingStrategy":"type-guard","validationCode":"if (Array.isArray(payload.modifiers))\n    payload.modifiers = payload.modifiers.filter(m => typeof m === 'string');","typeGuard":"function isStringArray(v: unknown): v is string[] {\n  return Array.isArray(v) && v.every((x): x is string => typeof x === 'string' && x !== null);\n}","tryCatchPattern":"try\n{\n    var list = JsonSerializer.Deserialize<List<ModifierType>>(json, ElectronJson.Options);\n}\ncatch (JsonException ex) when (ex.Message.Contains(\"Expected string enum value\"))\n{\n    Logger.Error(\"ModifierType array elements must be strings like \\\"ctrl\\\", not numbers or null.\");\n}","preventionTips":["Use string enum names end-to-end; never send numeric bitmasks for modifiers.","Type the array as ModifierType[] (union of string literals) in the renderer.","Strip null/undefined entries before sending IPC messages.","Document the expected wire format next to the converter."],"tags":["json","deserialization","enum","converter"],"backgroundTag":"invalid-enum-value","analyzedSha":"87cc6f98b6a9c5968d7846c0e775ea713bd0d7b2","analyzedAt":"2026-09-14T03:09:47.608Z","contentChangedAt":"2026-09-14T03:09:47.608Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}