JamesNK/Newtonsoft.Json · error · ArgumentException
Unsupported type: {0}. Use the JsonSerializer class to get t
Error message
Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation. What it means
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.
Source
Thrown at Src/Newtonsoft.Json/JsonConvert.cs:518
return Null;
#endif
#if HAVE_DATE_TIME_OFFSET
case PrimitiveTypeCode.DateTimeOffset:
return ToString((DateTimeOffset)value);
#endif
case PrimitiveTypeCode.Guid:
return ToString((Guid)value);
case PrimitiveTypeCode.Uri:
return ToString((Uri)value);
case PrimitiveTypeCode.TimeSpan:
return ToString((TimeSpan)value);
#if HAVE_BIG_INTEGER
case PrimitiveTypeCode.BigInteger:
return ToStringInternal((BigInteger)value);
#endif
}
throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
}
#region Serialize
/// <summary>
/// Serializes the specified object to a JSON string.
/// </summary>
/// <param name="value">The object to serialize.</param>
/// <returns>A JSON string representation of the object.</returns>
[DebuggerStepThrough]
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
public static string SerializeObject(object? value)
{
return SerializeObject(value, null, (JsonSerializerSettings?)null);
}
/// <summary>
/// Serializes the specified object to a JSON string using formatting.View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before string json = JsonConvert.ToString(myObject); // after string json = JsonConvert.SerializeObject(myObject);
Defensive patterns
Strategy: validation
Validate before calling
static string SafeToString(object value)
{
if (value == null) return "null";
var t = value.GetType();
if (ConvertUtils.GetTypeCode(t) == PrimitiveTypeCode.Object &&
!t.IsEnum && t != typeof(TimeSpan) && t != typeof(BigInteger))
{
return JsonConvert.SerializeObject(value);
}
return JsonConvert.ToString(value);
} Type guard
static bool IsJsonPrimitive(object value) => value switch
{
null => true,
string or char or bool or sbyte or byte or short or ushort or int or uint
or long or ulong or float or double or decimal or DateTime or Guid or Uri
or TimeSpan or BigInteger => true,
_ => value.GetType().IsEnum,
}; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unexpected value type when writing binary: {0}
- Unexpected value when converting date. Expected DateTime or
- Expected date object value.
- Expected date object value.
- Expected Version object value
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/0bebaefd6ce6a4dd.
Report an issue: GitHub.