JamesNK/Newtonsoft.Json · error · ArgumentNullException

Serialization binder cannot be null.

Error message

Serialization binder cannot be null.

What it means

Thrown by the obsolete JsonSerializer.Binder setter when assigning null. The legacy SerializationBinder is required for type-name resolution during $type-polymorphic serialization; a null binder would break type binding later, so the setter fails fast with ArgumentNullException. Use the SerializationBinder (ISerializationBinder) property for new code.

Source

Thrown at Src/Newtonsoft.Json/JsonSerializer.cs:129

            get
            {
                if (_serializationBinder is SerializationBinder legacySerializationBinder)
                {
                    return legacySerializationBinder;
                }

                if (_serializationBinder is SerializationBinderAdapter adapter)
                {
                    return adapter.SerializationBinder;
                }

                throw new InvalidOperationException("Cannot get SerializationBinder because an ISerializationBinder was previously set.");
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value), "Serialization binder cannot be null.");
                }

                _serializationBinder = value as ISerializationBinder ?? new SerializationBinderAdapter(value);
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="ISerializationBinder"/> used by the serializer when resolving type names.
        /// </summary>
        public virtual ISerializationBinder SerializationBinder
        {
            get => _serializationBinder;
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException(nameof(value), "Serialization binder cannot be null.");
                }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Use the non-obsolete SerializationBinder property and assign a concrete ISerializationBinder (e.g. DefaultSerializationBinder.Instance).
  2. To reset to default behaviour, do not null the binder; create a fresh JsonSerializer or assign DefaultSerializationBinder.Instance.
  3. Guard config/DI: serializer.Binder = binder ?? DefaultSerializationBinder.Instance;.

Example fix

// before
serializer.Binder = null;

// after
serializer.SerializationBinder = DefaultSerializationBinder.Instance;
Defensive patterns

Strategy: validation

Validate before calling

serializer.Binder = (binder ?? DefaultSerializationBinder.Instance) as SerializationBinder
    ?? throw new ArgumentException("Use SerializationBinder property for ISerializationBinder.");

Type guard

static bool IsLegacyBinder(object b) => b is SerializationBinder;

Prevention

When it happens

Trigger: Assigning serializer.Binder = null; clearing the binder via null; DI/config supplying null for the legacy binder property.

Common situations: Migrating off the obsolete API and nulling it for 'default'; DI returning null; tests stubbing null binders.

Related errors


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