aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/IntermediateTuple.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/IntermediateTuple.cs')
-rw-r--r--src/WixToolset.Data/IntermediateTuple.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/WixToolset.Data/IntermediateTuple.cs b/src/WixToolset.Data/IntermediateTuple.cs
new file mode 100644
index 00000000..36cee2aa
--- /dev/null
+++ b/src/WixToolset.Data/IntermediateTuple.cs
@@ -0,0 +1,70 @@
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 SimpleJson;
6
7 public class IntermediateTuple
8 {
9 //public IntermediateTuple(IntermediateTupleDefinition definition) : this(definition, null, null)
10 //{
11 //}
12
13 public IntermediateTuple(IntermediateTupleDefinition definition, SourceLineNumber sourceLineNumber, Identifier id = null)
14 {
15 this.Definition = definition;
16 this.Fields = new IntermediateField[definition.FieldDefinitions.Length];
17 this.SourceLineNumbers = sourceLineNumber;
18 this.Id = id;
19 }
20
21 public IntermediateTupleDefinition Definition { get; }
22
23 public IntermediateField[] Fields { get; }
24
25 public SourceLineNumber SourceLineNumbers { get; set; }
26
27 public Identifier Id { get; set; }
28
29 public IntermediateField this[int index] => this.Fields[index];
30
31 internal static IntermediateTuple Deserialize(ITupleDefinitionCreator creator, JsonObject jsonObject)
32 {
33 var definitionName = jsonObject.GetValueOrDefault<string>("def");
34 var fieldsJson = jsonObject.GetValueOrDefault<JsonArray>("fields");
35
36 creator.TryGetTupleDefinitionByName(definitionName, out var definition); // TODO: this isn't sufficient.
37 var tuple = definition.CreateTuple();
38
39 for (var i = 0; i < fieldsJson.Count; ++i)
40 {
41 if (tuple.Fields.Length > i && fieldsJson[i] is JsonObject fieldJson)
42 {
43 tuple.Fields[i] = IntermediateField.Deserialize(tuple.Definition.FieldDefinitions[i], fieldJson);
44 }
45 }
46
47 return tuple;
48 }
49
50 internal JsonObject Serialize()
51 {
52 var jsonObject = new JsonObject
53 {
54 { "def", this.Definition.Name }
55 };
56
57 var fieldsJson = new JsonArray(this.Fields.Length);
58
59 foreach (var field in this.Fields)
60 {
61 var fieldJson = field?.Serialize();
62 fieldsJson.Add(fieldJson);
63 }
64
65 jsonObject.Add("fields", fieldsJson);
66
67 return jsonObject;
68 }
69 }
70}