JamesNK/Newtonsoft.Json · error · NotSupportedException

Newtonsoft.Json support for System.ComponentModel is not com

Error message

Newtonsoft.Json support for System.ComponentModel is not compatible with trimming and has been disabled. Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported is set to false.

What it means

Thrown by JContainer's ITypedList.GetItemProperties when the System.ComponentModel integration has been disabled at runtime via an AppContext feature switch. Newtonsoft.Json gates ComponentModel features (used by data-binding/PropertyGrid scenarios) behind the switch 'Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported' because they rely on reflection that trimming/AOT cannot guarantee. When the switch is explicitly set to false (or a host/SDK sets it), any call reaching the typed-list binding surface throws NotSupportedException.

Source

Thrown at Src/Newtonsoft.Json/Linq/JContainer.cs:965

            foreach (JToken item in ChildrenTokens)
            {
                hashCode ^= item.GetDeepHashCode();
            }
            return hashCode;
        }

#if HAVE_COMPONENT_MODEL
        string ITypedList.GetListName(PropertyDescriptor[]? listAccessors)
        {
            return string.Empty;
        }

        PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[]? listAccessors)
        {
#if HAVE_APPCONTEXT
            if (!ComponentModelIsSupported)
            {
                throw new NotSupportedException(ComponentModelNotSupportedMessage);
            }
#endif
            ICustomTypeDescriptor? d = First as ICustomTypeDescriptor;

#pragma warning disable IL2026
            return d?.GetProperties() ?? new PropertyDescriptorCollection(CollectionUtils.ArrayEmpty<PropertyDescriptor>());
#pragma warning restore IL2026
        }
#endif

        #region IList<JToken> Members
        int IList<JToken>.IndexOf(JToken item)
        {
            return IndexOfItem(item);
        }

        void IList<JToken>.Insert(int index, JToken item)
        {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. If you need ComponentModel data-binding, do not trim/AOT this path and leave the switch at its default (true) or set 'Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported' to true in runtimeconfig.
  2. If you are trimming/AOT, stop using JToken nodes in ITypedList/data-binding UI; project to plain POCOs/DataTable before binding.
  3. If a host forces the switch off, deserialize into typed CLR objects instead of JObject and bind those.

Example fix

// before
var obj = JObject.Parse(json);
bindingSource.DataSource = obj; // triggers ITypedList.GetItemProperties

// after (trim/AOT-safe)
var model = JsonConvert.DeserializeObject<MyModel>(json);
bindingSource.DataSource = model;
Defensive patterns

Strategy: validation

Validate before calling

// Before binding a JContainer to a ComponentModel host, check the switch.
bool componentModelSupported =
    !AppContext.TryGetSwitch("Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported", out bool enabled)
    || enabled;
if (!componentModelSupported)
{
    throw new InvalidOperationException(
        "ComponentModel binding is disabled; project JToken to a POCO before binding.");
}

Try / catch

try { ((ITypedList)container).GetItemProperties(null); }
catch (NotSupportedException ex) when (ex.Message.Contains("ComponentModelIsSupported"))
{
    // ComponentModel disabled under trimming; fall back to POCO binding.
}

Prevention

When it happens

Trigger: Calling ITypedList.GetItemProperties on a JContainer (JObject/JArray) — typically via a BindingSource, PropertyGrid, CurrencyManager, or any Windows Forms/WPF data-binding host that queries ITypedList — after the AppContext switch 'Newtonsoft.Json.Linq.JToken.ComponentModelIsSupported' has been set to false. Occurs in trimmed/Native AOT apps or where a host forces the switch off.

Common situations: Publishing with .NET trimming or Native AOT (the SDK or a runtimeconfig flips ComponentModel off), then binding a JObject to a UI control; mixing Newtonsoft.Json.Linq nodes into a WinForms/WPF binding pipeline; upgrading to a Newtonsoft.Json version that introduced trimming switches.

Related errors


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