{"record":{"id":"475b18a051ea4618","repo":"JamesNK/Newtonsoft.Json","slug":"expected-version-object-value","errorCode":null,"errorMessage":"Expected Version object value","messagePattern":"Expected Version object value","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Converters/VersionConverter.cs","lineNumber":55,"sourceCode":"        /// <summary>\n        /// Writes the JSON representation of the object.\n        /// </summary>\n        /// <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 override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)\n        {\n            if (value == null)\n            {\n                writer.WriteNull();\n            }\n            else if (value is Version)\n            {\n                writer.WriteValue(value.ToString());\n            }\n            else\n            {\n                throw new JsonSerializationException(\"Expected Version object value\");\n            }\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 property value of the JSON that is being converted.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        /// <returns>The object value.</returns>\n        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)\n        {\n            if (reader.TokenType == JsonToken.Null)\n            {\n                return null;\n            }\n            else","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Converters/VersionConverter.cs#L37-L73","documentation":"Thrown by VersionConverter.WriteJson when the value is not null and not a System.Version. The converter writes the Version as its string representation; any other non-null runtime type reaches the else branch and throws this JsonSerializationException.","triggerScenarios":"Applying VersionConverter to a property/type whose runtime value is not a System.Version (e.g. a custom Version-like struct, a string, or a SemVer class). A polymorphic value whose declared type is Version but whose runtime type differs.","commonSituations":"Refactoring a Version property into a custom SemanticVersion type but leaving the VersionConverter attribute. Forcing VersionConverter onto a string-typed property via a contract resolver. Polymorphic serialization where the actual instance is not a Version.","solutions":["Ensure the value is a System.Version (or null) before it reaches VersionConverter.","Remove the VersionConverter attribute from properties whose type is no longer System.Version.","For a custom SemVer type, implement a dedicated JsonConverter rather than reusing VersionConverter."],"exampleFix":"// before: custom SemVer type with VersionConverter\n[JsonConverter(typeof(VersionConverter))]\npublic SemanticVersion Version { get; set; }\n\n// after: dedicated converter for the custom type\n[JsonConverter(typeof(SemVerConverter))]\npublic SemanticVersion Version { get; set; }","handlingStrategy":"type-guard","validationCode":"object value = GetValue();\nif (value != null && !(value is Version))\n    throw new InvalidOperationException($\"VersionConverter cannot serialize {value.GetType()}\");","typeGuard":"static bool IsVersion(object v) => v is Version;","tryCatchPattern":"try { conv.WriteJson(writer, value, serializer); }\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Expected Version object\"))\n{\n    throw new InvalidOperationException($\"VersionConverter got {value?.GetType()}\", ex);\n}","preventionTips":["Only attach VersionConverter to System.Version members.","For custom SemVer types, write a dedicated converter.","Re-audit converter attributes after refactoring."],"tags":["serialization","converter","version","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}