diff options
Diffstat (limited to 'src/WixToolset.Data/IntermediateFieldValue.cs')
-rw-r--r-- | src/WixToolset.Data/IntermediateFieldValue.cs | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/WixToolset.Data/IntermediateFieldValue.cs b/src/WixToolset.Data/IntermediateFieldValue.cs new file mode 100644 index 00000000..2064afec --- /dev/null +++ b/src/WixToolset.Data/IntermediateFieldValue.cs | |||
@@ -0,0 +1,131 @@ | |||
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 System.Diagnostics; | ||
7 | using SimpleJson; | ||
8 | |||
9 | [DebuggerDisplay("{Data}")] | ||
10 | public class IntermediateFieldValue | ||
11 | { | ||
12 | public string Context { get; internal set; } | ||
13 | |||
14 | internal object Data { get; set; } | ||
15 | |||
16 | public IntermediateFieldValue PreviousValue { get; internal set; } | ||
17 | |||
18 | public static explicit operator bool(IntermediateFieldValue value) | ||
19 | { | ||
20 | return value.AsBool(); | ||
21 | } | ||
22 | |||
23 | public static explicit operator bool? (IntermediateFieldValue value) | ||
24 | { | ||
25 | return value.AsNullableBool(); | ||
26 | } | ||
27 | |||
28 | public static explicit operator int(IntermediateFieldValue value) | ||
29 | { | ||
30 | return value.AsNumber(); | ||
31 | } | ||
32 | |||
33 | public static explicit operator int? (IntermediateFieldValue value) | ||
34 | { | ||
35 | return value.AsNullableNumber(); | ||
36 | } | ||
37 | |||
38 | public static explicit operator IntermediateFieldPathValue(IntermediateFieldValue value) | ||
39 | { | ||
40 | return value.AsPath(); | ||
41 | } | ||
42 | |||
43 | public static explicit operator string(IntermediateFieldValue value) | ||
44 | { | ||
45 | return value.AsString(); | ||
46 | } | ||
47 | |||
48 | internal static IntermediateFieldValue Deserialize(JsonObject jsonObject) | ||
49 | { | ||
50 | var context = jsonObject.GetValueOrDefault<string>("context"); | ||
51 | if (!jsonObject.TryGetValue("data", out var data)) | ||
52 | { | ||
53 | throw new ArgumentException(); | ||
54 | } | ||
55 | |||
56 | var value = data; | ||
57 | |||
58 | if (data is JsonObject jsonData) | ||
59 | { | ||
60 | Uri baseUri = null; | ||
61 | |||
62 | if (jsonData.TryGetValue("baseUri", out var baseUriValue) && baseUriValue is string) | ||
63 | { | ||
64 | baseUri = new Uri((string)baseUriValue); | ||
65 | } | ||
66 | jsonData.TryGetValue("embeddedIndex", out var embeddedIndex); | ||
67 | |||
68 | value = new IntermediateFieldPathValue | ||
69 | { | ||
70 | BaseUri = baseUri, | ||
71 | EmbeddedFileIndex = (int?)embeddedIndex, | ||
72 | Path = jsonData.GetValueOrDefault<string>("path"), | ||
73 | }; | ||
74 | } | ||
75 | |||
76 | var previousValueJson = jsonObject.GetValueOrDefault<JsonObject>("prev"); | ||
77 | var previousValue = (previousValueJson == null) ? null : IntermediateFieldValue.Deserialize(previousValueJson); | ||
78 | |||
79 | return new IntermediateFieldValue | ||
80 | { | ||
81 | Context = context, | ||
82 | Data = value, | ||
83 | PreviousValue = previousValue | ||
84 | }; | ||
85 | } | ||
86 | |||
87 | internal JsonObject Serialize() | ||
88 | { | ||
89 | var jsonObject = new JsonObject(); | ||
90 | |||
91 | if (!String.IsNullOrEmpty(this.Context)) | ||
92 | { | ||
93 | jsonObject.Add("context", this.Context); | ||
94 | } | ||
95 | |||
96 | if (this.Data is IntermediateFieldPathValue pathField) | ||
97 | { | ||
98 | var jsonData = new JsonObject(); | ||
99 | |||
100 | if (pathField.BaseUri != null) | ||
101 | { | ||
102 | jsonData.Add("baseUri", pathField.BaseUri.AbsoluteUri); | ||
103 | } | ||
104 | |||
105 | if (pathField.EmbeddedFileIndex.HasValue) | ||
106 | { | ||
107 | jsonData.Add("embeddedIndex", pathField.EmbeddedFileIndex.Value); | ||
108 | } | ||
109 | |||
110 | if (!String.IsNullOrEmpty(pathField.Path)) | ||
111 | { | ||
112 | jsonData.Add("path", pathField.Path); | ||
113 | } | ||
114 | |||
115 | jsonObject.Add("data", jsonData); | ||
116 | } | ||
117 | else | ||
118 | { | ||
119 | jsonObject.Add("data", this.Data); | ||
120 | } | ||
121 | |||
122 | if (this.PreviousValue != null) | ||
123 | { | ||
124 | var previousValueJson = this.PreviousValue.Serialize(); | ||
125 | jsonObject.Add("prev", previousValueJson); | ||
126 | } | ||
127 | |||
128 | return jsonObject; | ||
129 | } | ||
130 | } | ||
131 | } | ||