{"record":{"id":"2f0083c1bb25a667","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-decimal","errorCode":null,"errorMessage":"Cannot convert {value} to Decimal","messagePattern":"Cannot convert (.+?) to Decimal","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":329,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<DateTime?>();\n    }\n\n    public static explicit operator DateTimeOffset(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<DateTimeOffset>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to DateTimeOffset\");\n    }\n\n    public static explicit operator DateTimeOffset?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<DateTimeOffset?>();\n    }\n\n    public static explicit operator decimal(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<decimal>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Decimal\");\n    }\n\n    public static explicit operator decimal?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<decimal?>();\n    }\n\n    public static explicit operator double(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<double>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Double\");\n    }\n\n    public static explicit operator double?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<double?>();\n    }\n","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L311-L347","documentation":"The explicit operator decimal on JsonDynamicValue converts the wrapped JSON value with GetValue<decimal>() and throws InvalidCastException when conversion yields null or the JSON token is not numeric-decimal-compatible. It provides a descriptive message rather than letting the JSON library surface a vaguer failure.","triggerScenarios":"Executing `(decimal)jsonDynamicValue` where the wrapper/_jsonValue is null, or the token is a non-numeric string, boolean, object, or array (numbers stored as strings also fail).","commonSituations":"Money/quantity fields in JSON content that are null, empty strings, or strings like \"12,50\" with locale-specific formatting; columns that changed from number to string in stored JSON; dynamic query results.","solutions":["Confirm the JSON token is a JSON number (or an invariant numeric string accepted by the JSON parser)","Use the nullable cast (decimal?) and coalesce/handle null","Convert explicitly with decimal.TryParse(value.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var d) when values may be numeric strings","Null-check the wrapper before casting"],"exampleFix":"// before\nvar price = (decimal)jsonDynamicValue[\"Price\"];\n// after\nvar price = (decimal?)jsonDynamicValue[\"Price\"] ?? 0m;","handlingStrategy":"type-guard","validationCode":"if (jsonDynamicValue is null) throw new InvalidOperationException(\"null wrapper\");\nvar raw = jsonDynamicValue.ToString();\nif (!decimal.TryParse(raw, NumberStyles.Number, CultureInfo.InvariantCulture, out _))\n    throw new InvalidOperationException($\"'{raw}' is not a decimal\");\nvar d = (decimal)jsonDynamicValue;","typeGuard":"static bool TryGetDecimal(JsonDynamicValue v, out decimal value)\n{\n    value = default;\n    if (v is null) return false;\n    var d = (decimal?)v;\n    if (d is null) return false;\n    value = d.Value;\n    return true;\n}","tryCatchPattern":"decimal amount;\ntry\n{\n    amount = (decimal)jsonDynamicValue[\"Price\"];\n}\ncatch (InvalidCastException ex)\n{\n    amount = 0m;\n}","preventionTips":["Store numbers as JSON numbers, not formatted strings","Use InvariantCulture when converting numeric strings manually","Use (decimal?) when null is acceptable","Validate numeric fields at ingestion time"],"tags":["casting","invalidcastexception","json","decimal"],"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"}