{"record":{"id":"8460d2b341a542d1","repo":"JamesNK/Newtonsoft.Json","slug":"object-must-be-of-type-byte","errorCode":null,"errorMessage":"Object must be of type byte[].","messagePattern":"Object must be of type byte\\[\\]\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JValue.cs","lineNumber":364,"sourceCode":"\n                        return dateA.CompareTo(dateB);\n#if HAVE_DATE_TIME_OFFSET\n                    }\n                    else\n                    {\n                        DateTimeOffset offsetA = (DateTimeOffset)objA;\n                        if (!(objB is DateTimeOffset offsetB))\n                        {\n                            offsetB = new DateTimeOffset(Convert.ToDateTime(objB, CultureInfo.InvariantCulture));\n                        }\n\n                        return offsetA.CompareTo(offsetB);\n                    }\n#endif\n                case JTokenType.Bytes:\n                    if (!(objB is byte[] bytesB))\n                    {\n                        throw new ArgumentException(\"Object must be of type byte[].\");\n                    }\n\n                    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;","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JValue.cs#L346-L382","documentation":"Thrown by JValue.CompareTo (and therefore by all ordering/equality on JValue) when the token's JSON type is JTokenType.Bytes and the object being compared against is not a byte[]. Comparing two byte[] JValues compares the arrays element-wise via ByteArrayCompare; a mismatched operand type is rejected with this ArgumentException because there is no defined ordering between a byte[] and an arbitrary object.","triggerScenarios":"Comparing a JValue holding a byte[] (created from new JValue(new byte[]{1,2,3}) or parsed base64) against a non-byte[] operand, e.g. token.CompareTo(\"hello\") or token.CompareTo(42). Also when sorting/mixing a JToken list containing byte[] values with values of other types.","commonSituations":"Binary/blob columns deserialized into JValue as byte[] then compared against string or int literals; sorting heterogeneous arrays; equality checks between a base64 token and a plain string.","solutions":["Coerce both operands to the same type before comparing, e.g. compare token.Value<byte[]>() with another byte[] using a structural comparer.","Validate the operand type with a guard: if (other is byte[] b) token.CompareTo(b).","Avoid mixing JTokenType.Bytes values into collections that get sorted alongside non-byte[] tokens."],"exampleFix":"// before\nint c = byteToken.CompareTo(\"AQID\");\n\n// after\nbyte[] other = Convert.FromBase64String(\"AQID\");\nint c = byteToken.CompareTo(other);","handlingStrategy":"type-guard","validationCode":"if (other is byte[] b) { int c = byteToken.CompareTo(b); }","typeGuard":"static bool IsByteArray(object? o) => o is byte[];","tryCatchPattern":"try { int c = token.CompareTo(other); } catch (ArgumentException ex) when (ex.Message.Contains(\"byte[]\")) { /* coerce type */ }","preventionTips":["Always pass a byte[] when comparing byte-valued JValues.","Coerce operands to the matching primitive before CompareTo.","Avoid mixing JTokenType.Bytes with other types in sorted collections."],"tags":["jvalue","compareto","bytes","invalid-cast","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}