@@ -18,6 +18,42 @@ namespace k8s
18
18
/// </summary>
19
19
public static class Yaml
20
20
{
21
+ public class ByteArrayStringYamlConverter : IYamlTypeConverter
22
+ {
23
+ public bool Accepts ( Type type )
24
+ {
25
+ return type == typeof ( System . Byte [ ] ) ;
26
+ }
27
+
28
+ public object ReadYaml ( IParser parser , Type type )
29
+ {
30
+ if ( parser . Current is YamlDotNet . Core . Events . Scalar scalar )
31
+ {
32
+ try
33
+ {
34
+ if ( string . IsNullOrEmpty ( scalar . Value ) )
35
+ {
36
+ return null ;
37
+ }
38
+
39
+ return Encoding . UTF8 . GetBytes ( scalar . Value ) ;
40
+ }
41
+ finally
42
+ {
43
+ parser . MoveNext ( ) ;
44
+ }
45
+ }
46
+
47
+ throw new InvalidOperationException ( parser . Current ? . ToString ( ) ) ;
48
+ }
49
+
50
+ public void WriteYaml ( IEmitter emitter , object value , Type type )
51
+ {
52
+ var obj = ( System . Byte [ ] ) value ;
53
+ emitter . Emit ( new YamlDotNet . Core . Events . Scalar ( Encoding . UTF8 . GetString ( obj ) ) ) ;
54
+ }
55
+ }
56
+
21
57
/// <summary>
22
58
/// Load a collection of objects from a stream asynchronously
23
59
/// </summary>
@@ -65,6 +101,7 @@ public static List<object> LoadAllFromString(String content, Dictionary<String,
65
101
. WithNamingConvention ( new CamelCaseNamingConvention ( ) )
66
102
. WithTypeInspector ( ti => new AutoRestTypeInspector ( ti ) )
67
103
. WithTypeConverter ( new IntOrStringYamlConverter ( ) )
104
+ . WithTypeConverter ( new ByteArrayStringYamlConverter ( ) )
68
105
. IgnoreUnmatchedProperties ( )
69
106
. Build ( ) ;
70
107
var types = new List < Type > ( ) ;
@@ -81,6 +118,7 @@ public static List<object> LoadAllFromString(String content, Dictionary<String,
81
118
. WithNamingConvention ( new CamelCaseNamingConvention ( ) )
82
119
. WithTypeInspector ( ti => new AutoRestTypeInspector ( ti ) )
83
120
. WithTypeConverter ( new IntOrStringYamlConverter ( ) )
121
+ . WithTypeConverter ( new ByteArrayStringYamlConverter ( ) )
84
122
. Build ( ) ;
85
123
parser = new Parser ( new StringReader ( content ) ) ;
86
124
parser . Expect < StreamStart > ( ) ;
@@ -118,6 +156,7 @@ public static T LoadFromString<T>(string content)
118
156
. WithNamingConvention ( new CamelCaseNamingConvention ( ) )
119
157
. WithTypeInspector ( ti => new AutoRestTypeInspector ( ti ) )
120
158
. WithTypeConverter ( new IntOrStringYamlConverter ( ) )
159
+ . WithTypeConverter ( new ByteArrayStringYamlConverter ( ) )
121
160
. Build ( ) ;
122
161
var obj = deserializer . Deserialize < T > ( content ) ;
123
162
return obj ;
@@ -135,6 +174,7 @@ public static string SaveToString<T>(T value)
135
174
. WithNamingConvention ( new CamelCaseNamingConvention ( ) )
136
175
. WithTypeInspector ( ti => new AutoRestTypeInspector ( ti ) )
137
176
. WithTypeConverter ( new IntOrStringYamlConverter ( ) )
177
+ . WithTypeConverter ( new ByteArrayStringYamlConverter ( ) )
138
178
. WithEventEmitter ( e => new StringQuotingEmitter ( e ) )
139
179
. BuildValueSerializer ( ) ;
140
180
emitter . Emit ( new StreamStart ( ) ) ;
0 commit comments