JamesNK/Newtonsoft.Json · critical · NotSupportedException

Newtonsoft.Json serialization is not compatible with trimmin

Error message

Newtonsoft.Json serialization is not compatible with trimming and has been disabled. Newtonsoft.Json.Linq.JToken.SerializationIsSupported is set to false.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Schema/JsonSchema.cs:356

            ValidationUtils.ArgumentNotNull(writer, nameof(writer));
            ValidationUtils.ArgumentNotNull(resolver, nameof(resolver));

            JsonSchemaWriter schemaWriter = new JsonSchemaWriter(writer, resolver);
            schemaWriter.WriteSchema(this);
        }

        /// <summary>
        /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="String"/> that represents the current <see cref="Object"/>.
        /// </returns>
        public override string ToString()
        {
#if HAVE_APPCONTEXT
            if (!JToken.SerializationIsSupported)
            {
                throw new NotSupportedException(JToken.SerializationNotSupportedMessage);
            }
#endif
            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
            JsonTextWriter jsonWriter = new JsonTextWriter(writer);
            jsonWriter.Formatting = Formatting.Indented;

#pragma warning disable IL2026, IL3050
            WriteTo(jsonWriter);
#pragma warning restore IL2026, IL3050

            return writer.ToString();
        }
    }
}

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Migrate JSON Schema validation/serialization to the separate Newtonsoft.Json.Schema package which is trimming/AOT-aware.
  2. Avoid calling ToString()/WriteTo on JsonSchema in trimmed builds; serialize via a different mechanism.
  3. 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).

Example fix

// before
var s = schema.ToString(); // throws in trimmed/AOT build
// after
// use Newtonsoft.Json.Schema package, or guard:
if (JToken.SerializationIsSupported)
{
    var s = schema.ToString();
}
Defensive patterns

Strategy: validation

Validate before calling

static bool CanSerializeSchema()
{
    return JToken.SerializationIsSupported;
}

Try / catch

try { var s = schema.ToString(); }
catch (NotSupportedException ex) when (ex.Message.Contains("SerializationIsSupported"))
{ /* running in trimmed/AOT; use the Newtonsoft.Json.Schema package instead */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/65e608283ff36696. Report an issue: GitHub.