JamesNK/Newtonsoft.Json · error · InvalidOperationException

Can not convert from {0} to {1}.

Error message

Can not convert from {0} to {1}.

What it means

ConvertUtils.Convert throws InvalidOperationException (ConvertUtils.cs:392-393) when TryConvertInternal returns ConvertResult.NoValidConversion: neither side is IConvertible, no special-case path matches, no TypeConverter applies, and DBNull handling doesn't apply (ConvertUtils.cs:588). There is simply no conversion between the two types.

Source

Thrown at Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:393

            CannotConvertNull = 1,
            NotInstantiableType = 2,
            NoValidConversion = 3
        }

        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            switch (TryConvertInternal(initialValue, culture, targetType, out object? value))
            {
                case ConvertResult.Success:
                    return value!;
                case ConvertResult.CannotConvertNull:
                    throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialValue.GetType(), targetType));
                case ConvertResult.NotInstantiableType:
                    throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), nameof(targetType));
                case ConvertResult.NoValidConversion:
                    throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialValue.GetType(), targetType));
                default:
                    throw new InvalidOperationException("Unexpected conversion result.");
            }
        }

        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        private static bool TryConvert(object? initialValue, CultureInfo culture, Type targetType, out object? value)
        {
            try
            {
                if (TryConvertInternal(initialValue, culture, targetType, out value) == ConvertResult.Success)
                {
                    return true;
                }

                value = null;
                return false;

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Implement a TypeConverter between the two types, or add an implicit/explicit conversion operator.
  2. Register a JsonConverter for the mismatched type that performs the mapping manually.
  3. Convert through an intermediate type that both can reach (e.g. via string).
  4. Perform the conversion yourself before invoking ConvertUtils.Convert.

Example fix

// before: A -> B with no converter -> NoValidConversion
// after: add an explicit operator on B
public static explicit operator B(A a) => new B { X = a.X };
Defensive patterns

Strategy: fallback

Validate before calling

// Probe for a conversion path before invoking Convert.
static bool HasConversion(Type from, Type to) {
    if (to.IsAssignableFrom(from)) return true;
    if (ConvertUtils.IsConvertible(from) && ConvertUtils.IsConvertible(to)) return true;
#if HAVE_TYPE_DESCRIPTOR
    var tc = System.ComponentModel.TypeDescriptor.GetConverter(from);
    if (tc != null && tc.CanConvertTo(to)) return true;
    var fc = System.ComponentModel.TypeDescriptor.GetConverter(to);
    if (fc != null && fc.CanConvertFrom(from)) return true;
#endif
    return from.GetMethod("op_Implicit", new[] { to }) != null || from.GetMethod("op_Explicit", new[] { to }) != null;
}

Type guard

static bool IsConvertiblePair(Type from, Type to) => HasConversion(from, to);

Try / catch

try { return ConvertUtils.Convert(value, culture, targetType); } catch (InvalidOperationException ex) when (ex.Message.Contains("Can not convert from")) { /* manual mapping fallback */ }

Prevention

When it happens

Trigger: Converting between two unrelated types that share no IConvertible/TypeConverter/assignment relationship and no implicit/explicit operator — e.g. a custom class to another custom class with no conversion path.

Common situations: Custom DTOs without TypeConverters or conversion operators; a TypeConverter removed after a dependency upgrade; mapping between domain types that were never wired for conversion.

Related errors


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