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);
                }
            }
        }
    }
}