{"record":{"id":"0b328f0e75b7a54b","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-from-0-to-1","errorCode":null,"errorMessage":"Can not convert from {0} to {1}.","messagePattern":"Can not convert from (.+?) to (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/ConvertUtils.cs","lineNumber":393,"sourceCode":"            CannotConvertNull = 1,\n            NotInstantiableType = 2,\n            NoValidConversion = 3\n        }\n\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public static object Convert(object initialValue, CultureInfo culture, Type targetType)\n        {\n            switch (TryConvertInternal(initialValue, culture, targetType, out object? value))\n            {\n                case ConvertResult.Success:\n                    return value!;\n                case ConvertResult.CannotConvertNull:\n                    throw new Exception(\"Can not convert null {0} into non-nullable {1}.\".FormatWith(CultureInfo.InvariantCulture, initialValue.GetType(), targetType));\n                case ConvertResult.NotInstantiableType:\n                    throw new ArgumentException(\"Target type {0} is not a value type or a non-abstract class.\".FormatWith(CultureInfo.InvariantCulture, targetType), nameof(targetType));\n                case ConvertResult.NoValidConversion:\n                    throw new InvalidOperationException(\"Can not convert from {0} to {1}.\".FormatWith(CultureInfo.InvariantCulture, initialValue.GetType(), targetType));\n                default:\n                    throw new InvalidOperationException(\"Unexpected conversion result.\");\n            }\n        }\n\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        private static bool TryConvert(object? initialValue, CultureInfo culture, Type targetType, out object? value)\n        {\n            try\n            {\n                if (TryConvertInternal(initialValue, culture, targetType, out value) == ConvertResult.Success)\n                {\n                    return true;\n                }\n\n                value = null;\n                return false;","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs#L375-L411","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom DTOs without TypeConverters or conversion operators; a TypeConverter removed after a dependency upgrade; mapping between domain types that were never wired for conversion.","solutions":["Implement a TypeConverter between the two types, or add an implicit/explicit conversion operator.","Register a JsonConverter for the mismatched type that performs the mapping manually.","Convert through an intermediate type that both can reach (e.g. via string).","Perform the conversion yourself before invoking ConvertUtils.Convert."],"exampleFix":"// before: A -> B with no converter -> NoValidConversion\n// after: add an explicit operator on B\npublic static explicit operator B(A a) => new B { X = a.X };","handlingStrategy":"fallback","validationCode":"// Probe for a conversion path before invoking Convert.\nstatic bool HasConversion(Type from, Type to) {\n    if (to.IsAssignableFrom(from)) return true;\n    if (ConvertUtils.IsConvertible(from) && ConvertUtils.IsConvertible(to)) return true;\n#if HAVE_TYPE_DESCRIPTOR\n    var tc = System.ComponentModel.TypeDescriptor.GetConverter(from);\n    if (tc != null && tc.CanConvertTo(to)) return true;\n    var fc = System.ComponentModel.TypeDescriptor.GetConverter(to);\n    if (fc != null && fc.CanConvertFrom(from)) return true;\n#endif\n    return from.GetMethod(\"op_Implicit\", new[] { to }) != null || from.GetMethod(\"op_Explicit\", new[] { to }) != null;\n}","typeGuard":"static bool IsConvertiblePair(Type from, Type to) => HasConversion(from, to);","tryCatchPattern":"try { return ConvertUtils.Convert(value, culture, targetType); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Can not convert from\")) { /* manual mapping fallback */ }","preventionTips":["Implement a TypeConverter or implicit/explicit operator for custom type pairs.","Register a JsonConverter for types without a built-in conversion.","Convert through an intermediate (e.g. string) when no direct path exists."],"tags":["conversion","type-mismatch","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}