{"record":{"id":"a8f15b5e41db7daf","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-boolean","errorCode":null,"errorMessage":"Cannot convert {value} to Boolean","messagePattern":"Cannot convert (.+?) to Boolean","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":274,"sourceCode":"        }\n\n        return left.Equals(right);\n    }\n\n    public static bool operator !=(JsonDynamicValue left, JsonDynamicValue right) => !(left == right);\n\n    public static bool operator <(JsonDynamicValue left, JsonDynamicValue right) => ReferenceEquals(left, null) ? !ReferenceEquals(right, null) : left.CompareTo(right) < 0;\n\n    public static bool operator <=(JsonDynamicValue left, JsonDynamicValue right) => ReferenceEquals(left, null) || left.CompareTo(right) <= 0;\n\n    public static bool operator >(JsonDynamicValue left, JsonDynamicValue right) => !ReferenceEquals(left, null) && left.CompareTo(right) > 0;\n\n    public static bool operator >=(JsonDynamicValue left, JsonDynamicValue right) => ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.CompareTo(right) >= 0;\n\n    public static explicit operator bool(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<bool>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Boolean\");\n    }\n\n    public static explicit operator bool?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<bool?>();\n    }\n\n    public static explicit operator byte(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<byte>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Byte\");\n    }\n\n    public static explicit operator byte?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<byte?>();\n    }\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L256-L292","documentation":"The explicit (bool) cast operator on JsonDynamicValue calls GetValue<bool>() on the underlying JValue and throws InvalidCastException when the value is null or not a JSON boolean. Dynamic JSON values only support the cast when the token is actually a boolean — no truthiness coercion is performed.","triggerScenarios":"Writing (bool)someJsonDynamicValue where the value is a string \"true\", a number 1, null, or any non-boolean token.","commonSituations":"Treating JSON string flags like \"true\"/\"false\" as booleans; content fields whose type changed from boolean to text; conditional checks on possibly-null dynamic properties.","solutions":["Use the explicit bool? cast or GetValue<bool?>() and treat null as false instead of the throwing non-nullable cast.","If the token is a string, parse it: bool.TryParse(value.ToString(), out var b).","Verify the JSON token type before casting (value?._jsonValue?.Type == JTokenType.Boolean).","Catch InvalidCastException if the shape of the data is uncertain."],"exampleFix":"// before\nbool enabled = (bool)part.Value; // throws for \"true\" string\n// after\nvar raw = part.Value?._jsonValue?.ToString();\nbool enabled = bool.TryParse(raw, out var b) && b;","handlingStrategy":"type-guard","validationCode":"bool IsJsonBoolean(JsonDynamicValue v) =>\n    v?._jsonValue?.Type == JTokenType.Boolean;","typeGuard":"static bool TryGetBool(JsonDynamicValue v, out bool value)\n{\n    if (v?._jsonValue?.Type == JTokenType.Boolean) { value = v._jsonValue.Value<bool>(); return true; }\n    if (bool.TryParse(v?.ToString(), out var b)) { value = b; return true; }\n    value = default; return false;\n}","tryCatchPattern":"bool enabled;\ntry { enabled = (bool)part.Value; }\ncatch (InvalidCastException) { enabled = bool.TryParse(part.Value?.ToString(), out var b) && b; }","preventionTips":["Never cast dynamic JSON values to non-nullable primitives without checking the token type first.","Prefer the bool? cast so null/absent values flow through instead of throwing.","For string flags (\"true\"/\"1\"), parse with bool.TryParse instead of casting."],"tags":["json","dynamic","cast"],"backgroundTag":"type-mismatch","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}