| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | |
| | | 6 | | namespace System.Text.Json.Serialization |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// When placed on a property, field, or type, specifies the converter type to use. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The specified converter type must derive from <see cref="JsonConverter"/>. |
| | | 13 | | /// When placed on a property or field, the specified converter will always be used. |
| | | 14 | | /// When placed on a type, the specified converter will be used unless a compatible converter is added to |
| | | 15 | | /// <see cref="JsonSerializerOptions.Converters"/> or there is another <see cref="JsonConverterAttribute"/> on a pro |
| | | 16 | | /// of the same type. |
| | | 17 | | /// </remarks> |
| | | 18 | | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enu |
| | | 19 | | public class JsonConverterAttribute : JsonAttribute |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of <see cref="JsonConverterAttribute"/> with the specified converter type. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="converterType">The type of the converter.</param> |
| | 269 | 25 | | public JsonConverterAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessCons |
| | 269 | 26 | | { |
| | 269 | 27 | | ConverterType = converterType; |
| | 269 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of <see cref="JsonConverterAttribute"/>. |
| | | 32 | | /// </summary> |
| | 0 | 33 | | protected JsonConverterAttribute() { } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The type of the converter to create, or null if <see cref="CreateConverter(Type)"/> should be used to obtain |
| | | 37 | | /// </summary> |
| | | 38 | | [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] |
| | 269 | 39 | | public Type? ConverterType { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// If overridden and <see cref="ConverterType"/> is null, allows a custom attribute to create the converter in |
| | | 43 | | /// </summary> |
| | | 44 | | /// <returns> |
| | | 45 | | /// The custom converter. |
| | | 46 | | /// </returns> |
| | | 47 | | public virtual JsonConverter? CreateConverter(Type typeToConvert) |
| | 0 | 48 | | { |
| | 0 | 49 | | return null; |
| | 0 | 50 | | } |
| | | 51 | | } |
| | | 52 | | } |