ElectronNET/Electron.NET · error · JsonException

Expected string enum value

Error message

Expected string enum value

What it means

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.

Solutions

  1. Send modifier names as strings: ["ctrl", "shift"], not numbers.
  2. Convert numeric values to their enum names on the sender side before IPC.
  3. If numeric values must be supported, extend the converter to accept numbers and map them via (ModifierType)value.
  4. Filter out null/undefined entries from the array on the JS side.

Example fix

// before
{ "modifiers": [1, 2] }
// after
{ "modifiers": ["ctrl", "shift"] }
Defensive patterns

Strategy: type-guard

Validate before calling

if (Array.isArray(payload.modifiers))
    payload.modifiers = payload.modifiers.filter(m => typeof m === 'string');

Type guard

function isStringArray(v: unknown): v is string[] {
  return Array.isArray(v) && v.every((x): x is string => typeof x === 'string' && x !== null);
}

Try / catch

try
{
    var list = JsonSerializer.Deserialize<List<ModifierType>>(json, ElectronJson.Options);
}
catch (JsonException ex) when (ex.Message.Contains("Expected string enum value"))
{
    Logger.Error("ModifierType array elements must be strings like \"ctrl\", not numbers or null.");
}

Prevention

When it happens

Trigger: Deserializing a ModifierType array containing a non-string element, e.g. "modifiers": [1, 2] or [null] or [{...}], when the converter expects ["ctrl", "alt"].

Common situations: 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.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of ElectronNET/Electron.NET@87cc6f98b6 (2026-09-14). Data as JSON: /api/errors/003f5f1fe715418f. Report an issue: GitHub.

Appendix: source

Thrown at src/ElectronNET.API/Converter/ModifierTypeListConverter.cs:30

public class ModifierTypeListConverter : JsonConverter<List<ModifierType>>
{
    public override List<ModifierType> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.Null)
        {
            return null;
        }

        var list = new List<ModifierType>();
        if (reader.TokenType != JsonTokenType.StartArray)
        {
            throw new JsonException("Expected array for ModifierType list");
        }

        while (reader.Read())
        {
            if (reader.TokenType == JsonTokenType.EndArray) break;
            if (reader.TokenType != JsonTokenType.String) throw new JsonException("Expected string enum value");
            var s = reader.GetString();
            list.Add((ModifierType)Enum.Parse(typeof(ModifierType), s, ignoreCase: true));
        }

        return list;
    }

    public override void Write(Utf8JsonWriter writer, List<ModifierType> value, JsonSerializerOptions options)
    {
        writer.WriteStartArray();
        foreach (var modifier in value)
        {
            writer.WriteStringValue(modifier.ToString().ToLowerInvariant());
        }

        writer.WriteEndArray();
    }
}

View on GitHub (pinned to 87cc6f98b6)