{"record":{"id":"e0199dbfc0b6be6e","repo":"JamesNK/Newtonsoft.Json","slug":"object-must-be-of-type-uri","errorCode":null,"errorMessage":"Object must be of type Uri.","messagePattern":"Object must be of type Uri\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JValue.cs","lineNumber":385,"sourceCode":"                    byte[]? bytesA = objA as byte[];\n                    MiscellaneousUtils.Assert(bytesA != null);\n\n                    return MiscellaneousUtils.ByteArrayCompare(bytesA!, bytesB);\n                case JTokenType.Guid:\n                    if (!(objB is Guid))\n                    {\n                        throw new ArgumentException(\"Object must be of type Guid.\");\n                    }\n\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            }","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JValue.cs#L367-L403","documentation":"Thrown by JValue.CompareTo when the token's JSON type is JTokenType.Uri and the operand is not a Uri. Uri JValues compare their underlying string forms with Comparer<string>; an operand that cannot be treated as a Uri is rejected. Note the operand is tested with `as Uri` so derived Uri types are accepted but strings are not.","triggerScenarios":"Comparing a Uri JValue (created from new JValue(new Uri(\"https://x\"))) against a string literal such as token.CompareTo(\"https://x\"), or against any non-Uri object. Happens during sorting or equality on token collections.","commonSituations":"Storing URL fields as Uri-typed JValues and then comparing against string constants in LINQ-to-JSON code; mixing Uri and string representations of the same URL.","solutions":["Wrap the operand in a Uri: token.CompareTo(new Uri(text)).","Compare the string forms directly: ((Uri)token.Value<Uri>()).ToString() == text.","Standardize on a single representation (Uri or string) for URI-typed fields."],"exampleFix":"// before\nint c = uriToken.CompareTo(\"https://example.com\");\n\n// after\nint c = uriToken.CompareTo(new Uri(\"https://example.com\"));","handlingStrategy":"type-guard","validationCode":"if (other is Uri u) { int c = uriToken.CompareTo(u); } else if (other is string s) { int c = uriToken.CompareTo(new Uri(s)); }","typeGuard":"static bool IsUri(object? o) => o is Uri;","tryCatchPattern":"try { int c = token.CompareTo(other); } catch (ArgumentException ex) when (ex.Message.Contains(\"Uri\")) { /* wrap in Uri */ }","preventionTips":["Wrap string operands in new Uri(...) before comparing Uri JValues.","Standardize on Uri for URI-typed fields.","Type-check operands before CompareTo."],"tags":["jvalue","compareto","uri","invalid-cast","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}