< Summary

Class:Azure.Core.Serialization.NewtonsoftJsonObjectSerializer
Assembly:Microsoft.Azure.Core.NewtonsoftJson
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Azure.Core.NewtonsoftJson\src\Serialization\NewtonsoftJsonObjectSerializer.cs
Covered lines:37
Uncovered lines:2
Coverable lines:39
Total lines:108
Line coverage:94.8% (37 of 39)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor()-0%100%
.ctor(...)-100%100%
Deserialize(...)-100%100%
DeserializeAsync(...)-100%100%
Serialize(...)-100%100%
SerializeAsync(...)-100%100%
Azure.Core.Serialization.IMemberNameConverter.ConvertMemberName(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Azure.Core.NewtonsoftJson\src\Serialization\NewtonsoftJsonObjectSerializer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Concurrent;
 6using System.IO;
 7using System.Reflection;
 8using System.Text;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using Newtonsoft.Json;
 12using Newtonsoft.Json.Serialization;
 13
 14namespace 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.
 224        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>
 032        public NewtonsoftJsonObjectSerializer() : this(new JsonSerializerSettings())
 33        {
 034        }
 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>
 841        public NewtonsoftJsonObjectSerializer(JsonSerializerSettings settings)
 42        {
 843            Argument.AssertNotNull(settings, nameof(settings));
 44
 445            _cache = new ConcurrentDictionary<MemberInfo, string?>();
 446            _serializer = JsonSerializer.Create(settings);
 447        }
 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        {
 853            Argument.AssertNotNull(stream, nameof(stream));
 854            Argument.AssertNotNull(returnType, nameof(returnType));
 55
 856            using StreamReader reader = new StreamReader(stream, UTF8NoBOM, true, DefaultBufferSize, true);
 857            return _serializer.Deserialize(reader, returnType);
 858        }
 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
 463            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        {
 869            Argument.AssertNotNull(stream, nameof(stream));
 870            Argument.AssertNotNull(inputType, nameof(inputType));
 71
 872            using StreamWriter writer = new StreamWriter(stream, UTF8NoBOM, DefaultBufferSize, true);
 873            _serializer.Serialize(writer, value, inputType);
 1674        }
 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        {
 480            Serialize(stream, value, inputType, cancellationToken);
 481            return new ValueTask();
 82        }
 83
 84        /// <inheritdoc/>
 85        string? IMemberNameConverter.ConvertMemberName(MemberInfo member)
 86        {
 4487            Argument.AssertNotNull(member, nameof(member));
 88
 4489            return _cache.GetOrAdd(member, m =>
 4490            {
 8891                if (_serializer.ContractResolver.ResolveContract(m.ReflectedType) is JsonObjectContract contract)
 4492                {
 58893                    foreach (JsonProperty property in contract.Properties)
 4494                    {
 28495                        if (!property.Ignored &&
 28496                            string.Equals(property.UnderlyingName, m.Name, StringComparison.Ordinal) &&
 28497                            property.DeclaringType == m.DeclaringType)
 4498                        {
 6899                            return property.PropertyName;
 44100                        }
 44101                    }
 44102                }
 44103
 64104                return null;
 68105            });
 106        }
 107    }
 108}