forked from kubernetes-client/csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntOrStringYamlConverter.cs
42 lines (37 loc) · 1.1 KB
/
IntOrStringYamlConverter.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
using System;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
namespace k8s.Models
{
public class IntOrStringYamlConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(IntstrIntOrString);
}
public object ReadYaml(IParser parser, Type type)
{
if (parser?.Current is YamlDotNet.Core.Events.Scalar scalar)
{
try
{
if (string.IsNullOrEmpty(scalar?.Value))
{
return null;
}
return new IntstrIntOrString(scalar?.Value);
}
finally
{
parser?.MoveNext();
}
}
throw new InvalidOperationException(parser?.Current?.ToString());
}
public void WriteYaml(IEmitter emitter, object value, Type type)
{
var obj = (IntstrIntOrString)value;
emitter?.Emit(new YamlDotNet.Core.Events.Scalar(obj?.Value));
}
}
}