|
5 | 5 | using System.Text.Json;
|
6 | 6 | using System.Text.Json.Serialization;
|
7 | 7 |
|
8 |
| -namespace System.ClientModel.Primitives |
| 8 | +namespace System.ClientModel.Primitives; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// A generic converter which allows <see cref="JsonSerializer"/> to be able to write and read any models that implement <see cref="IJsonModel{T}"/>. |
| 12 | +/// </summary> |
| 13 | +[RequiresUnreferencedCode("The constructors of the type being deserialized are dynamically accessed and may be trimmed.")] |
| 14 | +#pragma warning disable AZC0014 // Avoid using banned types in public API |
| 15 | +internal class JsonModelConverter : JsonConverter<IJsonModel<object>> |
| 16 | +#pragma warning restore AZC0014 // Avoid using banned types in public API |
9 | 17 | {
|
10 | 18 | /// <summary>
|
11 |
| - /// A generic converter which allows <see cref="JsonSerializer"/> to be able to write and read any models that implement <see cref="IJsonModel{T}"/>. |
| 19 | + /// Gets the <see cref="ModelReaderWriterOptions"/> used to read and write models. |
12 | 20 | /// </summary>
|
13 |
| - [RequiresUnreferencedCode("The constructors of the type being deserialized are dynamically accessed and may be trimmed.")] |
14 |
| -#pragma warning disable AZC0014 // Avoid using banned types in public API |
15 |
| - internal class JsonModelConverter : JsonConverter<IJsonModel<object>> |
16 |
| -#pragma warning restore AZC0014 // Avoid using banned types in public API |
| 21 | + public ModelReaderWriterOptions Options { get; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of <see cref="JsonModelConverter"/> with a default options of <see cref="ModelReaderWriterOptions.Json"/>. |
| 25 | + /// </summary> |
| 26 | + public JsonModelConverter() |
| 27 | + : this(ModelReaderWriterOptions.Json) { } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of <see cref="JsonModelConverter"/>. |
| 31 | + /// </summary> |
| 32 | + /// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param> |
| 33 | + public JsonModelConverter(ModelReaderWriterOptions options) |
17 | 34 | {
|
18 |
| - /// <summary> |
19 |
| - /// Gets the <see cref="ModelReaderWriterOptions"/> used to read and write models. |
20 |
| - /// </summary> |
21 |
| - public ModelReaderWriterOptions Options { get; } |
22 |
| - |
23 |
| - /// <summary> |
24 |
| - /// Initializes a new instance of <see cref="JsonModelConverter"/> with a default options of <see cref="ModelReaderWriterOptions.Json"/>. |
25 |
| - /// </summary> |
26 |
| - public JsonModelConverter() |
27 |
| - : this(ModelReaderWriterOptions.Json) { } |
28 |
| - |
29 |
| - /// <summary> |
30 |
| - /// Initializes a new instance of <see cref="JsonModelConverter"/>. |
31 |
| - /// </summary> |
32 |
| - /// <param name="options">The <see cref="ModelReaderWriterOptions"/> to use.</param> |
33 |
| - public JsonModelConverter(ModelReaderWriterOptions options) |
34 |
| - { |
35 |
| - Options = options; |
36 |
| - } |
37 |
| - |
38 |
| - /// <inheritdoc/> |
39 |
| - public override bool CanConvert(Type typeToConvert) |
40 |
| - { |
41 |
| - return !Attribute.IsDefined(typeToConvert, typeof(JsonConverterAttribute)); |
42 |
| - } |
43 |
| - |
44 |
| - /// <inheritdoc/> |
| 35 | + Options = options; |
| 36 | + } |
| 37 | + |
| 38 | + /// <inheritdoc/> |
| 39 | + public override bool CanConvert(Type typeToConvert) |
| 40 | + { |
| 41 | + return !Attribute.IsDefined(typeToConvert, typeof(JsonConverterAttribute)); |
| 42 | + } |
| 43 | + |
| 44 | + /// <inheritdoc/> |
45 | 45 | #pragma warning disable AZC0014 // Avoid using banned types in public API
|
46 |
| - public override IJsonModel<object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 46 | + public override IJsonModel<object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
47 | 47 | #pragma warning restore AZC0014 // Avoid using banned types in public API
|
48 |
| - { |
49 |
| - using JsonDocument document = JsonDocument.ParseValue(ref reader); |
50 |
| - return (IJsonModel<object>)ModelReaderWriter.Read(BinaryData.FromString(document.RootElement.GetRawText()), typeToConvert, Options)!; |
51 |
| - } |
| 48 | + { |
| 49 | + using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 50 | + return (IJsonModel<object>)ModelReaderWriter.Read(BinaryData.FromString(document.RootElement.GetRawText()), typeToConvert, Options)!; |
| 51 | + } |
52 | 52 |
|
53 |
| - /// <inheritdoc/> |
| 53 | + /// <inheritdoc/> |
54 | 54 | #pragma warning disable AZC0014 // Avoid using banned types in public API
|
55 |
| - public override void Write(Utf8JsonWriter writer, IJsonModel<object> value, JsonSerializerOptions options) |
| 55 | + public override void Write(Utf8JsonWriter writer, IJsonModel<object> value, JsonSerializerOptions options) |
56 | 56 | #pragma warning restore AZC0014 // Avoid using banned types in public API
|
57 |
| - { |
58 |
| - value.Write(writer, Options); |
59 |
| - } |
| 57 | + { |
| 58 | + value.Write(writer, Options); |
60 | 59 | }
|
61 | 60 | }
|
0 commit comments