| | | 1 | | using System.Reflection; |
| | | 2 | | using Newtonsoft.Json; |
| | | 3 | | using Newtonsoft.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides generic helper extensions, including deep-cloning via JSON serialization. |
| | | 9 | | /// </summary> |
| | | 10 | | public 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 | | { |
| | 18 | 35 | | var serialized = JsonConvert.SerializeObject(source, CloneSettings); |
| | | 36 | | |
| | 16 | 37 | | 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"]. |
| | 2 | 43 | | private static readonly JsonSerializerSettings CloneSettings = new() |
| | 2 | 44 | | { |
| | 2 | 45 | | ContractResolver = new ReplaceSettableMembersResolver() |
| | 2 | 46 | | }; |
| | | 47 | | |
| | | 48 | | private sealed class ReplaceSettableMembersResolver : DefaultContractResolver |
| | | 49 | | { |
| | | 50 | | protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) |
| | | 51 | | { |
| | 18 | 52 | | 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. |
| | 18 | 55 | | if (property.Writable) |
| | | 56 | | { |
| | 18 | 57 | | property.ObjectCreationHandling = ObjectCreationHandling.Replace; |
| | | 58 | | } |
| | | 59 | | |
| | 18 | 60 | | return property; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | } |