{"record":{"id":"65e608283ff36696","repo":"JamesNK/Newtonsoft.Json","slug":"newtonsoft-json-serialization-is-not-compatible-wi-65e608","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":"critical","filePath":"Src/Newtonsoft.Json/Schema/JsonSchema.cs","lineNumber":356,"sourceCode":"            ValidationUtils.ArgumentNotNull(writer, nameof(writer));\n            ValidationUtils.ArgumentNotNull(resolver, nameof(resolver));\n\n            JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver);\n            schemaWriter.WriteSchema(this);\n        }\n\n        /// <summary>\n        /// Returns a <see cref=\"String\"/> that represents the current <see cref=\"Object\"/>.\n        /// </summary>\n        /// <returns>\n        /// A <see cref=\"String\"/> that represents the current <see cref=\"Object\"/>.\n        /// </returns>\n        public override string ToString()\n        {\n#if HAVE_APPCONTEXT\n            if (!JToken.SerializationIsSupported)\n            {\n                throw new NotSupportedException(JToken.SerializationNotSupportedMessage);\n            }\n#endif\n            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);\n            JsonTextWriter jsonWriter = new JsonTextWriter(writer);\n            jsonWriter.Formatting = Formatting.Indented;\n\n#pragma warning disable IL2026, IL3050\n            WriteTo(jsonWriter);\n#pragma warning restore IL2026, IL3050\n\n            return writer.ToString();\n        }\n    }\n}","sourceCodeStart":338,"sourceCodeEnd":370,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Schema/JsonSchema.cs#L338-L370","documentation":"Thrown as a NotSupportedException by JsonSchema.ToString (and similarly JValue type-conversion paths) when the runtime feature switch Newtonsoft.Json.Linq.JToken.SerializationIsSupported has been set to false. This switch is turned off automatically in trimmed/Native AOT scenarios where reflection-based serialization is incompatible, so the library refuses operations that would need it.","triggerScenarios":"Calling schema.ToString() (or any path that triggers WriteTo) in an application published trimmed or with AOT where the feature switch defaults SerializationIsSupported to false. Also triggered if the switch is explicitly set to false at runtime via AppContext.SetSwitch.","commonSituations":"Publishing a project with <PublishTrimmed>true</PublishTrimmed> or <PublishReadyToRun>/<PublishAot> and then calling JSON Schema serialization; .NET MAUI / client scenarios that trim aggressively; explicitly disabling the switch to enforce AOT safety.","solutions":["Migrate JSON Schema validation/serialization to the separate Newtonsoft.Json.Schema package which is trimming/AOT-aware.","Avoid calling ToString()/WriteTo on JsonSchema in trimmed builds; serialize via a different mechanism.","If you must use it, set the feature switch to true only after verifying your app retains the required types (publish with a rd.xml or DynamicDependency)."],"exampleFix":"// before\nvar s = schema.ToString(); // throws in trimmed/AOT build\n// after\n// use Newtonsoft.Json.Schema package, or guard:\nif (JToken.SerializationIsSupported)\n{\n    var s = schema.ToString();\n}","handlingStrategy":"validation","validationCode":"static bool CanSerializeSchema()\n{\n    return JToken.SerializationIsSupported;\n}","typeGuard":null,"tryCatchPattern":"try { var s = schema.ToString(); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"SerializationIsSupported\"))\n{ /* running in trimmed/AOT; use the Newtonsoft.Json.Schema package instead */ }","preventionTips":["Gate schema serialization behind JToken.SerializationIsSupported in trimmed builds.","Migrate schema features to the dedicated Newtonsoft.Json.Schema package.","Avoid calling ToString()/WriteTo on JsonSchema under AOT."],"tags":["schema","json","trimming","aot","notsupported","feature-switch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}