aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/Json/JsonObjectExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/Json/JsonObjectExtensions.cs')
-rw-r--r--src/WixToolset.Data/Json/JsonObjectExtensions.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/WixToolset.Data/Json/JsonObjectExtensions.cs b/src/WixToolset.Data/Json/JsonObjectExtensions.cs
new file mode 100644
index 00000000..091d90f4
--- /dev/null
+++ b/src/WixToolset.Data/Json/JsonObjectExtensions.cs
@@ -0,0 +1,29 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixToolset.Data
4{
5 using System;
6 using SimpleJson;
7
8 internal static class JsonObjectExtensions
9 {
10 public static int GetValueOrDefault(this JsonObject jsonObject, string key, int defaultValue)
11 {
12 return jsonObject.TryGetValue(key, out var value) ? Convert.ToInt32(value) : defaultValue;
13 }
14
15 public static T GetValueOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue = default(T)) where T : class
16 {
17 return jsonObject.TryGetValue(key, out var value) ? value as T: defaultValue;
18 }
19
20 public static T GetEnumOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue) where T : struct
21 {
22#if DEBUG
23 if (!typeof(T).IsEnum) throw new ArgumentException("This method is designed to only only support enums.", nameof(T));
24#endif
25 var value = jsonObject.GetValueOrDefault<string>(key);
26 return Enum.TryParse(value, true, out T e) ? e : defaultValue;
27 }
28 }
29}