Policy.Autorest/custom/SerializationHelpers.cs
|
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information. // Code generated by Microsoft (R) AutoRest Code Generator. // Changes may cause incorrect behavior and will be lost if the code is regenerated. namespace Microsoft.Azure.PowerShell.Cmdlets.Policy.Models { using System; using System.Collections; using System.Collections.Generic; /// <summary> /// Class containing static methods useful for serialization tasks /// </summary> public class SerializationHelpers { /// <summary> /// SetSentinel methods deep-grovel the given object looking for properties with null values. /// Every such property is replaced with a DateTime with a Year value of 1. This sentinel value /// convinces the autorest serializer to serialize a null value. /// </summary> /// <param name="inputObject">Input object under serialization.</param> public static void SetSentinel(Hashtable inputObject) { var updates = new List<KeyValuePair<string, object>>(); foreach (var key in inputObject.Keys) { var item = new KeyValuePair<string, object>(key.ToString(), inputObject[key]); if (item.Value == null) { updates.Add(item); } else if (item.Value is Hashtable childHashtable) { SerializationHelpers.SetSentinel(childHashtable); } else if (item.Value is Dictionary<string, object> child) { SerializationHelpers.SetSentinel(child); } else if (item.Value is object[]) { SerializationHelpers.SetSentinel((object[])item.Value); } } foreach (var item in updates) { inputObject[item.Key] = new DateTime(1, 1, 1); } } /// <summary> /// SetSentinel methods deep-grovel the given object looking for properties with null values. /// Every such property is replaced with a DateTime with a Year value of 1. This sentinel value /// convinces the autorest serializer to serialize a null value. /// </summary> /// <param name="inputObject">Input object under serialization.</param> public static void SetSentinel(IDictionary<string, object> inputObject) { var updates = new List<KeyValuePair<string, object>>(); foreach (var key in inputObject.Keys) { var item = new KeyValuePair<string, object>(key.ToString(), inputObject[key]); if (item.Value == null) { updates.Add(item); } else if (item.Value is Hashtable childHashtable) { SerializationHelpers.SetSentinel(childHashtable); } else if (item.Value is Dictionary<string, object> child) { SerializationHelpers.SetSentinel(child); } else if (item.Value is object[]) { SerializationHelpers.SetSentinel((object[])item.Value); } } foreach (var item in updates) { inputObject[item.Key] = new DateTime(1, 1, 1); } } /// <summary> /// SetSentinel methods deep-grovel the given object looking for properties with null values. /// Every such property is replaced with a DateTime with a Year value of 1. This sentinel value /// convinces the autorest serializer to serialize a null value. /// </summary> /// <param name="inputObject">Input object under serialization.</param> public static void SetSentinel(object[] inputObject) { foreach (var item in inputObject) { if (item is Hashtable childHashtable) { SerializationHelpers.SetSentinel(childHashtable); } else if (item is Dictionary<string, object> child) { SerializationHelpers.SetSentinel(child); } } } /// <summary> /// RestoreSentinel reverses the effect of <see cref="SetSentinel(IDictionary{string, object})"/>: /// any value that was replaced with a <see cref="DateTime"/> sentinel (Year == 1) is set back to <c>null</c>. /// Call this after serialization to avoid permanently mutating the in-memory object. /// </summary> public static void RestoreSentinel(IDictionary<string, object> inputObject) { var updates = new List<KeyValuePair<string, object>>(); foreach (var key in inputObject.Keys) { var value = inputObject[key]; if (value is DateTime dt && dt.Year == 1) { updates.Add(new KeyValuePair<string, object>(key, null)); } else if (value is Hashtable childHashtable) { RestoreSentinel(childHashtable); } else if (value is Dictionary<string, object> child) { RestoreSentinel(child); } else if (value is object[] arr) { RestoreSentinel(arr); } } foreach (var item in updates) { inputObject[item.Key] = null; } } /// <summary> /// RestoreSentinel reverses the effect of <see cref="SetSentinel(Hashtable)"/>: /// any value that was replaced with a <see cref="DateTime"/> sentinel (Year == 1) is set back to <c>null</c>. /// </summary> public static void RestoreSentinel(Hashtable inputObject) { var updates = new List<KeyValuePair<string, object>>(); foreach (var key in inputObject.Keys) { var value = inputObject[key]; if (value is DateTime dt && dt.Year == 1) { updates.Add(new KeyValuePair<string, object>(key.ToString(), null)); } else if (value is Hashtable childHashtable) { RestoreSentinel(childHashtable); } else if (value is Dictionary<string, object> child) { RestoreSentinel(child); } else if (value is object[] arr) { RestoreSentinel(arr); } } foreach (var item in updates) { inputObject[item.Key] = null; } } /// <summary> /// RestoreSentinel reverses the effect of <see cref="SetSentinel(object[])"/> on nested dictionaries within an array. /// </summary> public static void RestoreSentinel(object[] inputObject) { foreach (var item in inputObject) { if (item is Hashtable childHashtable) { RestoreSentinel(childHashtable); } else if (item is Dictionary<string, object> child) { RestoreSentinel(child); } } } } } |