{"record":{"id":"7b49c9ec705ba399","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-double","errorCode":null,"errorMessage":"Cannot convert {value} to Double","messagePattern":"Cannot convert (.+?) to Double","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":340,"sourceCode":"    {\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\n    public static explicit operator Guid(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<Guid>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Guid\");\n    }\n\n    public static explicit operator Guid?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<Guid?>();\n    }\n","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L322-L358","documentation":"The explicit operator double on JsonDynamicValue converts via GetValue<double>() and throws InvalidCastException when the wrapped value is null or the JSON token cannot be represented as a double. The nullable operator (double?) exists for non-throwing access.","triggerScenarios":"Executing `(double)jsonDynamicValue` where the wrapper/_jsonValue is null or the token is a non-numeric string, boolean, object, or array.","commonSituations":"Numeric measurements in JSON stored as strings or null; dynamic access to content-item or configuration JSON where a field's type drifted; parsing third-party API payloads dynamically.","solutions":["Ensure the JSON token is a JSON number before casting","Use the nullable cast (double?) and handle null","Pre-validate with double.TryParse on value.ToString() when the token may be a numeric string","Null-check the JsonDynamicValue wrapper before casting"],"exampleFix":"// before\nvar ratio = (double)jsonDynamicValue[\"Ratio\"];\n// after\nvar ratio = (double?)jsonDynamicValue[\"Ratio\"] ?? 0d;","handlingStrategy":"type-guard","validationCode":"if (jsonDynamicValue is null) throw new InvalidOperationException(\"null wrapper\");\nvar raw = jsonDynamicValue.ToString();\nif (!double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out _))\n    throw new InvalidOperationException($\"'{raw}' is not a double\");\nvar d = (double)jsonDynamicValue;","typeGuard":"static bool TryGetDouble(JsonDynamicValue v, out double value)\n{\n    value = default;\n    if (v is null) return false;\n    var d = (double?)v;\n    if (d is null) return false;\n    value = d.Value;\n    return true;\n}","tryCatchPattern":"double ratio;\ntry\n{\n    ratio = (double)jsonDynamicValue[\"Ratio\"];\n}\ncatch (InvalidCastException ex)\n{\n    ratio = 0d;\n}","preventionTips":["Prefer (double?) casts for optional numeric fields","Keep numeric values as JSON numbers in stored data","Parse numeric strings with InvariantCulture before casting","Cover wrong-type JSON cases in tests"],"tags":["casting","invalidcastexception","json","double"],"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"}