{"record":{"id":"e9fc9d481010090b","repo":"JamesNK/Newtonsoft.Json","slug":"value-is-not-a-jtoken","errorCode":null,"errorMessage":"Value is not a JToken.","messagePattern":"Value is not a JToken\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs","lineNumber":69,"sourceCode":"            _contract = contract;\n            _member = member;\n        }\n\n        private T GetTokenValue<T>(object value)\n        {\n            ValidationUtils.ArgumentNotNull(value, nameof(value));\n\n            JValue v = (JValue)value;\n            return (T)System.Convert.ChangeType(v.Value, typeof(T), CultureInfo.InvariantCulture)!;\n        }\n\n        public object Convert(object value, Type type)\n        {\n            ValidationUtils.ArgumentNotNull(value, nameof(value));\n\n            if (!(value is JToken token))\n            {\n                throw new ArgumentException(\"Value is not a JToken.\", nameof(value));\n            }\n\n            return _reader.CreateISerializableItem(token, type, _contract, _member)!;\n        }\n\n        public object Convert(object value, TypeCode typeCode)\n        {\n            ValidationUtils.ArgumentNotNull(value, nameof(value));\n\n            object? resolvedValue = (value is JValue v) ? v.Value : value;\n\n            return System.Convert.ChangeType(resolvedValue, typeCode, CultureInfo.InvariantCulture)!;\n        }\n\n        public bool ToBoolean(object value)\n        {\n            return GetTokenValue<bool>(value);\n        }","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/JsonFormatterConverter.cs#L51-L87","documentation":"Thrown by JsonFormatterConverter.Convert(object, Type) when the value handed to the IFormatterConverter is not a JToken. JsonFormatterConverter is the bridge used while deserializing ISerializable types (types implementing ISerializable with a serialization constructor); each value passed to the ISerializable constructor is expected to be a JToken parsed from the JSON stream.","triggerScenarios":"Deserializing a type that implements ISerializable and whose SerializationInfo values are not JTokens. This is an internal-protocol error, typically caused by a custom JsonConverter or ISerializable implementation that hands non-JToken objects into the converter, or by an internal pipeline mismatch during ISerializable population.","commonSituations":"Mixing ISerializable types with custom converters, partial deserialization via JToken.ReadFrom creating non-token inputs, edge cases with $values / typewriting during ISerializable round-tripping, version mismatches where an ISerializable contract is reused incorrectly.","solutions":["Avoid implementing ISerializable for types you serialize as JSON; prefer plain POCOs with [JsonObject]/[JsonProperty].","If ISerializable is required, ensure the serialization stream contains native JSON values that become JTokens.","Remove or adjust any custom JsonConverter that reads ISerializable types and injects non-JToken values.","Report a repro if reached through pure library code without custom converters."],"exampleFix":"// before: ISerializable type with custom converter injecting non-JToken\n[JsonConverter(typeof(MyBadConverter))]\n[Serializable] public class Foo : ISerializable { ... }\n// after: drop ISerializable, use POCO\npublic class Foo { public int Id { get; set; } public string Name { get; set; } }","handlingStrategy":"validation","validationCode":"if (!(value is Newtonsoft.Json.Linq.JToken)) throw new ArgumentException(\"expected a JToken\");","typeGuard":"static bool IsJToken(object value) => value is Newtonsoft.Json.Linq.JToken;","tryCatchPattern":"try { JsonConvert.DeserializeObject<T>(json); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Value is not a JToken\")) {\n    logger.Error(ex, \"ISerializable payload contained non-JToken value; review custom converters.\"); throw;\n}","preventionTips":["Avoid implementing ISerializable on types you serialize as JSON.","Don't write custom JsonConverters that re-enter ISerializable deserialization with raw objects.","Keep ISerializable round-tripping inside Json.NET's native pipeline.","Test ISerializable types with full serialize/deserialize round-trips."],"tags":["deserialization","iserializable","formatter-converter","jtoken"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}