{"record":{"id":"cc765ca51301fd66","repo":"JamesNK/Newtonsoft.Json","slug":"converter-cannot-read-json-with-the-specified-exis","errorCode":null,"errorMessage":"Converter cannot read JSON with the specified existing value. {0} is required.","messagePattern":"Converter cannot read JSON with the specified existing value\\. (.+?) is required\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonConverter.cs","lineNumber":121,"sourceCode":"        /// <param name=\"writer\">The <see cref=\"JsonWriter\"/> to write to.</param>\n        /// <param name=\"value\">The value.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        public abstract void WriteJson(JsonWriter writer, T? value, JsonSerializer serializer);\n\n        /// <summary>\n        /// Reads the JSON representation of the object.\n        /// </summary>\n        /// <param name=\"reader\">The <see cref=\"JsonReader\"/> to read from.</param>\n        /// <param name=\"objectType\">Type of the object.</param>\n        /// <param name=\"existingValue\">The existing value of object being read.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        /// <returns>The object value.</returns>\n        public sealed override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)\n        {\n            bool existingIsNull = existingValue == null;\n            if (!(existingIsNull || existingValue is T))\n            {\n                throw new JsonSerializationException(\"Converter cannot read JSON with the specified existing value. {0} is required.\".FormatWith(CultureInfo.InvariantCulture, typeof(T)));\n            }\n            return ReadJson(reader, objectType, existingIsNull ? default : (T?)existingValue, !existingIsNull, serializer);\n        }\n\n        /// <summary>\n        /// Reads the JSON representation of the object.\n        /// </summary>\n        /// <param name=\"reader\">The <see cref=\"JsonReader\"/> to read from.</param>\n        /// <param name=\"objectType\">Type of the object.</param>\n        /// <param name=\"existingValue\">The existing value of object being read. If there is no existing value then <c>null</c> will be used.</param>\n        /// <param name=\"hasExistingValue\">The existing value has a value.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        /// <returns>The object value.</returns>\n        public abstract T? ReadJson(JsonReader reader, Type objectType, T? existingValue, bool hasExistingValue, JsonSerializer serializer);\n\n        /// <summary>\n        /// Determines whether this instance can convert the specified object type.\n        /// </summary>","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonConverter.cs#L103-L139","documentation":"Thrown by the sealed override JsonConverter<T>.ReadJson when the existingValue argument is non-null but not assignable to the converter's generic type T. The base sealed method validates the existing value before forwarding it as a typed T? to the user ReadJson overload, so a mismatched existing value cannot cause an invalid cast inside the typed method. It is a JsonSerializationException naming the required type T.","triggerScenarios":"Deserialization with a JsonConverter<T> where the serializer passes an existingValue of a different type — e.g. populating an existing object whose declared type differs from T, or using JsonSerializer.Populate / a non-default current value with a converter that only accepts T. Also when CanConvert matches a type other than the one actually constructed for the existing value.","commonSituations":"Using Populate with converters; polymorphic deserialization where the runtime existing value's type does not match the converter's T; converters shared across a base type hierarchy.","solutions":["Restrict CanConvert to exactly T so the converter is not chosen for mismatched existing-value types.","Pass null/default existingValue when populating through a converter, or ensure the object being populated is of type T.","If you need broader handling, subclass JsonConverter (non-generic) and implement ReadJson with manual type checks."],"exampleFix":"// before\nvar obj = new OtherType();\nserializer.Populate(reader, obj); // existingValue type != converter T\n\n// after\nvar obj = new MyType(); // matches converter's T\nserializer.Populate(reader, obj);","handlingStrategy":"validation","validationCode":"if (existingValue != null && converter is JsonConverter<T> && existingValue is not T)\n{\n    existingValue = null; // or default(T)\n}","typeGuard":"static bool ExistingValueCompatible<T>(object existing, JsonConverter<T> _)\n    => existing is null or T;","tryCatchPattern":null,"preventionTips":["Pass null existingValue to Populate when the target is a fresh T.","Keep CanConvert narrow so converters are not applied to mismatched types.","Prefer the non-generic JsonConverter base when handling heterogeneous types."],"tags":["converter","deserialization","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}