34
34
import org .slf4j .LoggerFactory ;
35
35
import org .yaml .snakeyaml .DumperOptions ;
36
36
import org .yaml .snakeyaml .constructor .Constructor ;
37
+ import org .yaml .snakeyaml .constructor .SafeConstructor ;
37
38
import org .yaml .snakeyaml .introspector .Property ;
38
39
import org .yaml .snakeyaml .nodes .MappingNode ;
39
40
import org .yaml .snakeyaml .nodes .Node ;
@@ -78,7 +79,7 @@ public static Object load(File f) throws IOException {
78
79
* @throws IOException If an error occurs while reading the YAML.
79
80
*/
80
81
public static Object load (Reader reader ) throws IOException {
81
- Map <String , Object > data = getSnakeYaml ().load (reader );
82
+ Map <String , Object > data = getSnakeYaml (null ).load (reader );
82
83
return modelMapper (data );
83
84
}
84
85
@@ -92,7 +93,7 @@ public static Object load(Reader reader) throws IOException {
92
93
* @throws IOException If an error occurs while reading the YAML.
93
94
*/
94
95
public static <T > T loadAs (String content , Class <T > clazz ) {
95
- return getSnakeYaml ().loadAs (new StringReader (content ), clazz );
96
+ return getSnakeYaml (clazz ).loadAs (new StringReader (content ), clazz );
96
97
}
97
98
98
99
/**
@@ -104,7 +105,7 @@ public static <T> T loadAs(String content, Class<T> clazz) {
104
105
* @throws IOException If an error occurs while reading the YAML.
105
106
*/
106
107
public static <T > T loadAs (File f , Class <T > clazz ) throws IOException {
107
- return getSnakeYaml ().loadAs (new FileReader (f ), clazz );
108
+ return getSnakeYaml (clazz ).loadAs (new FileReader (f ), clazz );
108
109
}
109
110
110
111
/**
@@ -117,7 +118,7 @@ public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
117
118
* @throws IOException If an error occurs while reading the YAML.
118
119
*/
119
120
public static <T > T loadAs (Reader reader , Class <T > clazz ) {
120
- return getSnakeYaml ().loadAs (reader , clazz );
121
+ return getSnakeYaml (clazz ).loadAs (reader , clazz );
121
122
}
122
123
123
124
/**
@@ -160,7 +161,7 @@ public static List<Object> loadAll(File f) throws IOException {
160
161
* @throws IOException If an error occurs while reading the YAML.
161
162
*/
162
163
public static List <Object > loadAll (Reader reader ) throws IOException {
163
- Iterable <Object > iterable = getSnakeYaml ().loadAll (reader );
164
+ Iterable <Object > iterable = getSnakeYaml (null ).loadAll (reader );
164
165
List <Object > list = new ArrayList <Object >();
165
166
for (Object object : iterable ) {
166
167
if (object != null ) {
@@ -182,7 +183,7 @@ public static List<Object> loadAll(Reader reader) throws IOException {
182
183
* @return A YAML String representing the API object.
183
184
*/
184
185
public static String dump (Object object ) {
185
- return getSnakeYaml ().dump (object );
186
+ return getSnakeYaml (object . getClass () ).dump (object );
186
187
}
187
188
188
189
/**
@@ -192,7 +193,7 @@ public static String dump(Object object) {
192
193
* @param writer The writer to write the YAML to.
193
194
*/
194
195
public static void dump (Object object , Writer writer ) {
195
- getSnakeYaml ().dump (object , writer );
196
+ getSnakeYaml (object . getClass () ).dump (object , writer );
196
197
}
197
198
198
199
/**
@@ -202,7 +203,7 @@ public static void dump(Object object, Writer writer) {
202
203
* @return A String representing the list of YAML API objects.
203
204
*/
204
205
public static String dumpAll (Iterator <? extends KubernetesType > data ) {
205
- return getSnakeYaml ().dumpAll (data );
206
+ return getSnakeYaml (null ).dumpAll (data );
206
207
}
207
208
208
209
/**
@@ -212,11 +213,15 @@ public static String dumpAll(Iterator<? extends KubernetesType> data) {
212
213
* @param output The writer to output the YAML String to.
213
214
*/
214
215
public static void dumpAll (Iterator <? extends KubernetesType > data , Writer output ) {
215
- getSnakeYaml ().dumpAll (data , output );
216
+ getSnakeYaml (null ).dumpAll (data , output );
216
217
}
217
218
218
219
/** Defines constructor logic for custom types in this library. */
219
220
public static class CustomConstructor extends Constructor {
221
+ public CustomConstructor (Class <?> type ) {
222
+ super (type );
223
+ }
224
+
220
225
@ Override
221
226
protected Object constructObject (Node node ) {
222
227
if (node .getType () == IntOrString .class ) {
@@ -225,11 +230,9 @@ protected Object constructObject(Node node) {
225
230
if (node .getType () == byte [].class ) {
226
231
return constructByteArray ((ScalarNode ) node );
227
232
}
228
-
229
233
if (node .getType () == OffsetDateTime .class ) {
230
234
return constructDateTime ((ScalarNode ) node );
231
235
}
232
-
233
236
return super .constructObject (node );
234
237
}
235
238
@@ -357,8 +360,16 @@ protected NodeTuple representJavaBeanProperty(
357
360
}
358
361
359
362
/** @return An instantiated SnakeYaml Object. */
363
+ @ Deprecated
360
364
public static org .yaml .snakeyaml .Yaml getSnakeYaml () {
361
- return new org .yaml .snakeyaml .Yaml (new CustomConstructor (), new CustomRepresenter ());
365
+ return getSnakeYaml (null );
366
+ }
367
+
368
+ private static org .yaml .snakeyaml .Yaml getSnakeYaml (Class <?> type ) {
369
+ if (type != null ) {
370
+ return new org .yaml .snakeyaml .Yaml (new CustomConstructor (type ), new CustomRepresenter ());
371
+ }
372
+ return new org .yaml .snakeyaml .Yaml (new SafeConstructor (), new CustomRepresenter ());
362
373
}
363
374
364
375
/**
@@ -381,7 +392,7 @@ private static Object modelMapper(Map<String, Object> data) throws IOException {
381
392
throw new IOException (
382
393
"Unknown apiVersionKind " + apiVersion + "/" + kind + " is it registered?" );
383
394
}
384
- return loadAs (new StringReader (getSnakeYaml ().dump (data )), clazz );
395
+ return loadAs (new StringReader (getSnakeYaml (clazz ).dump (data )), clazz );
385
396
}
386
397
387
398
@ Deprecated
0 commit comments