{"record":{"id":"d0d58260e2b27a52","repo":"elsa-workflows/elsa-core","slug":"cannot-convert-reader-tokentype-to-bool-d0d582","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/NullableBooleanConverter.cs","lineNumber":24,"sourceCode":"public class NullableBooleanConverter : 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            case JsonTokenType.Null:\n                return null;\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        if (value.HasValue)\n            writer.WriteBooleanValue(value.Value);\n        else\n            writer.WriteNullValue();\n    }\n}","sourceCodeStart":6,"sourceCodeEnd":34,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Common/Converters/NullableBooleanConverter.cs#L6-L34","documentation":"NullableBooleanConverter converts JSON booleans, parseable \"true\"/\"false\" strings, and JSON null into bool?. Any other token type (number, object, array, unparseable string) causes Read to throw this JsonException naming the token type.","triggerScenarios":"Deserializing a bool? property from a JSON number (1), a non-boolean string (\"yes\"), an object, or an array; JSON null is fine and yields null.","commonSituations":"Numeric flag encodings from legacy APIs; localized boolean spellings; accidentally mapping an object property to a bool? field in a model.","solutions":["Send true, false, null, or the strings \"true\"/\"false\".","If the source emits 1/0, extend or replace the converter to map numbers to booleans.","If the source emits \"yes\"/\"no\", pre-map the payload or use a custom converter with a lookup.","Check the failing property name via the serializer path in the exception to fix the right field."],"exampleFix":"// before\n{\"archived\": 0}\n// after\n{\"archived\": false}","handlingStrategy":"validation","validationCode":"var ok = node.ValueKind is JsonValueKind.True or JsonValueKind.False or JsonValueKind.Null\n    || (node.ValueKind == JsonValueKind.String && bool.TryParse(node.GetString(), out _));\nif (!ok) throw new FormatException($\"Expected bool?/null, got {node.ValueKind}\");","typeGuard":"bool IsNullableJsonBool(JsonElement e) => e.ValueKind is JsonValueKind.Null or JsonValueKind.True or JsonValueKind.False || (e.ValueKind == JsonValueKind.String && bool.TryParse(e.GetString(), out _));","tryCatchPattern":"try { flag = JsonSerializer.Deserialize<bool?>(json, options); }\ncatch (JsonException ex) { logger.LogWarning(ex, \"Skipping invalid nullable boolean\"); flag = null; }","preventionTips":["Map numeric flags (0/1) to booleans before deserialization","Document the accepted string forms (\"true\"/\"false\") in your API contract","Use null only to mean 'absent', not false"],"tags":["json","deserialization","nullable"],"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-23T08:17:48.524Z"}