{"record":{"id":"a2341b8637404594","repo":"JamesNK/Newtonsoft.Json","slug":"newtonsoft-json-serialization-is-not-compatible-wi","errorCode":null,"errorMessage":"Newtonsoft.Json serialization is not compatible with trimming and has been disabled. Newtonsoft.Json.Linq.JToken.SerializationIsSupported is set to false.","messagePattern":"Newtonsoft\\.Json serialization is not compatible with trimming and has been disabled\\. Newtonsoft\\.Json\\.Linq\\.JToken\\.SerializationIsSupported is set to false\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JValue.cs","lineNumber":1206,"sourceCode":"            return (double)this;\n        }\n\n        decimal IConvertible.ToDecimal(IFormatProvider? provider)\n        {\n            return (decimal)this;\n        }\n\n        DateTime IConvertible.ToDateTime(IFormatProvider? provider)\n        {\n            return (DateTime)this;\n        }\n\n        object IConvertible.ToType(Type conversionType, IFormatProvider? provider)\n        {\n#if HAVE_APPCONTEXT\n            if (!SerializationIsSupported)\n            {\n                throw new NotSupportedException(SerializationNotSupportedMessage);\n            }\n#endif\n#pragma warning disable IL2026, IL3050\n            return ToObject(conversionType)!;\n#pragma warning restore IL2026, IL3050\n        }\n#endif\n    }\n}\n","sourceCodeStart":1188,"sourceCodeEnd":1216,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JValue.cs#L1188-L1216","documentation":"Thrown from JValue's IConvertible.ToType implementation when Convert.ChangeType (or any IConvertible consumer) routes to JValue.ToType on a trimmed/AOT build where the feature switch Newtonsoft.Json.Linq.JToken.SerializationIsSupported is false. ToType internally calls ToObject(conversionType), which requires reflection-based serialization that trimming removes; the guard rejects it with NotSupportedException instead of producing a trim-induced failure at runtime.","triggerScenarios":"Calling Convert.ChangeType(jvalue, typeof(SomeType)) or any code path that invokes the IConvertible.ToType route on a JValue, in a trimmed/AOT-published app or with Newtonsoft.Json.Linq.JToken.SerializationIsSupported=false. Less commonly, direct casts routed through IConvertible.","commonSituations":"Native AOT / trimmed self-contained deployments; MAUI/Blazor WASM; manually setting the switch off after seeing IL2026 warnings; generic conversion helpers that go through Convert.ChangeType.","solutions":["Use the AOT-safe explicit API: jvalue.ToObject<T>() annotated appropriately, or cast via the explicit operators ((int)jvalue).","Deserialize via JsonSerializer with source-generated context instead of IConvertible routing.","If IConvertible routing is mandatory on a non-trimmed build, set Newtonsoft.Json.Linq.JToken.SerializationIsSupported=true and disable trimming."],"exampleFix":"// before\nvar dt = (DateTime)Convert.ChangeType(jvalue, typeof(DateTime));\n\n// after\nvar dt = (DateTime)jvalue;\n// or\nvar dt = jvalue.ToObject<DateTime>();","handlingStrategy":"validation","validationCode":"var v = (DateTime)jvalue; // use explicit cast instead of Convert.ChangeType","typeGuard":"static bool IsSerializationSupported => AppContext.TryGetSwitch(\"Newtonsoft.Json.Linq.JToken.SerializationIsSupported\", out bool s) ? s : true;","tryCatchPattern":"try { var v = Convert.ChangeType(jvalue, typeof(T)); } catch (NotSupportedException) { /* fall back to explicit cast */ }","preventionTips":["Avoid Convert.ChangeType on JValue in trimmed/AOT apps; use explicit casts.","Use ToObject<T>() with source-generated serializer context for AOT.","Audit code for IConvertible.ToType usage before enabling trimming."],"tags":["iconvertible","trimming","native-aot","serialization","notsupported","jvalue"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}