Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json.Serialization
{
/// <summary>
Expand All @@ -21,7 +23,7 @@ public class JsonConverterAttribute : JsonAttribute
/// Initializes a new instance of <see cref="JsonConverterAttribute"/> with the specified converter type.
/// </summary>
/// <param name="converterType">The type of the converter.</param>
public JsonConverterAttribute(Type converterType)
public JsonConverterAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type converterType)
{
ConverterType = converterType;
}
Expand All @@ -34,6 +36,7 @@ protected JsonConverterAttribute() { }
/// <summary>
/// The type of the converter to create, or null if <see cref="CreateConverter(Type)"/> should be used to obtain the converter.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type? ConverterType { get; private set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -25,23 +24,6 @@ public override bool CanConvert(Type typeToConvert)
return typeof(IEnumerable).IsAssignableFrom(typeToConvert);
}

[DynamicDependency("#ctor", typeof(ArrayConverter<,>))]
[DynamicDependency("#ctor", typeof(ConcurrentQueueOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(ConcurrentStackOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(DictionaryOfStringTValueConverter<,>))]
[DynamicDependency("#ctor", typeof(ICollectionOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(IDictionaryOfStringTValueConverter<,>))]
[DynamicDependency("#ctor", typeof(IEnumerableOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(IEnumerableWithAddMethodConverter<>))]
[DynamicDependency("#ctor", typeof(IListConverter<>))]
[DynamicDependency("#ctor", typeof(IListOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(ImmutableDictionaryOfStringTValueConverter<,>))]
[DynamicDependency("#ctor", typeof(ImmutableEnumerableOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(IReadOnlyDictionaryOfStringTValueConverter<,>))]
[DynamicDependency("#ctor", typeof(ISetOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(ListOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(QueueOfTConverter<,>))]
[DynamicDependency("#ctor", typeof(StackOfTConverter<,>))]
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
Type converterType = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public static bool IsImmutableEnumerableType(this Type type)
}
}

// TODO: https://github.com/dotnet/runtime/issues/38593.
public static MethodInfo GetImmutableEnumerableCreateRangeMethod(this Type type, Type elementType)
{
Type constructingType = GetImmutableEnumerableConstructingType(type);
Expand All @@ -159,6 +160,7 @@ public static MethodInfo GetImmutableEnumerableCreateRangeMethod(this Type type,
return null!;
}

// TODO: https://github.com/dotnet/runtime/issues/38593.
public static MethodInfo GetImmutableDictionaryCreateRangeMethod(this Type type, Type elementType)
{
Type constructingType = GetImmutableDictionaryConstructingType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace System.Text.Json.Serialization.Converters
Expand All @@ -23,10 +22,6 @@ public override bool CanConvert(Type typeToConvert)
return true;
}

[DynamicDependency("#ctor", typeof(KeyValuePairConverter<,>))]
[DynamicDependency("#ctor", typeof(LargeObjectWithParameterizedConstructorConverter<>))]
[DynamicDependency("#ctor", typeof(ObjectDefaultConverter<>))]
[DynamicDependency("#ctor", typeof(SmallObjectWithParameterizedConstructorConverter<,,,,>))]
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
if (IsKeyValuePair(typeToConvert))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System.Reflection;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json.Serialization.Converters
{
Expand All @@ -18,9 +17,6 @@ public override bool CanConvert(Type type)
return type.IsEnum;
}

[DynamicDependency(
"#ctor(System.Text.Json.Serialization.Converters.EnumConverterOptions,System.Text.Json.JsonSerializerOptions)",
typeof(EnumConverter<>))]
public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
{
JsonConverter converter = (JsonConverter)Activator.CreateInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace System.Text.Json
{
public static partial class JsonSerializer
{
// Members accessed by the serializer when deserializing.
private const DynamicallyAccessedMemberTypes MembersAccessedOnRead = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties;

[return: MaybeNull]
private static TValue ReadCore<TValue>(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static partial class JsonSerializer
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
[return: MaybeNull]
public static TValue Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerializerOptions? options = null)
public static TValue Deserialize<[DynamicallyAccessedMembers(MembersAccessedOnRead)] TValue>(ReadOnlySpan<byte> utf8Json, JsonSerializerOptions? options = null)
{
if (options == null)
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public static TValue Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerial
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="returnType"/> or its serializable members.
/// </exception>
public static object? Deserialize(ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerOptions? options = null)
public static object? Deserialize(ReadOnlySpan<byte> utf8Json, [DynamicallyAccessedMembers(MembersAccessedOnRead)] Type returnType, JsonSerializerOptions? options = null)
{
if (returnType == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
Expand Down Expand Up @@ -35,7 +36,7 @@ public static partial class JsonSerializer
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
public static ValueTask<TValue> DeserializeAsync<TValue>(
public static ValueTask<TValue> DeserializeAsync<[DynamicallyAccessedMembers(MembersAccessedOnRead)] TValue>(
Stream utf8Json,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -73,7 +74,7 @@ public static ValueTask<TValue> DeserializeAsync<TValue>(
/// </exception>
public static ValueTask<object?> DeserializeAsync(
Stream utf8Json,
Type returnType,
[DynamicallyAccessedMembers(MembersAccessedOnRead)] Type returnType,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static partial class JsonSerializer
/// UTF-8 methods since the implementation natively uses UTF-8.
/// </remarks>
[return: MaybeNull]
public static TValue Deserialize<TValue>(string json, JsonSerializerOptions? options = null)
public static TValue Deserialize<[DynamicallyAccessedMembers(MembersAccessedOnRead)] TValue>(string json, JsonSerializerOptions? options = null)
{
if (json == null)
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public static TValue Deserialize<TValue>(string json, JsonSerializerOptions? opt
/// <remarks>Using a <see cref="string"/> is not as efficient as using the
/// UTF-8 methods since the implementation natively uses UTF-8.
/// </remarks>
public static object? Deserialize(string json, Type returnType, JsonSerializerOptions? options = null)
public static object? Deserialize(string json, [DynamicallyAccessedMembers(MembersAccessedOnRead)] Type returnType, JsonSerializerOptions? options = null)
{
if (json == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static partial class JsonSerializer
/// </para>
/// </remarks>
[return: MaybeNull]
public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions? options = null)
public static TValue Deserialize<[DynamicallyAccessedMembers(MembersAccessedOnRead)] TValue>(ref Utf8JsonReader reader, JsonSerializerOptions? options = null)
{
if (options == null)
{
Expand Down Expand Up @@ -112,7 +112,7 @@ public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializ
/// Hence, <see cref="JsonReaderOptions.AllowTrailingCommas"/>, <see cref="JsonReaderOptions.MaxDepth"/>, <see cref="JsonReaderOptions.CommentHandling"/> are used while reading.
/// </para>
/// </remarks>
public static object? Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions? options = null)
public static object? Deserialize(ref Utf8JsonReader reader, [DynamicallyAccessedMembers(MembersAccessedOnRead)] Type returnType, JsonSerializerOptions? options = null)
{
if (returnType == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json
{
public static partial class JsonSerializer
Expand All @@ -16,7 +18,9 @@ public static partial class JsonSerializer
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
public static byte[] SerializeToUtf8Bytes<TValue>(TValue value, JsonSerializerOptions? options = null)
public static byte[] SerializeToUtf8Bytes<[DynamicallyAccessedMembers(MembersAccessedOnWrite)] TValue>(
TValue value,
JsonSerializerOptions? options = null)
{
return WriteCoreBytes<TValue>(value, typeof(TValue), options);
}
Expand All @@ -38,7 +42,10 @@ public static byte[] SerializeToUtf8Bytes<TValue>(TValue value, JsonSerializerOp
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="inputType"/> or its serializable members.
/// </exception>
public static byte[] SerializeToUtf8Bytes(object? value, Type inputType, JsonSerializerOptions? options = null)
public static byte[] SerializeToUtf8Bytes(
object? value,
[DynamicallyAccessedMembers(MembersAccessedOnWrite)] Type inputType,
JsonSerializerOptions? options = null)
{
if (inputType == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace System.Text.Json
{
public static partial class JsonSerializer
{
// Members accessed by the serializer when serializing.
private const DynamicallyAccessedMemberTypes MembersAccessedOnWrite = DynamicallyAccessedMemberTypes.PublicProperties;

private static void WriteCore<TValue>(
Utf8JsonWriter writer,
in TValue value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
Expand All @@ -26,7 +27,7 @@ public static partial class JsonSerializer
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
public static Task SerializeAsync<TValue>(
public static Task SerializeAsync<[DynamicallyAccessedMembers(MembersAccessedOnWrite)] TValue>(
Stream utf8Json,
TValue value,
JsonSerializerOptions? options = null,
Expand Down Expand Up @@ -60,7 +61,7 @@ public static Task SerializeAsync<TValue>(
public static Task SerializeAsync(
Stream utf8Json,
object? value,
Type inputType,
[DynamicallyAccessedMembers(MembersAccessedOnWrite)] Type inputType,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;

namespace System.Text.Json
{
public static partial class JsonSerializer
Expand All @@ -20,7 +22,7 @@ public static partial class JsonSerializer
/// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes"/>
/// and <see cref="SerializeAsync"/>.
/// </remarks>
public static string Serialize<TValue>(TValue value, JsonSerializerOptions? options = null)
public static string Serialize<[DynamicallyAccessedMembers(MembersAccessedOnWrite)] TValue>(TValue value, JsonSerializerOptions? options = null)
{
return Serialize<TValue>(value, typeof(TValue), options);
}
Expand All @@ -43,7 +45,10 @@ public static string Serialize<TValue>(TValue value, JsonSerializerOptions? opti
/// encoding since the implementation internally uses UTF-8. See also <see cref="SerializeToUtf8Bytes"/>
/// and <see cref="SerializeAsync"/>.
/// </remarks>
public static string Serialize(object? value, Type inputType, JsonSerializerOptions? options = null)
public static string Serialize(
object? value,
[DynamicallyAccessedMembers(MembersAccessedOnWrite)] Type inputType,
JsonSerializerOptions? options = null)
{
if (inputType == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json.Serialization;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace System.Text.Json
{
Expand All @@ -22,7 +23,10 @@ public static partial class JsonSerializer
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <typeparamref name="TValue"/> or its serializable members.
/// </exception>
public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSerializerOptions? options = null)
public static void Serialize<[DynamicallyAccessedMembers(MembersAccessedOnWrite)] TValue>(
Utf8JsonWriter writer,
TValue value,
JsonSerializerOptions? options = null)
{
Serialize<TValue>(writer, value, typeof(TValue), options);
}
Expand All @@ -44,7 +48,11 @@ public static void Serialize<TValue>(Utf8JsonWriter writer, TValue value, JsonSe
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
/// for <paramref name="inputType"/> or its serializable members.
/// </exception>
public static void Serialize(Utf8JsonWriter writer, object? value, Type inputType, JsonSerializerOptions? options = null)
public static void Serialize(
Utf8JsonWriter writer,
object? value,
[DynamicallyAccessedMembers(MembersAccessedOnWrite)] Type inputType,
JsonSerializerOptions? options = null)
{
if (inputType == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization.Converters;

Expand Down Expand Up @@ -54,9 +53,6 @@ public override bool CanConvert(Type typeToConvert)
}

/// <inheritdoc />
[DynamicDependency(
"#ctor(System.Text.Json.Serialization.Converters.EnumConverterOptions,System.Text.Json.JsonNamingPolicy,System.Text.Json.JsonSerializerOptions)",
typeof(EnumConverter<>))]
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
JsonConverter converter = (JsonConverter)Activator.CreateInstance(
Expand Down
Loading