@@ -15,16 +15,22 @@ namespace k8s
15
15
/// </summary>
16
16
public static class KubernetesYaml
17
17
{
18
- private static readonly IDeserializer Deserializer =
18
+ private static readonly DeserializerBuilder CommonDeserializerBuilder =
19
19
new DeserializerBuilder ( )
20
20
. WithNamingConvention ( CamelCaseNamingConvention . Instance )
21
21
. WithTypeConverter ( new IntOrStringYamlConverter ( ) )
22
22
. WithTypeConverter ( new ByteArrayStringYamlConverter ( ) )
23
23
. WithTypeConverter ( new ResourceQuantityYamlConverter ( ) )
24
24
. WithAttemptingUnquotedStringTypeDeserialization ( )
25
- . WithOverridesFromJsonPropertyAttributes ( )
26
- . IgnoreUnmatchedProperties ( )
27
- . Build ( ) ;
25
+ . WithOverridesFromJsonPropertyAttributes ( ) ;
26
+ private static readonly IDeserializer StrictDeserializer =
27
+ CommonDeserializerBuilder
28
+ . Build ( ) ;
29
+ private static readonly IDeserializer Deserializer =
30
+ CommonDeserializerBuilder
31
+ . IgnoreUnmatchedProperties ( )
32
+ . Build ( ) ;
33
+ private static IDeserializer GetDeserializer ( bool strict ) => strict ? StrictDeserializer : Deserializer ;
28
34
29
35
private static readonly IValueSerializer Serializer =
30
36
new SerializerBuilder ( )
@@ -100,8 +106,9 @@ public void WriteYaml(IEmitter emitter, object value, Type type)
100
106
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
101
107
/// be used.
102
108
/// </param>
109
+ /// <param name="strict">true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise</param>
103
110
/// <returns>collection of objects</returns>
104
- public static async Task < List < object > > LoadAllFromStreamAsync ( Stream stream , IDictionary < string , Type > typeMap = null )
111
+ public static async Task < List < object > > LoadAllFromStreamAsync ( Stream stream , IDictionary < string , Type > typeMap = null , bool strict = false )
105
112
{
106
113
var reader = new StreamReader ( stream ) ;
107
114
var content = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
@@ -117,8 +124,9 @@ public static async Task<List<object>> LoadAllFromStreamAsync(Stream stream, IDi
117
124
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
118
125
/// be used.
119
126
/// </param>
127
+ /// <param name="strict">true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise</param>
120
128
/// <returns>collection of objects</returns>
121
- public static async Task < List < object > > LoadAllFromFileAsync ( string fileName , IDictionary < string , Type > typeMap = null )
129
+ public static async Task < List < object > > LoadAllFromFileAsync ( string fileName , IDictionary < string , Type > typeMap = null , bool strict = false )
122
130
{
123
131
using ( var fileStream = File . OpenRead ( fileName ) )
124
132
{
@@ -136,8 +144,9 @@ public static async Task<List<object>> LoadAllFromFileAsync(string fileName, IDi
136
144
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
137
145
/// be used.
138
146
/// </param>
147
+ /// <param name="strict">true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise</param>
139
148
/// <returns>collection of objects</returns>
140
- public static List < object > LoadAllFromString ( string content , IDictionary < string , Type > typeMap = null )
149
+ public static List < object > LoadAllFromString ( string content , IDictionary < string , Type > typeMap = null , bool strict = false )
141
150
{
142
151
var mergedTypeMap = new Dictionary < string , Type > ( ModelTypeMap ) ;
143
152
// merge in KVPs from typeMap, overriding any in ModelTypeMap
@@ -148,7 +157,7 @@ public static List<object> LoadAllFromString(string content, IDictionary<string,
148
157
parser . Consume < StreamStart > ( ) ;
149
158
while ( parser . Accept < DocumentStart > ( out _ ) )
150
159
{
151
- var obj = Deserializer . Deserialize < KubernetesObject > ( parser ) ;
160
+ var obj = GetDeserializer ( strict ) . Deserialize < KubernetesObject > ( parser ) ;
152
161
types . Add ( mergedTypeMap [ obj . ApiVersion + "/" + obj . Kind ] ) ;
153
162
}
154
163
@@ -159,32 +168,32 @@ public static List<object> LoadAllFromString(string content, IDictionary<string,
159
168
while ( parser . Accept < DocumentStart > ( out _ ) )
160
169
{
161
170
var objType = types [ ix ++ ] ;
162
- var obj = Deserializer . Deserialize ( parser , objType ) ;
171
+ var obj = GetDeserializer ( strict ) . Deserialize ( parser , objType ) ;
163
172
results . Add ( obj ) ;
164
173
}
165
174
166
175
return results ;
167
176
}
168
177
169
- public static async Task < T > LoadFromStreamAsync < T > ( Stream stream )
178
+ public static async Task < T > LoadFromStreamAsync < T > ( Stream stream , bool strict = false )
170
179
{
171
180
var reader = new StreamReader ( stream ) ;
172
181
var content = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
173
- return Deserialize < T > ( content ) ;
182
+ return Deserialize < T > ( content , strict ) ;
174
183
}
175
184
176
- public static async Task < T > LoadFromFileAsync < T > ( string file )
185
+ public static async Task < T > LoadFromFileAsync < T > ( string file , bool strict = false )
177
186
{
178
187
using ( var fs = File . OpenRead ( file ) )
179
188
{
180
- return await LoadFromStreamAsync < T > ( fs ) . ConfigureAwait ( false ) ;
189
+ return await LoadFromStreamAsync < T > ( fs , strict ) . ConfigureAwait ( false ) ;
181
190
}
182
191
}
183
192
184
193
[ Obsolete ( "use Deserialize" ) ]
185
- public static T LoadFromString < T > ( string content )
194
+ public static T LoadFromString < T > ( string content , bool strict = false )
186
195
{
187
- return Deserialize < T > ( content ) ;
196
+ return Deserialize < T > ( content , strict ) ;
188
197
}
189
198
190
199
[ Obsolete ( "use Serialize" ) ]
@@ -193,14 +202,14 @@ public static string SaveToString<T>(T value)
193
202
return Serialize ( value ) ;
194
203
}
195
204
196
- public static TValue Deserialize < TValue > ( string yaml )
205
+ public static TValue Deserialize < TValue > ( string yaml , bool strict = false )
197
206
{
198
- return Deserializer . Deserialize < TValue > ( yaml ) ;
207
+ return GetDeserializer ( strict ) . Deserialize < TValue > ( yaml ) ;
199
208
}
200
209
201
- public static TValue Deserialize < TValue > ( Stream yaml )
210
+ public static TValue Deserialize < TValue > ( Stream yaml , bool strict = false )
202
211
{
203
- return Deserializer . Deserialize < TValue > ( new StreamReader ( yaml ) ) ;
212
+ return GetDeserializer ( strict ) . Deserialize < TValue > ( new StreamReader ( yaml ) ) ;
204
213
}
205
214
206
215
public static string SerializeAll ( IEnumerable < object > values )
0 commit comments