diff --git a/src/libraries/System.Private.DataContractSerialization/src/ILLink/ILLink.Suppressions.xml b/src/libraries/System.Private.DataContractSerialization/src/ILLink/ILLink.Suppressions.xml index 032094abce4ff..7e36a2f01ffab 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/ILLink/ILLink.Suppressions.xml +++ b/src/libraries/System.Private.DataContractSerialization/src/ILLink/ILLink.Suppressions.xml @@ -21,87 +21,81 @@ ILLink - IL2055 + IL2046 member - M:System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.SetKeyValuePairAdapterFlags(System.Type) + M:System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.InternalReadObject(System.Runtime.Serialization.XmlReaderDelegator,System.Boolean) ILLink - IL2055 + IL2046 member - M:System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper.GetCollectionElementType + M:System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.InternalWriteObject(System.Runtime.Serialization.XmlWriterDelegator,System.Object) ILLink - IL2055 + IL2046 member - M:System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper.IncrementCollectionCount(System.Runtime.Serialization.XmlWriterDelegator,System.Object,System.Runtime.Serialization.XmlObjectSerializerWriteContext) + M:System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.InternalWriteObjectContent(System.Runtime.Serialization.XmlWriterDelegator,System.Object) ILLink - IL2055 + IL2046 member - M:System.Runtime.Serialization.CollectionDataContract.IsCollectionOrTryCreate(System.Type,System.Boolean,System.Runtime.Serialization.DataContract@,System.Type@,System.Boolean) + M:System.Runtime.Serialization.Json.ReflectionJsonReader.ReflectionReadDictionaryItem(System.Runtime.Serialization.XmlReaderDelegator,System.Runtime.Serialization.XmlObjectSerializerReadContext,System.Runtime.Serialization.CollectionDataContract) ILLink - IL2055 + IL2046 member - M:System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractAdapterType(System.Type) + M:System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(System.Runtime.Serialization.DataContract,System.Runtime.Serialization.XmlReaderDelegator) ILLink - IL2055 + IL2046 member - M:System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(System.Type,System.Collections.Generic.Dictionary{System.Type,System.Type},System.Collections.Generic.Dictionary{System.Xml.XmlQualifiedName,System.Runtime.Serialization.DataContract}@) + M:System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue(System.Runtime.Serialization.DataContract,System.Runtime.Serialization.XmlWriterDelegator,System.Object,System.RuntimeTypeHandle) ILLink IL2055 member - M:System.Runtime.Serialization.Globals.get_TypeOfHashtable - - - ILLink - IL2055 - member - M:System.Runtime.Serialization.Json.DataContractJsonSerializerImpl.AddCollectionItemTypeToKnownTypes(System.Type) + M:System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.SetKeyValuePairAdapterFlags(System.Type) ILLink IL2055 member - M:System.Runtime.Serialization.Json.JsonDataContract.JsonDataContractCriticalHelper.AddCollectionItemContractsToKnownDataContracts + M:System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper.GetCollectionElementType ILLink IL2055 member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.GenerateClassReader(System.Runtime.Serialization.ClassDataContract) + M:System.Runtime.Serialization.CollectionDataContract.CollectionDataContractCriticalHelper.IncrementCollectionCount(System.Runtime.Serialization.XmlWriterDelegator,System.Object,System.Runtime.Serialization.XmlObjectSerializerWriteContext) ILLink IL2055 member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.ReadCollection(System.Runtime.Serialization.CollectionDataContract) + M:System.Runtime.Serialization.CollectionDataContract.IsCollectionOrTryCreate(System.Type,System.Boolean,System.Runtime.Serialization.DataContract@,System.Type@,System.Boolean) ILLink IL2055 member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.WrapNullableObject(System.Reflection.Emit.LocalBuilder,System.Reflection.Emit.LocalBuilder,System.Int32) + M:System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractAdapterType(System.Type) ILLink IL2055 member - M:System.Runtime.Serialization.Json.JsonFormatWriterGenerator.CriticalHelper.InitArgs(System.Type) + M:System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(System.Type,System.Collections.Generic.Dictionary{System.Type,System.Type},System.Collections.Generic.Dictionary{System.Xml.XmlQualifiedName,System.Runtime.Serialization.DataContract}@) ILLink IL2055 member - M:System.Runtime.Serialization.Json.JsonFormatWriterGenerator.CriticalHelper.WriteCollection(System.Runtime.Serialization.CollectionDataContract) + M:System.Runtime.Serialization.Globals.get_TypeOfHashtable ILLink @@ -169,18 +163,6 @@ member M:System.Runtime.Serialization.FastInvokerBuilder.GetMakeNewInstanceFunc(System.Type) - - ILLink - IL2060 - member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.ReadCollection(System.Runtime.Serialization.CollectionDataContract) - - - ILLink - IL2060 - member - M:System.Runtime.Serialization.Json.JsonFormatWriterGenerator.CriticalHelper.WriteCollection(System.Runtime.Serialization.CollectionDataContract) - ILLink IL2060 @@ -229,12 +211,6 @@ member M:System.Runtime.Serialization.XmlFormatReaderGenerator.UnsafeGetUninitializedObject(System.Type) - - ILLink - IL2067 - member - M:System.Runtime.Serialization.SurrogateDataContract.GetUninitializedObject(System.Type) - ILLink IL2070 @@ -271,18 +247,6 @@ member M:System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(System.Type,System.Collections.Generic.Dictionary{System.Type,System.Type},System.Collections.Generic.Dictionary{System.Xml.XmlQualifiedName,System.Runtime.Serialization.DataContract}@) - - ILLink - IL2070 - member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.BeginMethod(System.Runtime.Serialization.CodeGenerator,System.String,System.Type,System.Boolean) - - - ILLink - IL2070 - member - M:System.Runtime.Serialization.Json.JsonFormatWriterGenerator.CriticalHelper.BeginMethod(System.Runtime.Serialization.CodeGenerator,System.String,System.Type,System.Boolean) - ILLink IL2070 @@ -361,36 +325,6 @@ member M:System.Runtime.Serialization.EnumDataContract.EnumDataContractCriticalHelper.ImportDataMembers - - ILLink - IL2075 - member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.CreateObject(System.Runtime.Serialization.ClassDataContract) - - - ILLink - IL2075 - member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.ReadCollection(System.Runtime.Serialization.CollectionDataContract) - - - ILLink - IL2075 - member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.ReadISerializable(System.Runtime.Serialization.ClassDataContract) - - - ILLink - IL2075 - member - M:System.Runtime.Serialization.Json.JsonFormatReaderGenerator.CriticalHelper.WrapNullableObject(System.Reflection.Emit.LocalBuilder,System.Reflection.Emit.LocalBuilder,System.Int32) - - - ILLink - IL2075 - member - M:System.Runtime.Serialization.Json.JsonFormatWriterGenerator.CriticalHelper.WriteCollection(System.Runtime.Serialization.CollectionDataContract) - ILLink IL2075 diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs index 780866c355f49..87625a843be20 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs @@ -12,6 +12,7 @@ using System.Security; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization.Json; namespace System.Runtime.Serialization { @@ -131,7 +132,7 @@ internal void BeginMethod(DynamicMethod dynamicMethod, Type delegateType, string internal void BeginMethod(string methodName, Type delegateType, bool allowPrivateMemberAccess) { - MethodInfo signature = delegateType.GetMethod("Invoke")!; + MethodInfo signature = JsonFormatWriterGenerator.GetInvokeMethod(delegateType); ParameterInfo[] parameters = signature.GetParameters(); Type[] paramTypes = new Type[parameters.Length]; for (int i = 0; i < parameters.Length; i++) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/DataContractJsonSerializer.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/DataContractJsonSerializer.cs index 4a66fdf2b83c1..f29eafb3ddb20 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/DataContractJsonSerializer.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/DataContractJsonSerializer.cs @@ -26,6 +26,8 @@ public sealed class DataContractJsonSerializer : XmlObjectSerializer private const char LOW_SURROGATE_END = (char)0xdfff; private const char MAX_CHAR = (char)0xfffe; private const char WHITESPACE = ' '; + internal const string SerializerTrimmerWarning = "Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the " + + "required types are preserved."; internal IList? knownTypeList; @@ -40,36 +42,43 @@ public sealed class DataContractJsonSerializer : XmlObjectSerializer private readonly DataContractJsonSerializerImpl _serializer; private readonly bool _ignoreExtensionDataObject; + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type) { _serializer = new DataContractJsonSerializerImpl(type); } + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type, string? rootName) : this(type, rootName, null) { } + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type, XmlDictionaryString? rootName) : this(type, rootName, null) { } + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type, IEnumerable? knownTypes) { _serializer = new DataContractJsonSerializerImpl(type, knownTypes); } + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type, string? rootName, IEnumerable? knownTypes) : this(type, new DataContractJsonSerializerSettings() { RootName = rootName, KnownTypes = knownTypes }) { } + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type, XmlDictionaryString? rootName, IEnumerable? knownTypes) { _serializer = new DataContractJsonSerializerImpl(type, rootName, knownTypes); } + [RequiresUnreferencedCode(SerializerTrimmerWarning)] public DataContractJsonSerializer(Type type, DataContractJsonSerializerSettings? settings) { _serializer = new DataContractJsonSerializerImpl(type, settings); @@ -425,11 +434,13 @@ internal static string ConvertXmlNameToJsonName(string xmlName) return (xmlName == null) ? null : new XmlDictionary().Add(ConvertXmlNameToJsonName(xmlName.Value)); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal static object? ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context) { return JsonDataContract.GetJsonDataContract(contract).ReadJsonValue(reader, context); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal static void WriteJsonValue(JsonDataContract contract, XmlWriterDelegator writer, object graph, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) { contract.WriteJsonValue(writer, graph, context, declaredTypeHandle); @@ -497,21 +508,25 @@ internal sealed class DataContractJsonSerializerImpl : XmlObjectSerializer private DateTimeFormat? _dateTimeFormat; private bool _useSimpleDictionaryFormat; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public DataContractJsonSerializerImpl(Type type) : this(type, (IEnumerable?)null) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public DataContractJsonSerializerImpl(Type type, IEnumerable? knownTypes) : this(type, null, knownTypes, int.MaxValue, false, false) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public DataContractJsonSerializerImpl(Type type, XmlDictionaryString? rootName, IEnumerable? knownTypes) : this(type, rootName, knownTypes, int.MaxValue, false, false) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal DataContractJsonSerializerImpl(Type type, XmlDictionaryString? rootName, IEnumerable? knownTypes, @@ -523,6 +538,7 @@ internal DataContractJsonSerializerImpl(Type type, Initialize(type, rootName, knownTypes, maxItemsInObjectGraph, ignoreExtensionDataObject, emitTypeInformation, false, null, false); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public DataContractJsonSerializerImpl(Type type, DataContractJsonSerializerSettings? settings) { if (settings == null) @@ -783,6 +799,7 @@ internal static bool IsJsonLocalName(XmlReaderDelegator reader, string elementNa return false; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal static object? ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson? context) { return JsonDataContract.GetJsonDataContract(contract).ReadJsonValue(reader, context); @@ -793,6 +810,7 @@ internal static void WriteJsonNull(XmlWriterDelegator writer) writer.WriteAttributeString(null, JsonGlobals.typeString, null, JsonGlobals.nullString); // prefix // namespace } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal static void WriteJsonValue(JsonDataContract contract, XmlWriterDelegator writer, object graph, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { contract.WriteJsonValue(writer, graph, context, declaredTypeHandle); @@ -818,6 +836,7 @@ internal override bool InternalIsStartObject(XmlReaderDelegator reader) return IsJsonLocalName(reader, RootName.Value); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal override object? InternalReadObject(XmlReaderDelegator xmlReader, bool verifyObjectName) { if (MaxItemsInObjectGraph == 0) @@ -852,6 +871,7 @@ internal override void InternalWriteEndObject(XmlWriterDelegator writer) writer.WriteEndElement(); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal override void InternalWriteObject(XmlWriterDelegator writer, object? graph) { InternalWriteStartObject(writer, graph); @@ -859,6 +879,7 @@ internal override void InternalWriteObject(XmlWriterDelegator writer, object? gr InternalWriteEndObject(writer); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal override void InternalWriteObjectContent(XmlWriterDelegator writer, object? graph) { if (MaxItemsInObjectGraph == 0) @@ -924,6 +945,7 @@ internal override void InternalWriteStartObject(XmlWriterDelegator writer, objec } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void AddCollectionItemTypeToKnownTypes(Type knownType) { Type? itemType; @@ -940,6 +962,7 @@ private void AddCollectionItemTypeToKnownTypes(Type knownType) } [MemberNotNull(nameof(_rootType))] + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void Initialize(Type type, IEnumerable? knownTypes, int maxItemsInObjectGraph, @@ -978,6 +1001,7 @@ private void Initialize(Type type, } [MemberNotNull(nameof(_rootType))] + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void Initialize(Type type, XmlDictionaryString? rootName, IEnumerable? knownTypes, diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonByteArrayDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonByteArrayDataContract.cs index b0ec7cdd07a09..eb43e5e96253c 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonByteArrayDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonByteArrayDataContract.cs @@ -6,16 +6,19 @@ using System.Text; using System.Diagnostics; using System.Xml; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { internal sealed class JsonByteArrayDataContract : JsonDataContract { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonByteArrayDataContract(ByteArrayDataContract traditionalByteArrayDataContract) : base(traditionalByteArrayDataContract) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { if (context == null) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonClassDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonClassDataContract.cs index 4c4c0d33b6e32..8b3494225cb08 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonClassDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonClassDataContract.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { @@ -13,6 +14,7 @@ internal sealed class JsonClassDataContract : JsonDataContract { private readonly JsonClassDataContractCriticalHelper _helper; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonClassDataContract(ClassDataContract traditionalDataContract) : base(new JsonClassDataContractCriticalHelper(traditionalDataContract)) { @@ -26,6 +28,7 @@ private JsonFormatClassReaderDelegate CreateJsonFormatReaderDelegate() internal JsonFormatClassReaderDelegate JsonFormatReaderDelegate { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (_helper.JsonFormatReaderDelegate == null) @@ -60,6 +63,7 @@ private JsonFormatClassWriterDelegate CreateJsonFormatWriterDelegate() internal JsonFormatClassWriterDelegate JsonFormatWriterDelegate { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (_helper.JsonFormatWriterDelegate == null) @@ -93,6 +97,7 @@ internal JsonFormatClassWriterDelegate JsonFormatWriterDelegate private ClassDataContract TraditionalClassDataContract => _helper.TraditionalClassDataContract; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { jsonReader.Read(); @@ -101,6 +106,7 @@ internal JsonFormatClassWriterDelegate JsonFormatWriterDelegate return o; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { Debug.Assert(context != null); @@ -116,6 +122,7 @@ private sealed class JsonClassDataContractCriticalHelper : JsonDataContractCriti private readonly ClassDataContract _traditionalClassDataContract; private readonly string _typeName; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonClassDataContractCriticalHelper(ClassDataContract traditionalDataContract) : base(traditionalDataContract) { diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonCollectionDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonCollectionDataContract.cs index 13693553deb9b..175835807faa2 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonCollectionDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonCollectionDataContract.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using System.Threading; @@ -13,6 +14,7 @@ internal sealed class JsonCollectionDataContract : JsonDataContract { private readonly JsonCollectionDataContractCriticalHelper _helper; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonCollectionDataContract(CollectionDataContract traditionalDataContract) : base(new JsonCollectionDataContractCriticalHelper(traditionalDataContract)) { @@ -26,6 +28,7 @@ private JsonFormatCollectionReaderDelegate CreateJsonFormatReaderDelegate() internal JsonFormatCollectionReaderDelegate JsonFormatReaderDelegate { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (_helper.JsonFormatReaderDelegate == null) @@ -60,6 +63,7 @@ private JsonFormatGetOnlyCollectionReaderDelegate CreateJsonFormatGetOnlyReaderD internal JsonFormatGetOnlyCollectionReaderDelegate JsonFormatGetOnlyReaderDelegate { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (_helper.JsonFormatGetOnlyReaderDelegate == null) @@ -93,6 +97,7 @@ internal JsonFormatGetOnlyCollectionReaderDelegate JsonFormatGetOnlyReaderDelega } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private JsonFormatCollectionWriterDelegate CreateJsonFormatWriterDelegate() { return new ReflectionJsonFormatWriter().ReflectionWriteCollection; @@ -101,6 +106,7 @@ private JsonFormatCollectionWriterDelegate CreateJsonFormatWriterDelegate() internal JsonFormatCollectionWriterDelegate JsonFormatWriterDelegate { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (_helper.JsonFormatWriterDelegate == null) @@ -130,6 +136,7 @@ internal JsonFormatCollectionWriterDelegate JsonFormatWriterDelegate private CollectionDataContract TraditionalCollectionDataContract => _helper.TraditionalCollectionDataContract; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { Debug.Assert(context != null); @@ -150,6 +157,7 @@ internal JsonFormatCollectionWriterDelegate JsonFormatWriterDelegate return o; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { Debug.Assert(context != null); @@ -165,6 +173,7 @@ private sealed class JsonCollectionDataContractCriticalHelper : JsonDataContract private JsonFormatCollectionWriterDelegate? _jsonFormatWriterDelegate; private readonly CollectionDataContract _traditionalCollectionDataContract; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonCollectionDataContractCriticalHelper(CollectionDataContract traditionalDataContract) : base(traditionalDataContract) { diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonDataContract.cs index 6482fd752639c..a638ff086e362 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonDataContract.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Xml; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { @@ -14,6 +15,7 @@ internal class JsonDataContract { private readonly JsonDataContractCriticalHelper _helper; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] protected JsonDataContract(DataContract traditionalDataContract) { _helper = new JsonDataContractCriticalHelper(traditionalDataContract); @@ -60,11 +62,13 @@ internal static JsonReadWriteDelegates GetReadWriteDelegatesFromGeneratedAssembl return result; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract) { return JsonDataContractCriticalHelper.GetJsonDataContract(traditionalDataContract); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public object? ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { PushKnownDataContracts(context); @@ -73,11 +77,13 @@ public static JsonDataContract GetJsonDataContract(DataContract traditionalDataC return deserializedObject; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public virtual object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { return TraditionalDataContract.ReadXmlValue(jsonReader, context); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public void WriteJsonValue(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { PushKnownDataContracts(context); @@ -85,6 +91,7 @@ public void WriteJsonValue(XmlWriterDelegator jsonWriter, object obj, XmlObjectS PopKnownDataContracts(context); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public virtual void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { TraditionalDataContract.WriteXmlValue(jsonWriter, obj, context); @@ -141,6 +148,7 @@ internal class JsonDataContractCriticalHelper private readonly DataContract _traditionalDataContract; private readonly string _typeName; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal JsonDataContractCriticalHelper(DataContract traditionalDataContract) { _traditionalDataContract = traditionalDataContract; @@ -154,6 +162,7 @@ internal JsonDataContractCriticalHelper(DataContract traditionalDataContract) internal virtual string TypeName => _typeName; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract) { int id = JsonDataContractCriticalHelper.GetId(traditionalDataContract.UnderlyingType.TypeHandle); @@ -199,6 +208,7 @@ internal static int GetId(RuntimeTypeHandle typeHandle) } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract) { lock (s_createDataContractLock) @@ -262,6 +272,7 @@ private static JsonDataContract CreateJsonDataContract(int id, DataContract trad } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void AddCollectionItemContractsToKnownDataContracts() { if (_traditionalDataContract.KnownDataContracts != null) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEnumDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEnumDataContract.cs index f27c5361ebd90..7fe0a429c05fe 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEnumDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonEnumDataContract.cs @@ -1,12 +1,15 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics.CodeAnalysis; + namespace System.Runtime.Serialization.Json { internal sealed class JsonEnumDataContract : JsonDataContract { private readonly JsonEnumDataContractCriticalHelper _helper; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonEnumDataContract(EnumDataContract traditionalDataContract) : base(new JsonEnumDataContractCriticalHelper(traditionalDataContract)) { @@ -15,6 +18,7 @@ public JsonEnumDataContract(EnumDataContract traditionalDataContract) public bool IsULong => _helper.IsULong; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { object enumValue; @@ -34,6 +38,7 @@ public JsonEnumDataContract(EnumDataContract traditionalDataContract) return enumValue; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { if (IsULong) @@ -50,6 +55,7 @@ private sealed class JsonEnumDataContractCriticalHelper : JsonDataContractCritic { private readonly bool _isULong; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonEnumDataContractCriticalHelper(EnumDataContract traditionalEnumDataContract) : base(traditionalEnumDataContract) { diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatGeneratorStatics.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatGeneratorStatics.cs index ea6c9f016b29b..43218be213304 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatGeneratorStatics.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatGeneratorStatics.cs @@ -7,6 +7,7 @@ using System.Runtime.Serialization.Json; using System.Xml; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization { @@ -127,6 +128,7 @@ public static MethodInfo GetItemContractMethod } public static MethodInfo GetJsonDataContractMethod { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (s_getJsonDataContractMethod == null) @@ -270,8 +272,10 @@ public static MethodInfo OnDeserializationMethod return s_onDeserializationMethod; } } + public static MethodInfo ReadJsonValueMethod { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (s_readJsonValueMethod == null) @@ -415,6 +419,7 @@ public static MethodInfo WriteJsonNameWithMappingMethod } public static MethodInfo WriteJsonValueMethod { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] get { if (s_writeJsonValueMethod == null) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatReaderGenerator.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatReaderGenerator.cs index 49f3055ec2427..e4aeed0d50556 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatReaderGenerator.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatReaderGenerator.cs @@ -9,6 +9,7 @@ namespace System.Runtime.Serialization.Json using System; using System.Collections.Generic; using System.Diagnostics; + using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Reflection.Emit; using System.Runtime; @@ -28,16 +29,19 @@ public JsonFormatReaderGenerator() _helper = new CriticalHelper(); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { return _helper.GenerateClassReader(classContract); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonFormatCollectionReaderDelegate GenerateCollectionReader(CollectionDataContract collectionContract) { return _helper.GenerateCollectionReader(collectionContract); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonFormatGetOnlyCollectionReaderDelegate GenerateGetOnlyCollectionReader(CollectionDataContract collectionContract) { return _helper.GenerateGetOnlyCollectionReader(collectionContract); @@ -54,6 +58,7 @@ private sealed class CriticalHelper private ArgBuilder? _collectionContractArg; private ArgBuilder _emptyDictionaryStringArg = null!; // initialized in InitArgs + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { _ilg = new CodeGenerator(); @@ -115,6 +120,7 @@ public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract class return (JsonFormatClassReaderDelegate)_ilg.EndMethod(); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonFormatCollectionReaderDelegate GenerateCollectionReader(CollectionDataContract collectionContract) { _ilg = GenerateCollectionReaderHelper(collectionContract, false /*isGetOnlyCollection*/); @@ -124,6 +130,7 @@ public JsonFormatCollectionReaderDelegate GenerateCollectionReader(CollectionDat return (JsonFormatCollectionReaderDelegate)_ilg.EndMethod(); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonFormatGetOnlyCollectionReaderDelegate GenerateGetOnlyCollectionReader(CollectionDataContract collectionContract) { _ilg = GenerateCollectionReaderHelper(collectionContract, true /*isGetOnlyCollection*/); @@ -164,7 +171,7 @@ private CodeGenerator GenerateCollectionReaderHelper(CollectionDataContract coll private void BeginMethod(CodeGenerator ilg, string methodName, Type delegateType, bool allowPrivateMemberAccess) { - MethodInfo signature = delegateType.GetMethod("Invoke")!; + MethodInfo signature = JsonFormatWriterGenerator.GetInvokeMethod(delegateType); ParameterInfo[] parameters = signature.GetParameters(); Type[] paramTypes = new Type[parameters.Length]; for (int i = 0; i < parameters.Length; i++) @@ -182,6 +189,7 @@ private void InitArgs() _memberNamesArg = _ilg.GetArg(3); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void CreateObject(ClassDataContract classContract) { _objectType = classContract.UnderlyingType; @@ -264,6 +272,7 @@ private bool InvokeFactoryMethod(ClassDataContract classContract) return false; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void ReadClass(ClassDataContract classContract) { if (classContract.HasExtensionData) @@ -288,6 +297,7 @@ private void ReadClass(ClassDataContract classContract) } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void ReadMembers(ClassDataContract classContract, LocalBuilder? extensionDataLocal) { int memberCount = classContract.MemberNames!.Length; @@ -335,6 +345,7 @@ private void ReadMembers(ClassDataContract classContract, LocalBuilder? extensio _ilg.MarkLabel(endOfTypeLabel); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private int ReadMembers(ClassDataContract classContract, BitFlagsGenerator expectedElements, Label[] memberLabels, Label throwDuplicateMemberLabel, LocalBuilder memberIndexLocal) { @@ -428,6 +439,7 @@ private void ResetExpectedElements(BitFlagsGenerator expectedElements, int index expectedElements.Store(index, false); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void ReadISerializable(ClassDataContract classContract) { ConstructorInfo? ctor = classContract.UnderlyingType.GetConstructor(Globals.ScanAllMembers, JsonFormatGeneratorStatics.SerInfoCtorArgs); @@ -441,6 +453,7 @@ private void ReadISerializable(ClassDataContract classContract) _ilg.Call(ctor); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private LocalBuilder ReadValue(Type type, string name) { LocalBuilder value = _ilg.DeclareLocal(type, "valueRead"); @@ -547,6 +560,7 @@ private void InternalDeserialize(LocalBuilder value, Type type, string name) _ilg.Stloc(value); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void WrapNullableObject(LocalBuilder innerValue, LocalBuilder outerValue, int nullables) { Type innerType = innerValue.LocalType, outerType = outerValue.LocalType; @@ -561,6 +575,7 @@ private void WrapNullableObject(LocalBuilder innerValue, LocalBuilder outerValue _ilg.Call(outerType.GetConstructor(new Type[] { innerType })!); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void ReadCollection(CollectionDataContract collectionContract) { Type type = collectionContract.UnderlyingType; @@ -686,6 +701,7 @@ private void ReadCollection(CollectionDataContract collectionContract) } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void ReadSimpleDictionary(CollectionDataContract collectionContract, Type keyValueType) { Type[] keyValueTypes = keyValueType.GetGenericArguments(); @@ -778,6 +794,7 @@ private void ReadSimpleDictionary(CollectionDataContract collectionContract, Typ } } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void ReadGetOnlyCollection(CollectionDataContract collectionContract) { Type type = collectionContract.UnderlyingType; @@ -916,6 +933,7 @@ private bool TryReadPrimitiveArray(Type itemType) return false; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private LocalBuilder ReadCollectionItem(CollectionDataContract collectionContract, Type itemType) { if (collectionContract.Kind == CollectionKind.Dictionary || collectionContract.Kind == CollectionKind.GenericDictionary) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatWriterGenerator.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatWriterGenerator.cs index e47e30fd67361..a39e36802db46 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatWriterGenerator.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonFormatWriterGenerator.cs @@ -9,6 +9,7 @@ namespace System.Runtime.Serialization.Json using System; using System.Collections; using System.Diagnostics; + using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Reflection.Emit; using System.Security; @@ -26,16 +27,26 @@ public JsonFormatWriterGenerator() _helper = new CriticalHelper(); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract) { return _helper.GenerateClassWriter(classContract); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal JsonFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract) { return _helper.GenerateCollectionWriter(collectionContract); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", + Justification = "The trimmer will never remove the Invoke method from delegates.")] + internal static MethodInfo GetInvokeMethod(Type delegateType) + { + Debug.Assert(typeof(Delegate).IsAssignableFrom(delegateType)); + return delegateType.GetMethod("Invoke")!; + } + private sealed class CriticalHelper { private CodeGenerator _ilg = null!; // initialized in GenerateXXXWriter @@ -49,6 +60,7 @@ private sealed class CriticalHelper private int _typeIndex = 1; private int _childElementIndex; + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract) { _ilg = new CodeGenerator(); @@ -74,6 +86,7 @@ internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract cla return (JsonFormatClassWriterDelegate)_ilg.EndMethod(); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] internal JsonFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract) { _ilg = new CodeGenerator(); @@ -100,8 +113,7 @@ internal JsonFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionD private void BeginMethod(CodeGenerator ilg, string methodName, Type delegateType, bool allowPrivateMemberAccess) { - - MethodInfo signature = delegateType.GetMethod("Invoke")!; + MethodInfo signature = GetInvokeMethod(delegateType); ParameterInfo[] parameters = signature.GetParameters(); Type[] paramTypes = new Type[parameters.Length]; for (int i = 0; i < parameters.Length; i++) @@ -111,6 +123,7 @@ private void BeginMethod(CodeGenerator ilg, string methodName, Type delegateType ilg.BeginMethod(dynamicMethod, delegateType, methodName, paramTypes, allowPrivateMemberAccess); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void InitArgs(Type objType) { _xmlWriterArg = _ilg.GetArg(0); @@ -272,6 +285,7 @@ private LocalBuilder LoadMemberValue(DataMember member) return memberValue; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] private void WriteCollection(CollectionDataContract collectionContract) { LocalBuilder itemName = _ilg.DeclareLocal(typeof(XmlDictionaryString), "itemName"); diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonObjectDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonObjectDataContract.cs index 034d57d757da0..2da99f1eff5d2 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonObjectDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonObjectDataContract.cs @@ -4,16 +4,19 @@ using System.Xml; using System.Runtime.Serialization; using System.Globalization; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { internal sealed class JsonObjectDataContract : JsonDataContract { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonObjectDataContract(DataContract traditionalDataContract) : base(traditionalDataContract) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { object? obj; @@ -53,6 +56,7 @@ public JsonObjectDataContract(DataContract traditionalDataContract) return obj; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { jsonWriter.WriteAttributeString(null, JsonGlobals.typeString, null, JsonGlobals.objectString); diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonQNameDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonQNameDataContract.cs index 57c61347c83a2..aa6d2f137a54c 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonQNameDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonQNameDataContract.cs @@ -6,16 +6,19 @@ using System.Text; using System.Diagnostics; using System.Xml; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { internal sealed class JsonQNameDataContract : JsonDataContract { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonQNameDataContract(QNameDataContract traditionalQNameDataContract) : base(traditionalQNameDataContract) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { if (context == null) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonStringDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonStringDataContract.cs index 89365be7c7626..19c1f28182829 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonStringDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonStringDataContract.cs @@ -6,16 +6,19 @@ using System.Text; using System.Diagnostics; using System.Xml; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { internal sealed class JsonStringDataContract : JsonDataContract { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonStringDataContract(StringDataContract traditionalStringDataContract) : base(traditionalStringDataContract) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { if (context == null) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonUriDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonUriDataContract.cs index de4dce0cf7fce..7cc8601bab92f 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonUriDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonUriDataContract.cs @@ -1,21 +1,19 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Collections.Generic; -using System.Text; -using System.Diagnostics; -using System.Xml; +using System.Diagnostics.CodeAnalysis; namespace System.Runtime.Serialization.Json { internal sealed class JsonUriDataContract : JsonDataContract { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonUriDataContract(UriDataContract traditionalUriDataContract) : base(traditionalUriDataContract) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { if (context == null) diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonXmlDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonXmlDataContract.cs index 12044fb8b2ec4..4fc9ac93da4cc 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonXmlDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JsonXmlDataContract.cs @@ -1,20 +1,23 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Xml; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; +using System.Xml; namespace System.Runtime.Serialization.Json { internal sealed class JsonXmlDataContract : JsonDataContract { + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public JsonXmlDataContract(XmlDataContract traditionalXmlDataContract) : base(traditionalXmlDataContract) { } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override object? ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson? context) { string xmlContent = jsonReader.ReadElementContentAsString(); @@ -40,6 +43,7 @@ public JsonXmlDataContract(XmlDataContract traditionalXmlDataContract) return xmlValue; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public override void WriteJsonValueCore(XmlWriterDelegator jsonWriter, object obj, XmlObjectSerializerWriteContextComplexJson? context, RuntimeTypeHandle declaredTypeHandle) { DataContractSerializer dataContractSerializer = new DataContractSerializer(Type.GetTypeFromHandle(declaredTypeHandle), diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatReader.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatReader.cs index dae0e1b19128f..e27857f03411f 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatReader.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatReader.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Text; @@ -102,6 +103,7 @@ protected override string GetCollectionContractNamespace(CollectionDataContract return string.Empty; } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] protected override object? ReflectionReadDictionaryItem(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, CollectionDataContract collectionContract) { var jsonContext = context as XmlObjectSerializerReadContextComplexJson; diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatWriter.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatWriter.cs index 2c1909e007870..c67784eed42c8 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatWriter.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/ReflectionJsonFormatWriter.cs @@ -5,6 +5,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Text; @@ -22,6 +23,7 @@ public void ReflectionWriteClass(XmlWriterDelegator xmlWriter, object obj, XmlOb _reflectionClassWriter.ReflectionWriteClass(xmlWriter, obj, context, classContract, memberNames); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] public void ReflectionWriteCollection(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, CollectionDataContract collectionContract) { JsonWriterDelegator? jsonWriter = xmlWriter as JsonWriterDelegator; diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerReadContextComplexJson.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerReadContextComplexJson.cs index f5f9b404b46a5..c84405311d409 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerReadContextComplexJson.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerReadContextComplexJson.cs @@ -34,6 +34,7 @@ internal static XmlObjectSerializerReadContextComplexJson CreateContext(DataCont return new XmlObjectSerializerReadContextComplexJson(serializer, rootTypeDataContract); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] protected override object? ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader) { return DataContractJsonSerializerImpl.ReadJsonValue(dataContract, reader, this); diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerWriteContextComplexJson.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerWriteContextComplexJson.cs index 996e2c3915f93..ef784e7499c4c 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerWriteContextComplexJson.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/XmlObjectSerializerWriteContextComplexJson.cs @@ -151,6 +151,7 @@ private void WriteTypeInfo(XmlWriterDelegator writer, string typeInformation) writer.WriteAttributeString(null, JsonGlobals.serverTypeString, null, typeInformation); } + [RequiresUnreferencedCode(DataContractJsonSerializer.SerializerTrimmerWarning)] protected override void WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle declaredTypeHandle) { JsonDataContract jsonDataContract = JsonDataContract.GetJsonDataContract(dataContract); diff --git a/src/libraries/System.Runtime.Serialization.Json/ref/System.Runtime.Serialization.Json.cs b/src/libraries/System.Runtime.Serialization.Json/ref/System.Runtime.Serialization.Json.cs index e9dee409abc33..6781f4925fba0 100644 --- a/src/libraries/System.Runtime.Serialization.Json/ref/System.Runtime.Serialization.Json.cs +++ b/src/libraries/System.Runtime.Serialization.Json/ref/System.Runtime.Serialization.Json.cs @@ -25,12 +25,19 @@ namespace System.Runtime.Serialization.Json { public sealed partial class DataContractJsonSerializer : System.Runtime.Serialization.XmlObjectSerializer { + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type, System.Collections.Generic.IEnumerable? knownTypes) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type, System.Runtime.Serialization.Json.DataContractJsonSerializerSettings? settings) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type, string? rootName) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type, string? rootName, System.Collections.Generic.IEnumerable? knownTypes) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type, System.Xml.XmlDictionaryString? rootName) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Json Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")] public DataContractJsonSerializer(System.Type type, System.Xml.XmlDictionaryString? rootName, System.Collections.Generic.IEnumerable? knownTypes) { } public System.Runtime.Serialization.DateTimeFormat? DateTimeFormat { get { throw null; } } public System.Runtime.Serialization.EmitTypeInformation EmitTypeInformation { get { throw null; } }