{"record":{"id":"69666efb5c1cf5ba","repo":"elsa-workflows/elsa-core","slug":"cannot-convert-reader-tokentype-to-bool","errorCode":null,"errorMessage":"Cannot convert {reader.TokenType} to bool","messagePattern":"Cannot convert (.+?) to bool","errorType":"validation","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Common/Converters/BooleanConverter.cs","lineNumber":22,"sourceCode":"namespace Elsa.Common.Converters;\n\npublic class BooleanConverter : JsonConverter<bool>\n{\n    public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)\n    {\n        switch (reader.TokenType)\n        {\n            case JsonTokenType.True:\n                return true;\n            case JsonTokenType.False:\n                return false;\n            case JsonTokenType.String:\n                var value = reader.GetString();\n                if (bool.TryParse(value, out var b))\n                    return b;\n                break;\n        }\n        throw new JsonException($\"Cannot convert {reader.TokenType} to bool\");\n    }\n\n    public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)\n    {\n        writer.WriteBooleanValue(value);\n    }\n}","sourceCodeStart":4,"sourceCodeEnd":29,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Common/Converters/BooleanConverter.cs#L4-L29","documentation":"BooleanConverter is a System.Text.Json JsonConverter<bool> that accepts booleans and bool-like strings (\"true\"/\"false\"). When the token is neither a JSON boolean nor a parseable string, Read throws this JsonException, naming the offending token type.","triggerScenarios":"Deserializing a JSON property bound to a bool where the token is a number (e.g. 1), null, an object, an array, or a non-boolean string like \"yes\"/\"True \" that bool.TryParse rejects.","commonSituations":"APIs that send booleans as 1/0; form data serialized as \"yes\"/\"no\"; null arriving at a non-nullable bool property; culture/ casing variations like \"TRUE\".","solutions":["Fix the payload to send a real JSON boolean (true/false) or the exact strings \"true\"/\"false\".","If the source sends 1/0, switch the property to int or write a custom converter that maps numbers to bool.","If the source sends \"yes\"/\"no\" or null, add a tolerant custom JsonConverter<bool> handling those cases.","Log the raw JSON around the failing token to identify which property is malformed."],"exampleFix":"// before\n{\"enabled\": 1}\n// after\n{\"enabled\": true}","handlingStrategy":"validation","validationCode":"if (node.ValueKind is not (JsonValueKind.True or JsonValueKind.False))\n{\n    if (node.ValueKind == JsonValueKind.String && bool.TryParse(node.GetString(), out _)) { /* ok */ }\n    else throw new FormatException($\"Expected bool, got {node.ValueKind}\");\n}","typeGuard":"bool IsJsonBool(JsonElement e) => e.ValueKind is JsonValueKind.True or JsonValueKind.False || (e.ValueKind == JsonValueKind.String && bool.TryParse(e.GetString(), out _));","tryCatchPattern":"try { var flag = JsonSerializer.Deserialize<bool>(json, options); }\ncatch (JsonException ex) { logger.LogError(ex, \"Invalid boolean in payload\"); }","preventionTips":["Contract-test payloads against your DTO schema","Normalize legacy 1/0 and yes/no booleans at the boundary","Prefer real JSON booleans over string-encoded ones"],"tags":["json","deserialization","system-text-json"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}