{"record":{"id":"cb0f28d32e5881d4","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-float","errorCode":null,"errorMessage":"Cannot convert {value} to Float","messagePattern":"Cannot convert (.+?) to Float","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":406,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<long?>();\n    }\n\n    public static explicit operator sbyte(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<sbyte>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to SByte\");\n    }\n\n    public static explicit operator sbyte?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<sbyte?>();\n    }\n\n    public static explicit operator float(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<float>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Float\");\n    }\n\n    public static explicit operator float?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<float?>();\n    }\n\n    public static explicit operator string?(JsonDynamicValue value)\n    {\n        return value?.ToString();\n    }\n\n    public static explicit operator ushort(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<ushort>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to UInt32\");\n    }\n","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L388-L424","documentation":"JsonDynamicValue wraps a JSON value and exposes explicit conversion operators for primitive types. The explicit operator float casts _jsonValue.GetValue<float>(); if the underlying JSON value is absent, null, or not representable as a single-precision float, the operator returns null and throws InvalidCastException with 'Cannot convert {value} to Float'. It signals that the requested numeric conversion is not possible for the wrapped JSON token.","triggerScenarios":"Casting a JsonDynamicValue instance to float with (float)jsonDynamicValue when the wrapped JSON token is null, not a number (e.g. a JSON string \"1.5\"), or a number Json.NET cannot convert to float, causing GetValue<float>() to yield null.","commonSituations":"Reading measurement fields stored as strings in JSON, casting on a JsonDynamicValue constructed from a null/default _jsonValue, or locale/producer differences where decimal values are serialized as strings instead of numbers.","solutions":["Cast to float? (explicit operator float?) instead of float, which returns null instead of throwing, then handle the null case.","Validate the JSON value with TryGetValue/GetValue and confirm it is a number before casting.","If the JSON stores the number as a string, parse it explicitly (float.TryParse with the right CultureInfo) instead of casting.","Wrap the cast in try/catch for InvalidCastException if the value is genuinely optional."],"exampleFix":"// before\nfloat ratio = (float)jsonValue; // throws when jsonValue is \"1.5\" (string)\n\n// after\nfloat? ratio = (float?)jsonValue;\nif (ratio is null) { /* handle missing/invalid value */ }","handlingStrategy":"type-guard","validationCode":"if (jsonValue is null || (jsonValue.Type != JTokenType.Float && jsonValue.Type != JTokenType.Integer))\n    throw new InvalidOperationException(\"Expected a numeric JSON value for float cast.\");","typeGuard":"static bool IsConvertibleToFloat(JsonDynamicValue v) =>\n    v?._jsonValue is { Type: JTokenType.Float or JTokenType.Integer };","tryCatchPattern":"try\n{\n    float f = (float)jsonValue;\n}\ncatch (InvalidCastException ex)\n{\n    // log and fall back to a default or skip the record\n    float f = default;\n}","preventionTips":["Use the nullable cast (float?) so non-numeric values yield null instead of throwing.","Verify the JToken type is Float or Integer before casting.","Parse string-encoded numbers explicitly with float.TryParse and the correct CultureInfo."],"tags":["invalid-cast","json","numeric-conversion","exception"],"backgroundTag":"type-mismatch","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}