-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareYamlToJson.cs
More file actions
179 lines (155 loc) · 5.76 KB
/
CompareYamlToJson.cs
File metadata and controls
179 lines (155 loc) · 5.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Uncomment to enable debugging tools
//#define TRACE_YAML_TO_JSON
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using FileMeta.Yaml;
namespace UnitTests
{
static class CompareYamlToJson
{
public static void Compare(string yamlFilename, string jsonFilename)
{
var yamlOptions = new YamlReaderOptions();
yamlOptions.MergeDocuments = true;
yamlOptions.IgnoreTextOutsideDocumentMarkers = true;
yamlOptions.CloseInput = true;
using (Stream yaml = new FileStream(yamlFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream json = new FileStream(jsonFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Compare(yaml, yamlOptions, json);
}
}
}
public static void Compare(Stream yamlStream, YamlReaderOptions yamlOptions, Stream jsonStream)
{
// Parse the YAML into a structure
JToken fromYaml = ReadJsonFromYaml(yamlStream, yamlOptions);
// Parse the JSON into a structure
JToken fromJson = ReadJsonFromJson(jsonStream);
CompareJsonTrees(fromYaml, fromJson);
}
static JToken ReadJsonFromYaml(Stream yamlStream, YamlReaderOptions yamlOptions)
{
return ReadJson(delegate {
yamlStream.Position = 0;
return new YamlJsonReader(new StreamReader(yamlStream, Encoding.UTF8, true, 512, true), yamlOptions);
});
}
static JToken ReadJsonFromJson(Stream jsonStream)
{
return ReadJson(delegate
{
jsonStream.Position = 0;
return new JsonTextReader(new StreamReader(jsonStream, Encoding.UTF8, true, 512, true));
});
}
static JToken ReadJson(Func<JsonReader> openReader)
{
try
{
using (var reader = openReader())
{
return JToken.ReadFrom(reader);
}
}
catch (JsonReaderException)
{
// See if it's an empty document
using (var reader = openReader())
{
if (!reader.Read())
{
return new JArray(); // Empty array. Not a perfect mapping but as close as I can get.
}
}
throw; // Re-throw the exception
}
}
static void CompareJsonTrees(JToken value, JToken expected)
{
// Perform type conversion
// (FileMeta.Yaml does not attempt to detect type as JSON does)
if (value.Type == JTokenType.String)
{
switch (expected.Type)
{
case JTokenType.Integer:
value = new JValue(int.Parse((string)value));
break;
case JTokenType.Float:
value = new JValue(double.Parse((string)value));
break;
case JTokenType.Boolean:
value = new JValue(string.Equals((string)value, "true", StringComparison.OrdinalIgnoreCase));
break;
case JTokenType.Null:
// In this case, we convert the expected value rather than the matching value
expected = new JValue(string.Empty);
break;
}
}
if (value.Type != expected.Type)
{
ReportCompareJsonError(value, $"Type mismatch: found='{value.Type}' expected='{expected.Type}'");
}
if (value is JProperty)
{
var v = (JProperty)value;
var e = (JProperty)expected;
if (!string.Equals(v.Name, e.Name))
{
ReportCompareJsonError(value, $"Property name mismatch: found='{Encode(v.Name)}' expected='{Encode(e.Name)}'");
}
}
if (value is JContainer)
{
var v = (JContainer)value;
var e = (JContainer)expected;
if (v.Count != e.Count)
{
ReportCompareJsonError(value, $"{value.Type} count mismatch: found={v.Count} expected={e.Count}");
}
var vi = v.First;
var ei = e.First;
while (vi != null)
{
CompareJsonTrees(vi, ei);
vi = vi.Next;
ei = ei.Next;
}
}
else // Must be JValue
{
var v = (JValue)value;
var e = (JValue)expected;
if (!v.Value.Equals(e.Value))
{
ReportCompareJsonError(value, $"Value mismatch: found='{Encode(v.Value)}' expected='{Encode(e.Value)}'");
}
}
}
static void ReportCompareJsonError(JToken token, string msg)
{
throw new UnitTestException($"Error at '{token.Path}': {msg}");
}
static string Encode(Object obj)
{
return obj.ToString().Replace("\n", "\\n").Replace("\t", "\\t");
}
static void Dump(JToken jtoken)
{
using (var writer = new JsonTextWriter(Console.Out))
{
writer.Formatting = Formatting.Indented;
jtoken.WriteTo(writer);
}
Console.Out.WriteLine();
}
}
}