< Summary

Information
Class: ArturRios.Extensions.GenericExtensions
Assembly: ArturRios.Extensions
File(s): /home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/GenericExtensions.cs
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 63
Line coverage: 100%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage
100%
Covered methods: 3
Fully covered methods: 3
Total methods: 3
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Clone(...)100%11100%
.cctor()100%11100%
CreateProperty(...)50%22100%

File(s)

/home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/GenericExtensions.cs

#LineLine coverage
 1using System.Reflection;
 2using Newtonsoft.Json;
 3using Newtonsoft.Json.Serialization;
 4
 5namespace ArturRios.Extensions;
 6
 7/// <summary>
 8///     Provides generic helper extensions, including deep-cloning via JSON serialization.
 9/// </summary>
 10public static class GenericExtensions
 11{
 12    /// <summary>
 13    ///     Creates a deep clone of the provided object via JSON serialization.
 14    /// </summary>
 15    /// <typeparam name="T">Type of the object.</typeparam>
 16    /// <param name="source">The instance to clone.</param>
 17    /// <returns>
 18    ///     A cloned instance of <typeparamref name="T"/>, or <c>null</c> when <paramref name="source"/> is
 19    ///     <c>null</c>.
 20    /// </returns>
 21    /// <exception cref="JsonException">
 22    ///     The value cannot be round-tripped through JSON — an object graph containing a reference cycle, for
 23    ///     example. Failures are reported rather than turned into <c>null</c>, which would be
 24    ///     indistinguishable from cloning a null.
 25    /// </exception>
 26    /// <remarks>
 27    ///     The clone is only as faithful as the JSON round trip: members the serializer ignores are absent from
 28    ///     the result, and reference identity between members of the graph is not preserved. A settable member
 29    ///     is replaced with the value read back, so a collection the constructor fills holds the source's items
 30    ///     once; a collection with no setter can only be added to, so one the constructor fills also keeps the
 31    ///     constructor's items.
 32    /// </remarks>
 33    public static T? Clone<T>(this T source)
 34    {
 1835        var serialized = JsonConvert.SerializeObject(source, CloneSettings);
 36
 1637        return JsonConvert.DeserializeObject<T>(serialized, CloneSettings);
 38    }
 39
 40    // Json.NET's default ObjectCreationHandling.Auto reuses a collection the constructor already created and
 41    // adds the items it reads to it: a List<string> initialized to ["default"] came back as
 42    // ["default", "default", "x"], and one the caller had emptied came back as ["default"].
 243    private static readonly JsonSerializerSettings CloneSettings = new()
 244    {
 245        ContractResolver = new ReplaceSettableMembersResolver()
 246    };
 47
 48    private sealed class ReplaceSettableMembersResolver : DefaultContractResolver
 49    {
 50        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
 51        {
 1852            var property = base.CreateProperty(member, memberSerialization);
 53
 54            // Replace only what can be set: with no setter, Replace would drop the member's value altogether.
 1855            if (property.Writable)
 56            {
 1857                property.ObjectCreationHandling = ObjectCreationHandling.Replace;
 58            }
 59
 1860            return property;
 61        }
 62    }
 63}