winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

JsonObjectExtensions.cs (2442B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="JsonObjectExtensions.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.Processor.Extensions
      8 {
      9     using System.Text.Json;
     10     using System.Text.Json.Nodes;
     11     using Windows.Foundation.Collections;
     12 
     13     /// <summary>
     14     /// Extensions for JsonObject.
     15     /// </summary>
     16     internal static class JsonObjectExtensions
     17     {
     18         /// <summary>
     19         /// Converts the JSON object to a ValueSet.
     20         /// </summary>
     21         /// <param name="jsonObject">The object to convert.</param>
     22         /// <returns>The ValueSet.</returns>
     23         public static ValueSet ToValueSet(this JsonObject? jsonObject)
     24         {
     25             ValueSet result = new ValueSet();
     26 
     27             if (jsonObject != null)
     28             {
     29                 foreach (var item in jsonObject)
     30                 {
     31                     result.Add(item.Key, ToValue(item.Value));
     32                 }
     33             }
     34 
     35             return result;
     36         }
     37 
     38         private static object? ToValue(JsonNode? node) => node switch
     39         {
     40             JsonObject obj => obj.ToValueSet(),
     41             JsonArray array => ToValueSet(array),
     42             JsonValue value => ToValue(value),
     43             _ => null,
     44         };
     45 
     46         private static ValueSet ToValueSet(JsonArray array)
     47         {
     48             ValueSet result = new ValueSet();
     49             result.Add(ValueSetExtensions.TreatAsArray, true);
     50 
     51             int index = 0;
     52             foreach (var item in array)
     53             {
     54                 result.Add(index.ToString(), ToValue(item));
     55                 ++index;
     56             }
     57 
     58             return result;
     59         }
     60 
     61         private static object? ToValue(JsonValue value) => value.GetValueKind() switch
     62         {
     63             JsonValueKind.Null => null,
     64             JsonValueKind.Undefined => null,
     65             JsonValueKind.String => value.GetValue<string>(),
     66             JsonValueKind.Number => value.GetValue<long>(),
     67             JsonValueKind.True => true,
     68             JsonValueKind.False => false,
     69             _ => throw new System.NotImplementedException("Unexpected default case")
     70         };
     71     }
     72 }