| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Concurrent; |
| | 6 | | using System.IO; |
| | 7 | | using System.Reflection; |
| | 8 | | using System.Text.Json; |
| | 9 | | using System.Text.Json.Serialization; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | namespace Azure.Core.Serialization |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// A <see cref="JsonObjectSerializer"/> implementation that uses <see cref="JsonSerializer"/> to for serialization/ |
| | 17 | | /// </summary> |
| | 18 | | public class JsonObjectSerializer : ObjectSerializer, IMemberNameConverter |
| | 19 | | { |
| | 20 | | private readonly ConcurrentDictionary<MemberInfo, string?> _cache; |
| | 21 | | private readonly JsonSerializerOptions _options; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes new instance of <see cref="JsonObjectSerializer"/>. |
| | 25 | | /// </summary> |
| 68 | 26 | | public JsonObjectSerializer() : this(new JsonSerializerOptions()) |
| | 27 | | { |
| 68 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Initializes new instance of <see cref="JsonObjectSerializer"/>. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="options">The <see cref="JsonSerializerOptions"/> instance to use when serializing/deserializing |
| | 34 | | /// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception> |
| 76 | 35 | | public JsonObjectSerializer(JsonSerializerOptions options) |
| | 36 | | { |
| 76 | 37 | | _options = options ?? throw new ArgumentNullException(nameof(options)); |
| | 38 | |
|
| | 39 | | // TODO: Consider using WeakReference cache to allow the GC to collect if the JsonObjectSerialized is held f |
| 72 | 40 | | _cache = new ConcurrentDictionary<MemberInfo, string?>(); |
| 72 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| | 44 | | public override void Serialize(Stream stream, object? value, Type inputType, CancellationToken cancellationToken |
| | 45 | | { |
| 12 | 46 | | var buffer = JsonSerializer.SerializeToUtf8Bytes(value, inputType, _options); |
| 12 | 47 | | stream.Write(buffer, 0, buffer.Length); |
| 12 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| | 51 | | public override async ValueTask SerializeAsync(Stream stream, object? value, Type inputType, CancellationToken c |
| | 52 | | { |
| 10 | 53 | | await JsonSerializer.SerializeAsync(stream, value, inputType, _options, cancellationToken).ConfigureAwait(fa |
| 10 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <inheritdoc /> |
| | 57 | | public override object Deserialize(Stream stream, Type returnType, CancellationToken cancellationToken) |
| | 58 | | { |
| 56 | 59 | | using var memoryStream = new MemoryStream(); |
| 56 | 60 | | stream.CopyTo(memoryStream); |
| 56 | 61 | | return JsonSerializer.Deserialize(memoryStream.ToArray(), returnType, _options); |
| 52 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <inheritdoc /> |
| | 65 | | public override async ValueTask<object> DeserializeAsync(Stream stream, Type returnType, CancellationToken cance |
| | 66 | | { |
| 56 | 67 | | return await JsonSerializer.DeserializeAsync(stream, returnType, _options, cancellationToken).ConfigureAwait |
| 52 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <inheritdoc/> |
| | 71 | | string? IMemberNameConverter.ConvertMemberName(MemberInfo member) |
| | 72 | | { |
| 32 | 73 | | Argument.AssertNotNull(member, nameof(member)); |
| | 74 | |
|
| 32 | 75 | | return _cache.GetOrAdd(member, m => |
| 32 | 76 | | { |
| 32 | 77 | | // Mimics property enumeration based on: |
| 32 | 78 | | // * https://github.com/dotnet/corefx/blob/v3.1.0/src/System.Text.Json/src/System/Text/Json/Serializatio |
| 32 | 79 | | // * TODO: Add support for fields when .NET 5 GAs (https://github.com/Azure/azure-sdk-for-net/issues/136 |
| 32 | 80 | |
|
| 64 | 81 | | if (m is PropertyInfo propertyInfo) |
| 32 | 82 | | { |
| 32 | 83 | | // Ignore indexers. |
| 52 | 84 | | if (propertyInfo.GetIndexParameters().Length > 0) |
| 32 | 85 | | { |
| 0 | 86 | | return null; |
| 32 | 87 | | } |
| 32 | 88 | |
|
| 32 | 89 | | // Only support public getters and/or setters. |
| 52 | 90 | | if (propertyInfo.GetMethod?.IsPublic == true || |
| 52 | 91 | | propertyInfo.SetMethod?.IsPublic == true) |
| 32 | 92 | | { |
| 48 | 93 | | if (propertyInfo.GetCustomAttribute<JsonIgnoreAttribute>() != null) |
| 32 | 94 | | { |
| 36 | 95 | | return null; |
| 32 | 96 | | } |
| 32 | 97 | |
|
| 32 | 98 | | // Ignore - but do not assert correctness - for JsonExtensionDataAttribute based on |
| 32 | 99 | | // https://github.com/dotnet/corefx/blob/v3.1.0/src/System.Text.Json/src/System/Text/Json/Serial |
| 44 | 100 | | if (propertyInfo.GetCustomAttribute<JsonExtensionDataAttribute>() != null) |
| 32 | 101 | | { |
| 0 | 102 | | return null; |
| 32 | 103 | | } |
| 32 | 104 | |
|
| 32 | 105 | | // No need to validate collisions since they are based on the serialized name. |
| 44 | 106 | | return GetPropertyName(propertyInfo); |
| 32 | 107 | | } |
| 32 | 108 | | } |
| 32 | 109 | |
|
| 32 | 110 | | // The member is unsupported or ignored. |
| 48 | 111 | | return null; |
| 32 | 112 | | }); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | private string GetPropertyName(MemberInfo memberInfo) |
| | 116 | | { |
| | 117 | | // Mimics property name determination based on |
| | 118 | | // https://github.com/dotnet/runtime/blob/dc8b6f90/src/libraries/System.Text.Json/src/System/Text/Json/Seria |
| | 119 | |
|
| 12 | 120 | | JsonPropertyNameAttribute nameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(false); |
| 12 | 121 | | if (nameAttribute != null) |
| | 122 | | { |
| 4 | 123 | | return nameAttribute.Name |
| 4 | 124 | | ?? throw new InvalidOperationException($"The JSON property name for '{memberInfo.DeclaringType}.{mem |
| | 125 | | } |
| 8 | 126 | | else if (_options.PropertyNamingPolicy != null) |
| | 127 | | { |
| 4 | 128 | | return _options.PropertyNamingPolicy.ConvertName(memberInfo.Name) |
| 4 | 129 | | ?? throw new InvalidOperationException($"The JSON property name for '{memberInfo.DeclaringType}.{mem |
| | 130 | | } |
| | 131 | | else |
| | 132 | | { |
| 4 | 133 | | return memberInfo.Name; |
| | 134 | | } |
| | 135 | | } |
| | 136 | | } |
| | 137 | | } |