{"record":{"id":"0bebaefd6ce6a4dd","repo":"JamesNK/Newtonsoft.Json","slug":"unsupported-type-0-use-the-jsonserializer-clas","errorCode":null,"errorMessage":"Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.","messagePattern":"Unsupported type: (.+?)\\. Use the JsonSerializer class to get the object's JSON representation\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonConvert.cs","lineNumber":518,"sourceCode":"                    return Null;\n#endif\n#if HAVE_DATE_TIME_OFFSET\n                case PrimitiveTypeCode.DateTimeOffset:\n                    return ToString((DateTimeOffset)value);\n#endif\n                case PrimitiveTypeCode.Guid:\n                    return ToString((Guid)value);\n                case PrimitiveTypeCode.Uri:\n                    return ToString((Uri)value);\n                case PrimitiveTypeCode.TimeSpan:\n                    return ToString((TimeSpan)value);\n#if HAVE_BIG_INTEGER\n                case PrimitiveTypeCode.BigInteger:\n                    return ToStringInternal((BigInteger)value);\n#endif\n            }\n\n            throw new ArgumentException(\"Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.\".FormatWith(CultureInfo.InvariantCulture, value.GetType()));\n        }\n\n        #region Serialize\n        /// <summary>\n        /// Serializes the specified object to a JSON string.\n        /// </summary>\n        /// <param name=\"value\">The object to serialize.</param>\n        /// <returns>A JSON string representation of the object.</returns>\n        [DebuggerStepThrough]\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public static string SerializeObject(object? value)\n        {\n            return SerializeObject(value, null, (JsonSerializerSettings?)null);\n        }\n\n        /// <summary>\n        /// Serializes the specified object to a JSON string using formatting.","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonConvert.cs#L500-L536","documentation":"Thrown by JsonConvert.ToString(object) when the supplied value's runtime type is not one of the recognized primitive types (String, Char, Boolean, integral numerics, floats, DateTime, Decimal, DBNull, DateTimeOffset, Guid, Uri, TimeSpan, BigInteger). ToString is meant only for scalar/primitive conversion to a JSON literal; complex types have no case in the switch and fall through to this ArgumentException. The message itself directs you to use JsonSerializer, which understands objects, collections, converters, and contract resolution.","triggerScenarios":"Calling JsonConvert.ToString(object) directly with a non-primitive value, e.g. JsonConvert.ToString(myPoco), JsonConvert.ToString(new Dictionary<string,object>()), or JsonConvert.ToString(anEnum) on some target frameworks where enum is not a primitive type code. Also triggered indirectly by code paths that route arbitrary objects through ToString(object) rather than SerializeObject.","commonSituations":"Developers reaching for JsonConvert.ToString when they actually want JsonConvert.SerializeObject; passing enums, structs, or custom value types that are not in the primitive switch; porting code that assumed ToString(object) was a general-purpose serializer.","solutions":["Replace JsonConvert.ToString(value) with JsonConvert.SerializeObject(value) (or a JsonSerializer instance) for any non-primitive type.","If you only need a primitive literal, convert the value to a supported primitive first (e.g. enum -> (int), custom struct -> its string/number representation).","For DateTime use the dedicated JsonConvert.ToString(DateTime) overload to avoid the object dispatch."],"exampleFix":"// before\nstring json = JsonConvert.ToString(myObject);\n\n// after\nstring json = JsonConvert.SerializeObject(myObject);","handlingStrategy":"validation","validationCode":"static string SafeToString(object value)\n{\n    if (value == null) return \"null\";\n    var t = value.GetType();\n    if (ConvertUtils.GetTypeCode(t) == PrimitiveTypeCode.Object &&\n        !t.IsEnum && t != typeof(TimeSpan) && t != typeof(BigInteger))\n    {\n        return JsonConvert.SerializeObject(value);\n    }\n    return JsonConvert.ToString(value);\n}","typeGuard":"static bool IsJsonPrimitive(object value) => value switch\n{\n    null => true,\n    string or char or bool or sbyte or byte or short or ushort or int or uint\n        or long or ulong or float or double or decimal or DateTime or Guid or Uri\n        or TimeSpan or BigInteger => true,\n    _ => value.GetType().IsEnum,\n};","tryCatchPattern":null,"preventionTips":["Default to JsonConvert.SerializeObject for any value that is not a primitive literal.","Never call JsonConvert.ToString(object) on POCOs, dictionaries, or collections.","Use the strongly-typed ToString overloads (ToString(DateTime), ToString(Guid), etc.) for scalars."],"tags":["serialization","type-mismatch","primitive"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}