| | 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; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Newtonsoft.Json; |
| | 12 | | using Newtonsoft.Json.Serialization; |
| | 13 | |
|
| | 14 | | namespace Azure.Core.Serialization |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// A <see cref="NewtonsoftJsonObjectSerializer"/> implementation that uses <see cref="JsonSerializer"/> to for seri |
| | 18 | | /// </summary> |
| | 19 | | public class NewtonsoftJsonObjectSerializer : ObjectSerializer, IMemberNameConverter |
| | 20 | | { |
| | 21 | | private const int DefaultBufferSize = 1024; |
| | 22 | |
|
| | 23 | | // Older StreamReader and StreamWriter would otherwise default to this. |
| 2 | 24 | | private static readonly Encoding UTF8NoBOM = new UTF8Encoding(false, true); |
| | 25 | |
|
| | 26 | | private readonly ConcurrentDictionary<MemberInfo, string?> _cache; |
| | 27 | | private readonly JsonSerializer _serializer; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes new instance of <see cref="NewtonsoftJsonObjectSerializer"/>. |
| | 31 | | /// </summary> |
| 0 | 32 | | public NewtonsoftJsonObjectSerializer() : this(new JsonSerializerSettings()) |
| | 33 | | { |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes new instance of <see cref="NewtonsoftJsonObjectSerializer"/>. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="settings">The <see cref="JsonSerializerSettings"/> instance to use when serializing/deserializi |
| | 40 | | /// <exception cref="ArgumentNullException"><paramref name="settings"/> is null.</exception> |
| 8 | 41 | | public NewtonsoftJsonObjectSerializer(JsonSerializerSettings settings) |
| | 42 | | { |
| 8 | 43 | | Argument.AssertNotNull(settings, nameof(settings)); |
| | 44 | |
|
| 4 | 45 | | _cache = new ConcurrentDictionary<MemberInfo, string?>(); |
| 4 | 46 | | _serializer = JsonSerializer.Create(settings); |
| 4 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <inheritdoc /> |
| | 50 | | /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="returnType"/> is null.< |
| | 51 | | public override object Deserialize(Stream stream, Type returnType, CancellationToken cancellationToken) |
| | 52 | | { |
| 8 | 53 | | Argument.AssertNotNull(stream, nameof(stream)); |
| 8 | 54 | | Argument.AssertNotNull(returnType, nameof(returnType)); |
| | 55 | |
|
| 8 | 56 | | using StreamReader reader = new StreamReader(stream, UTF8NoBOM, true, DefaultBufferSize, true); |
| 8 | 57 | | return _serializer.Deserialize(reader, returnType); |
| 8 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <inheritdoc /> |
| | 61 | | /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="returnType"/> is null.< |
| | 62 | | public override ValueTask<object> DeserializeAsync(Stream stream, Type returnType, CancellationToken cancellatio |
| 4 | 63 | | new ValueTask<object>(Deserialize(stream, returnType, cancellationToken)); |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| | 66 | | /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="inputType"/> is null.</ |
| | 67 | | public override void Serialize(Stream stream, object? value, Type inputType, CancellationToken cancellationToken |
| | 68 | | { |
| 8 | 69 | | Argument.AssertNotNull(stream, nameof(stream)); |
| 8 | 70 | | Argument.AssertNotNull(inputType, nameof(inputType)); |
| | 71 | |
|
| 8 | 72 | | using StreamWriter writer = new StreamWriter(stream, UTF8NoBOM, DefaultBufferSize, true); |
| 8 | 73 | | _serializer.Serialize(writer, value, inputType); |
| 16 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <inheritdoc /> |
| | 77 | | /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="inputType"/> is null.</ |
| | 78 | | public override ValueTask SerializeAsync(Stream stream, object? value, Type inputType, CancellationToken cancell |
| | 79 | | { |
| 4 | 80 | | Serialize(stream, value, inputType, cancellationToken); |
| 4 | 81 | | return new ValueTask(); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | /// <inheritdoc/> |
| | 85 | | string? IMemberNameConverter.ConvertMemberName(MemberInfo member) |
| | 86 | | { |
| 44 | 87 | | Argument.AssertNotNull(member, nameof(member)); |
| | 88 | |
|
| 44 | 89 | | return _cache.GetOrAdd(member, m => |
| 44 | 90 | | { |
| 88 | 91 | | if (_serializer.ContractResolver.ResolveContract(m.ReflectedType) is JsonObjectContract contract) |
| 44 | 92 | | { |
| 588 | 93 | | foreach (JsonProperty property in contract.Properties) |
| 44 | 94 | | { |
| 284 | 95 | | if (!property.Ignored && |
| 284 | 96 | | string.Equals(property.UnderlyingName, m.Name, StringComparison.Ordinal) && |
| 284 | 97 | | property.DeclaringType == m.DeclaringType) |
| 44 | 98 | | { |
| 68 | 99 | | return property.PropertyName; |
| 44 | 100 | | } |
| 44 | 101 | | } |
| 44 | 102 | | } |
| 44 | 103 | |
|
| 64 | 104 | | return null; |
| 68 | 105 | | }); |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |