diff options
Diffstat (limited to 'src/api/wix/WixToolset.Data/Json/JsonObjectExtensions.cs')
-rw-r--r-- | src/api/wix/WixToolset.Data/Json/JsonObjectExtensions.cs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/api/wix/WixToolset.Data/Json/JsonObjectExtensions.cs b/src/api/wix/WixToolset.Data/Json/JsonObjectExtensions.cs new file mode 100644 index 00000000..bc52333c --- /dev/null +++ b/src/api/wix/WixToolset.Data/Json/JsonObjectExtensions.cs | |||
@@ -0,0 +1,79 @@ | |||
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 | |||
3 | namespace WixToolset.Data | ||
4 | { | ||
5 | using System; | ||
6 | using SimpleJson; | ||
7 | |||
8 | internal static class JsonObjectExtensions | ||
9 | { | ||
10 | public static JsonObject AddNonDefaultValue(this JsonObject jsonObject, string key, bool value, bool defaultValue = default(bool)) | ||
11 | { | ||
12 | if (value != defaultValue) | ||
13 | { | ||
14 | jsonObject.Add(key, value); | ||
15 | } | ||
16 | |||
17 | return jsonObject; | ||
18 | } | ||
19 | |||
20 | public static JsonObject AddNonDefaultValue(this JsonObject jsonObject, string key, int value, int defaultValue = default(int)) | ||
21 | { | ||
22 | if (value != defaultValue) | ||
23 | { | ||
24 | jsonObject.Add(key, value); | ||
25 | } | ||
26 | |||
27 | return jsonObject; | ||
28 | } | ||
29 | |||
30 | public static JsonObject AddNonDefaultValue(this JsonObject jsonObject, string key, object value, object defaultValue = null) | ||
31 | { | ||
32 | if (value != defaultValue) | ||
33 | { | ||
34 | jsonObject.Add(key, value); | ||
35 | } | ||
36 | |||
37 | return jsonObject; | ||
38 | } | ||
39 | |||
40 | public static JsonObject AddIsNotNullOrEmpty(this JsonObject jsonObject, string key, string value) | ||
41 | { | ||
42 | if (!String.IsNullOrEmpty(value)) | ||
43 | { | ||
44 | jsonObject.Add(key, value); | ||
45 | } | ||
46 | |||
47 | return jsonObject; | ||
48 | } | ||
49 | |||
50 | public static bool GetValueOrDefault(this JsonObject jsonObject, string key, bool defaultValue) | ||
51 | { | ||
52 | return jsonObject.TryGetValue(key, out var value) ? Convert.ToBoolean(value) : defaultValue; | ||
53 | } | ||
54 | |||
55 | public static int GetValueOrDefault(this JsonObject jsonObject, string key, int defaultValue) | ||
56 | { | ||
57 | return jsonObject.TryGetValue(key, out var value) ? Convert.ToInt32(value) : defaultValue; | ||
58 | } | ||
59 | |||
60 | public static int? GetValueOrDefault(this JsonObject jsonObject, string key, int? defaultValue) | ||
61 | { | ||
62 | return jsonObject.TryGetValue(key, out var value) ? Convert.ToInt32(value) : defaultValue; | ||
63 | } | ||
64 | |||
65 | public static T GetValueOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue = default(T)) where T : class | ||
66 | { | ||
67 | return jsonObject.TryGetValue(key, out var value) ? value as T : defaultValue; | ||
68 | } | ||
69 | |||
70 | public static T GetEnumOrDefault<T>(this JsonObject jsonObject, string key, T defaultValue) where T : struct | ||
71 | { | ||
72 | #if DEBUG | ||
73 | if (!typeof(T).IsEnum) { throw new ArgumentException("This method only supports enums.", nameof(T)); } | ||
74 | #endif | ||
75 | var value = jsonObject.GetValueOrDefault<string>(key); | ||
76 | return Enum.TryParse(value, true, out T e) ? e : defaultValue; | ||
77 | } | ||
78 | } | ||
79 | } | ||