-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathYamlConverter.cs
139 lines (127 loc) · 4.99 KB
/
YamlConverter.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json.Nodes;
using SharpYaml;
using SharpYaml.Serialization;
namespace Microsoft.OpenApi.YamlReader
{
/// <summary>
/// Provides extensions to convert YAML models to JSON models.
/// </summary>
public static class YamlConverter
{
/// <summary>
/// Converts all of the documents in a YAML stream to <see cref="JsonNode"/>s.
/// </summary>
/// <param name="yaml">The YAML stream.</param>
/// <returns>A collection of nodes representing the YAML documents in the stream.</returns>
public static IEnumerable<JsonNode> ToJsonNode(this YamlStream yaml)
{
return yaml.Documents.Select(x => x.ToJsonNode());
}
/// <summary>
/// Converts a single YAML document to a <see cref="JsonNode"/>.
/// </summary>
/// <param name="yaml">The YAML document.</param>
/// <returns>A `JsonNode` representative of the YAML document.</returns>
public static JsonNode ToJsonNode(this YamlDocument yaml)
{
return yaml.RootNode.ToJsonNode();
}
/// <summary>
/// Converts a single YAML node to a <see cref="JsonNode"/>.
/// </summary>
/// <param name="yaml">The YAML node.</param>
/// <returns>A `JsonNode` representative of the YAML node.</returns>
/// <exception cref="NotSupportedException">Thrown for YAML that is not compatible with JSON.</exception>
public static JsonNode ToJsonNode(this YamlNode yaml)
{
return yaml switch
{
YamlMappingNode map => map.ToJsonObject(),
YamlSequenceNode seq => seq.ToJsonArray(),
YamlScalarNode scalar => scalar.ToJsonValue(),
_ => throw new NotSupportedException("This yaml isn't convertible to JSON")
};
}
/// <summary>
/// Converts a single JSON node to a <see cref="YamlNode"/>.
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static YamlNode ToYamlNode(this JsonNode json)
{
return json switch
{
JsonObject obj => obj.ToYamlMapping(),
JsonArray arr => arr.ToYamlSequence(),
JsonValue val => val.ToYamlScalar(),
_ => throw new NotSupportedException("This isn't a supported JsonNode")
};
}
/// <summary>
/// Converts a <see cref="YamlMappingNode"/> to a <see cref="JsonObject"/>.
/// </summary>
/// <param name="yaml"></param>
/// <returns></returns>
public static JsonObject ToJsonObject(this YamlMappingNode yaml)
{
var node = new JsonObject();
foreach (var keyValuePair in yaml)
{
var key = ((YamlScalarNode)keyValuePair.Key).Value!;
node[key] = keyValuePair.Value.ToJsonNode();
}
return node;
}
private static YamlMappingNode ToYamlMapping(this JsonObject obj)
{
return new YamlMappingNode(obj.ToDictionary(x => (YamlNode)new YamlScalarNode(x.Key), x => x.Value!.ToYamlNode()));
}
/// <summary>
/// Converts a <see cref="YamlSequenceNode"/> to a <see cref="JsonArray"/>.
/// </summary>
/// <param name="yaml"></param>
/// <returns></returns>
public static JsonArray ToJsonArray(this YamlSequenceNode yaml)
{
var node = new JsonArray();
foreach (var value in yaml)
{
node.Add(value.ToJsonNode());
}
return node;
}
private static YamlSequenceNode ToYamlSequence(this JsonArray arr)
{
return new YamlSequenceNode(arr.Select(x => x!.ToYamlNode()));
}
private static JsonValue ToJsonValue(this YamlScalarNode yaml)
{
switch (yaml.Style)
{
case ScalarStyle.Plain:
return decimal.TryParse(yaml.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)
? JsonValue.Create(d)
: bool.TryParse(yaml.Value, out var b)
? JsonValue.Create(b)
: JsonValue.Create(yaml.Value)!;
case ScalarStyle.SingleQuoted:
case ScalarStyle.DoubleQuoted:
case ScalarStyle.Literal:
case ScalarStyle.Folded:
case ScalarStyle.Any:
return JsonValue.Create(yaml.Value)!;
default:
throw new ArgumentOutOfRangeException();
}
}
private static YamlScalarNode ToYamlScalar(this JsonValue val)
{
return new YamlScalarNode(val.ToJsonString());
}
}
}