1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// 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.
namespace WixToolset.Data
{
using System;
using System.Diagnostics;
using SimpleJson;
[DebuggerDisplay("{Data}")]
public class IntermediateFieldValue
{
public string Context { get; internal set; }
internal object Data { get; set; }
public IntermediateFieldValue PreviousValue { get; internal set; }
public static explicit operator bool(IntermediateFieldValue value) => value.AsBool();
public static explicit operator bool? (IntermediateFieldValue value) => value.AsNullableBool();
public static explicit operator int(IntermediateFieldValue value) => value.AsNumber();
public static explicit operator int? (IntermediateFieldValue value) => value.AsNullableNumber();
public static explicit operator IntermediateFieldPathValue(IntermediateFieldValue value) => value.AsPath();
public static explicit operator string(IntermediateFieldValue value) => value.AsString();
internal static IntermediateFieldValue Deserialize(JsonObject jsonObject, Uri baseUri, IntermediateFieldType type)
{
var context = jsonObject.GetValueOrDefault<string>("context");
if (!jsonObject.TryGetValue("data", out var data))
{
throw new ArgumentException();
}
var value = data;
switch (value)
{
case int intData:
switch (type)
{
case IntermediateFieldType.Bool:
value = intData != 0;
break;
case IntermediateFieldType.LargeNumber:
value = Convert.ToInt64(data);
break;
case IntermediateFieldType.Path:
case IntermediateFieldType.String:
value = intData.ToString();
break;
}
break;
case long longData:
switch (type)
{
case IntermediateFieldType.Bool:
value = longData != 0;
break;
case IntermediateFieldType.Number:
value = Convert.ToInt32(longData);
break;
case IntermediateFieldType.Path:
case IntermediateFieldType.String:
value = longData.ToString();
break;
}
break;
case JsonObject jsonData:
jsonData.TryGetValue("embed", out var embed);
value = new IntermediateFieldPathValue
{
BaseUri = (embed != null) ? baseUri : null,
Embed = embed != null,
Path = jsonData.GetValueOrDefault<string>("path"),
};
break;
// Nothing to do for this case, so leave it out.
// case string stringData:
// break;
}
var previousValueJson = jsonObject.GetValueOrDefault<JsonObject>("prev");
var previousValue = (previousValueJson == null) ? null : IntermediateFieldValue.Deserialize(previousValueJson, baseUri, type);
return new IntermediateFieldValue
{
Context = context,
Data = value,
PreviousValue = previousValue
};
}
internal JsonObject Serialize()
{
var jsonObject = new JsonObject();
if (!String.IsNullOrEmpty(this.Context))
{
jsonObject.Add("context", this.Context);
}
if (this.Data is IntermediateFieldPathValue pathField)
{
var jsonData = new JsonObject();
// pathField.BaseUri is set during load, not saved.
if (pathField.Embed)
{
jsonData.Add("embed", "true");
}
if (!String.IsNullOrEmpty(pathField.Path))
{
jsonData.Add("path", pathField.Path);
}
jsonObject.Add("data", jsonData);
}
else
{
jsonObject.Add("data", this.Data);
}
if (this.PreviousValue != null)
{
var previousValueJson = this.PreviousValue.Serialize();
jsonObject.Add("prev", previousValueJson);
}
return jsonObject;
}
}
}
|