@@ -12,6 +12,9 @@ namespace k8s
12
12
/// </summary>
13
13
public static class KubernetesYaml
14
14
{
15
+ private static readonly object DeserializerLockObject = new object ( ) ;
16
+ private static readonly object SerializerLockObject = new object ( ) ;
17
+
15
18
private static DeserializerBuilder CommonDeserializerBuilder =>
16
19
new DeserializerBuilder ( )
17
20
. WithNamingConvention ( CamelCaseNamingConvention . Instance )
@@ -156,8 +159,11 @@ public static List<object> LoadAllFromString(string content, IDictionary<string,
156
159
parser . Consume < StreamStart > ( ) ;
157
160
while ( parser . Accept < DocumentStart > ( out _ ) )
158
161
{
159
- var dict = GetDeserializer ( strict ) . Deserialize < Dictionary < object , object > > ( parser ) ;
160
- types . Add ( mergedTypeMap [ dict [ "apiVersion" ] + "/" + dict [ "kind" ] ] ) ;
162
+ lock ( DeserializerLockObject )
163
+ {
164
+ var dict = GetDeserializer ( strict ) . Deserialize < Dictionary < object , object > > ( parser ) ;
165
+ types . Add ( mergedTypeMap [ dict [ "apiVersion" ] + "/" + dict [ "kind" ] ] ) ;
166
+ }
161
167
}
162
168
163
169
parser = new MergingParser ( new Parser ( new StringReader ( content ) ) ) ;
@@ -167,8 +173,11 @@ public static List<object> LoadAllFromString(string content, IDictionary<string,
167
173
while ( parser . Accept < DocumentStart > ( out _ ) )
168
174
{
169
175
var objType = types [ ix ++ ] ;
170
- var obj = GetDeserializer ( strict ) . Deserialize ( parser , objType ) ;
171
- results . Add ( obj ) ;
176
+ lock ( DeserializerLockObject )
177
+ {
178
+ var obj = GetDeserializer ( strict ) . Deserialize ( parser , objType ) ;
179
+ results . Add ( obj ) ;
180
+ }
172
181
}
173
182
174
183
return results ;
@@ -204,13 +213,19 @@ public static string SaveToString<T>(T value)
204
213
public static TValue Deserialize < TValue > ( string yaml , bool strict = false )
205
214
{
206
215
using var reader = new StringReader ( yaml ) ;
207
- return GetDeserializer ( strict ) . Deserialize < TValue > ( new MergingParser ( new Parser ( reader ) ) ) ;
216
+ lock ( DeserializerLockObject )
217
+ {
218
+ return GetDeserializer ( strict ) . Deserialize < TValue > ( new MergingParser ( new Parser ( reader ) ) ) ;
219
+ }
208
220
}
209
221
210
222
public static TValue Deserialize < TValue > ( Stream yaml , bool strict = false )
211
223
{
212
224
using var reader = new StreamReader ( yaml ) ;
213
- return GetDeserializer ( strict ) . Deserialize < TValue > ( new MergingParser ( new Parser ( reader ) ) ) ;
225
+ lock ( DeserializerLockObject )
226
+ {
227
+ return GetDeserializer ( strict ) . Deserialize < TValue > ( new MergingParser ( new Parser ( reader ) ) ) ;
228
+ }
214
229
}
215
230
216
231
public static string SerializeAll ( IEnumerable < object > values )
@@ -231,7 +246,11 @@ public static string SerializeAll(IEnumerable<object> values)
231
246
if ( value != null )
232
247
{
233
248
emitter . Emit ( new DocumentStart ( ) ) ;
234
- Serializer . SerializeValue ( emitter , value , value . GetType ( ) ) ;
249
+ lock ( SerializerLockObject )
250
+ {
251
+ Serializer . SerializeValue ( emitter , value , value . GetType ( ) ) ;
252
+ }
253
+
235
254
emitter . Emit ( new DocumentEnd ( true ) ) ;
236
255
}
237
256
}
@@ -252,7 +271,10 @@ public static string Serialize(object value)
252
271
253
272
emitter . Emit ( new StreamStart ( ) ) ;
254
273
emitter . Emit ( new DocumentStart ( ) ) ;
255
- Serializer . SerializeValue ( emitter , value , value . GetType ( ) ) ;
274
+ lock ( SerializerLockObject )
275
+ {
276
+ Serializer . SerializeValue ( emitter , value , value . GetType ( ) ) ;
277
+ }
256
278
257
279
return stringBuilder . ToString ( ) ;
258
280
}
0 commit comments