{"record":{"id":"1939851c447e1eb8","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-timespan","errorCode":null,"errorMessage":"Cannot convert {value} to TimeSpan","messagePattern":"Cannot convert (.+?) to TimeSpan","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":469,"sourceCode":"\n    public static explicit operator byte[]?(JsonDynamicValue value)\n    {\n        if (value?._jsonValue.GetObjectValue() is string str)\n        {\n            return Convert.FromBase64String(str);\n        }\n\n        throw new InvalidCastException($\"Cannot convert {value} to Byte array\");\n    }\n\n    public static explicit operator TimeSpan(JsonDynamicValue value)\n    {\n        if (value?._jsonValue?.GetObjectValue() is string str)\n        {\n            return TimeSpan.Parse(str, CultureInfo.InvariantCulture);\n        }\n\n        throw new InvalidCastException($\"Cannot convert {value} to TimeSpan\");\n    }\n\n    public static explicit operator TimeSpan?(JsonDynamicValue value)\n    {\n        var str = value?._jsonValue?.GetObjectValue() as string;\n\n        return str is not null\n            ? TimeSpan.Parse(str, CultureInfo.InvariantCulture)\n            : null;\n    }\n\n    public static explicit operator Uri?(JsonDynamicValue value)\n    {\n        return new(value?._jsonValue?.GetValue<string>() ?? string.Empty);\n    }\n\n    public static implicit operator JsonDynamicValue(bool value)\n    {","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L451-L487","documentation":"This InvalidCastException is thrown by the explicit TimeSpan conversion operator on JsonDynamicValue when the wrapped JSON value is not a string, since TimeSpan is parsed from its string representation using TimeSpan.Parse with invariant culture. Non-string JSON values (numbers, objects, null) cannot be converted.","triggerScenarios":"Casting a JsonDynamicValue with (TimeSpan) when the wrapped value is a number, object, array, or null instead of a duration string like '1.02:03:04'.","commonSituations":"Durations serialized as milliseconds/seconds by APIs; null fields; strings not in an invariant-culture parseable TimeSpan format will fail inside TimeSpan.Parse.","solutions":["Ensure the JSON field is a string in TimeSpan.Parse format (e.g. 'hh:mm:ss' or 'd.hh:mm:ss')","Get the string first and parse with TimeSpan.TryParse yourself","Validate with GetObjectValue() is string before casting","Convert numeric durations (e.g. seconds) manually before casting"],"exampleFix":"// before\nvar ts = (TimeSpan)dynamicValue;\n// after\nif (dynamicValue?.GetObjectValue() is string s && TimeSpan.TryParse(s, CultureInfo.InvariantCulture, out var ts))\n{\n    // use ts\n}","handlingStrategy":"type-guard","validationCode":"if (value?.GetObjectValue() is not string ts || !TimeSpan.TryParse(ts, CultureInfo.InvariantCulture, out _))\n    throw new InvalidOperationException(\"JSON value is not a parseable TimeSpan string\");","typeGuard":"static bool IsTimeSpanString(JsonDynamicValue? v) =>\n    v?.GetObjectValue() is string s && TimeSpan.TryParse(s, CultureInfo.InvariantCulture, out _);","tryCatchPattern":"TimeSpan ts;\ntry { ts = (TimeSpan)dynamicValue; }\ncatch (InvalidCastException ex) { /* log, fallback */ ts = TimeSpan.Zero; }","preventionTips":["Store durations as invariant-culture TimeSpan strings in JSON","Convert numeric durations (seconds/milliseconds) to TimeSpan manually before casting","Use TimeSpan.TryParse on your side instead of the throwing cast","Handle JSON null explicitly"],"tags":["invalid-cast","json","timespan","parsing"],"backgroundTag":"incompatible-source-type","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"}