{"record":{"id":"3a2b1e159a38d88a","repo":"JamesNK/Newtonsoft.Json","slug":"object-must-be-of-type-timespan","errorCode":null,"errorMessage":"Object must be of type TimeSpan.","messagePattern":"Object must be of type TimeSpan\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JValue.cs","lineNumber":394,"sourceCode":"\n                    Guid guid1 = (Guid)objA;\n                    Guid guid2 = (Guid)objB;\n\n                    return guid1.CompareTo(guid2);\n                case JTokenType.Uri:\n                    Uri? uri2 = objB as Uri;\n                    if (uri2 == null)\n                    {\n                        throw new ArgumentException(\"Object must be of type Uri.\");\n                    }\n\n                    Uri uri1 = (Uri)objA;\n\n                    return Comparer<string>.Default.Compare(uri1.ToString(), uri2.ToString());\n                case JTokenType.TimeSpan:\n                    if (!(objB is TimeSpan))\n                    {\n                        throw new ArgumentException(\"Object must be of type TimeSpan.\");\n                    }\n\n                    TimeSpan ts1 = (TimeSpan)objA;\n                    TimeSpan ts2 = (TimeSpan)objB;\n\n                    return ts1.CompareTo(ts2);\n                default:\n                    throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(valueType), valueType, \"Unexpected value type: {0}\".FormatWith(CultureInfo.InvariantCulture, valueType));\n            }\n        }\n\n        private static int CompareFloat(object objA, object objB)\n        {\n            double d1 = Convert.ToDouble(objA, CultureInfo.InvariantCulture);\n            double d2 = Convert.ToDouble(objB, CultureInfo.InvariantCulture);\n\n            // take into account possible floating point errors\n            if (MathUtils.ApproxEquals(d1, d2))","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JValue.cs#L376-L412","documentation":"Thrown by JValue.CompareTo when the token's JSON type is JTokenType.TimeSpan and the operand is not a TimeSpan. TimeSpan JValues are compared with TimeSpan.CompareTo; a non-TimeSpan operand is rejected. This is the last of the four type-strict primitive comparisons (Bytes/Guid/Uri/TimeSpan) inside CompareTo.","triggerScenarios":"Comparing a TimeSpan JValue against a string such as token.CompareTo(\"00:01:00\") or an integer number of ticks. Triggered during sorting/equality on token collections that contain duration fields.","commonSituations":"Duration/delay fields deserialized as TimeSpan then compared to string or numeric literals; mixing TimeSpan and string duration representations.","solutions":["Parse the operand to TimeSpan before comparing: token.CompareTo(TimeSpan.Parse(text)).","Compare the underlying values: token.Value<TimeSpan>() vs TimeSpan.FromSeconds(n).","Type-check the operand before invoking CompareTo."],"exampleFix":"// before\nint c = tsToken.CompareTo(\"00:05:00\");\n\n// after\nint c = tsToken.CompareTo(TimeSpan.Parse(\"00:05:00\"));","handlingStrategy":"type-guard","validationCode":"if (other is TimeSpan ts) { int c = token.CompareTo(ts); } else if (other is string s) { int c = token.CompareTo(TimeSpan.Parse(s)); }","typeGuard":"static bool IsTimeSpan(object? o) => o is TimeSpan;","tryCatchPattern":"try { int c = token.CompareTo(other); } catch (ArgumentException ex) when (ex.Message.Contains(\"TimeSpan\")) { /* parse string to TimeSpan */ }","preventionTips":["Parse duration strings to TimeSpan before comparing.","Standardize on TimeSpan for duration fields.","Type-check operands in heterogeneous comparisons."],"tags":["jvalue","compareto","timespan","invalid-cast","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}