{"record":{"id":"7467ed913568248a","repo":"JamesNK/Newtonsoft.Json","slug":"newtonsoft-json-support-for-dynamic-is-not-compati-7467ed","errorCode":null,"errorMessage":"Newtonsoft.Json support for dynamic is not compatible with trimming and has been disabled. Newtonsoft.Json.Linq.JToken.DynamicIsSupported is set to false.","messagePattern":"Newtonsoft\\.Json support for dynamic is not compatible with trimming and has been disabled\\. Newtonsoft\\.Json\\.Linq\\.JToken\\.DynamicIsSupported is set to false\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JValue.cs","lineNumber":980,"sourceCode":"            {\n                return _value.ToString()!;\n            }\n        }\n\n#if HAVE_DYNAMIC\n        /// <summary>\n        /// Returns the <see cref=\"DynamicMetaObject\"/> responsible for binding operations performed on this object.\n        /// </summary>\n        /// <param name=\"parameter\">The expression tree representation of the runtime value.</param>\n        /// <returns>\n        /// The <see cref=\"DynamicMetaObject\"/> to bind this object.\n        /// </returns>\n        protected override DynamicMetaObject GetMetaObject(Expression parameter)\n        {\n#if HAVE_APPCONTEXT\n            if (!DynamicIsSupported)\n            {\n                throw new NotSupportedException(DynamicNotSupportedMessage);\n            }\n#endif\n#pragma warning disable IL2026, IL3050\n            return new DynamicProxyMetaObject<JValue>(parameter, this, new JValueDynamicProxy());\n#pragma warning restore IL2026, IL3050\n        }\n\n        [RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        private class JValueDynamicProxy : DynamicProxy<JValue>\n        {\n            public override bool TryConvert(JValue instance, ConvertBinder binder, [NotNullWhen(true)]out object? result)\n            {\n                if (binder.Type == typeof(JValue) || binder.Type == typeof(JToken))\n                {\n                    result = instance;\n                    return true;\n                }","sourceCodeStart":962,"sourceCodeEnd":998,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JValue.cs#L962-L998","documentation":"Same dynamic-trimming guard as the JToken version, raised from JValue.GetMetaObject (the overridden IDynamicMetaObjectProvider hook). When the runtime attempts dynamic dispatch (dynamic keyword / ConvertBinder) against a JValue and the feature switch Newtonsoft.Json.Linq.JToken.DynamicIsSupported is false, Newtonsoft throws NotSupportedException. JValue has its own JValueDynamicProxy that relies on reflection/MakeDynamic, which trimming removes, so the guard protects AOT/trimmed apps.","triggerScenarios":"dynamic dyn = jvalue; var x = dyn.SomeProperty; on a trimmed/AOT-published app, or with the AppContext switch Newtonsoft.Json.Linq.JToken.DynamicIsSupported=false. The JValue-specific proxy (TryConvert/TryGetMember) is what triggers GetMetaObject.","commonSituations":"Publishing with PublishTrimmed/PublishAot; Blazor WASM/MAUI trimmed apps; flipping the switch off after trim warnings; dynamic conversions of JValue to other types.","solutions":["Use explicit casts/indexers instead of dynamic: (int)jvalue or jvalue.Value<int>().","Deserialize to a strongly typed POCO with ToObject<T>().","If dynamic is required on a full-framework/non-trimmed build, set Newtonsoft.Json.Linq.JToken.DynamicIsSupported=true and disable trimming."],"exampleFix":"// before\ndynamic dyn = jvalue;\nint n = dyn;\n\n// after\nint n = jvalue.Value<int>();","handlingStrategy":"validation","validationCode":"// Avoid dynamic on JValue in trimmed builds; cast explicitly\nvar n = jvalue.Value<int>();","typeGuard":"static bool IsDynamicSupported => AppContext.TryGetSwitch(\"Newtonsoft.Json.Linq.JToken.DynamicIsSupported\", out bool s) ? s : true;","tryCatchPattern":"try { dynamic d = jvalue; var x = (int)d; } catch (NotSupportedException) { /* use explicit cast */ }","preventionTips":["Do not use the dynamic keyword against JValue in trimmed/AOT apps.","Prefer explicit operators and Value<T>().","Confirm PublishTrimmed/PublishAot implications before adopting dynamic."],"tags":["dynamic","trimming","native-aot","notsupported","jvalue","feature-switch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}