3434import org .slf4j .LoggerFactory ;
3535import org .yaml .snakeyaml .DumperOptions ;
3636import org .yaml .snakeyaml .constructor .Constructor ;
37+ import org .yaml .snakeyaml .constructor .SafeConstructor ;
3738import org .yaml .snakeyaml .introspector .Property ;
3839import org .yaml .snakeyaml .nodes .MappingNode ;
3940import org .yaml .snakeyaml .nodes .Node ;
@@ -78,7 +79,7 @@ public static Object load(File f) throws IOException {
7879 * @throws IOException If an error occurs while reading the YAML.
7980 */
8081 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 );
8283 return modelMapper (data );
8384 }
8485
@@ -92,7 +93,7 @@ public static Object load(Reader reader) throws IOException {
9293 * @throws IOException If an error occurs while reading the YAML.
9394 */
9495 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 );
9697 }
9798
9899 /**
@@ -104,7 +105,7 @@ public static <T> T loadAs(String content, Class<T> clazz) {
104105 * @throws IOException If an error occurs while reading the YAML.
105106 */
106107 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 );
108109 }
109110
110111 /**
@@ -117,7 +118,7 @@ public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
117118 * @throws IOException If an error occurs while reading the YAML.
118119 */
119120 public static <T > T loadAs (Reader reader , Class <T > clazz ) {
120- return getSnakeYaml ().loadAs (reader , clazz );
121+ return getSnakeYaml (clazz ).loadAs (reader , clazz );
121122 }
122123
123124 /**
@@ -160,7 +161,7 @@ public static List<Object> loadAll(File f) throws IOException {
160161 * @throws IOException If an error occurs while reading the YAML.
161162 */
162163 public static List <Object > loadAll (Reader reader ) throws IOException {
163- Iterable <Object > iterable = getSnakeYaml ().loadAll (reader );
164+ Iterable <Object > iterable = getSnakeYaml (null ).loadAll (reader );
164165 List <Object > list = new ArrayList <Object >();
165166 for (Object object : iterable ) {
166167 if (object != null ) {
@@ -182,7 +183,7 @@ public static List<Object> loadAll(Reader reader) throws IOException {
182183 * @return A YAML String representing the API object.
183184 */
184185 public static String dump (Object object ) {
185- return getSnakeYaml ().dump (object );
186+ return getSnakeYaml (object . getClass () ).dump (object );
186187 }
187188
188189 /**
@@ -192,7 +193,7 @@ public static String dump(Object object) {
192193 * @param writer The writer to write the YAML to.
193194 */
194195 public static void dump (Object object , Writer writer ) {
195- getSnakeYaml ().dump (object , writer );
196+ getSnakeYaml (object . getClass () ).dump (object , writer );
196197 }
197198
198199 /**
@@ -202,7 +203,7 @@ public static void dump(Object object, Writer writer) {
202203 * @return A String representing the list of YAML API objects.
203204 */
204205 public static String dumpAll (Iterator <? extends KubernetesType > data ) {
205- return getSnakeYaml ().dumpAll (data );
206+ return getSnakeYaml (null ).dumpAll (data );
206207 }
207208
208209 /**
@@ -212,11 +213,15 @@ public static String dumpAll(Iterator<? extends KubernetesType> data) {
212213 * @param output The writer to output the YAML String to.
213214 */
214215 public static void dumpAll (Iterator <? extends KubernetesType > data , Writer output ) {
215- getSnakeYaml ().dumpAll (data , output );
216+ getSnakeYaml (null ).dumpAll (data , output );
216217 }
217218
218219 /** Defines constructor logic for custom types in this library. */
219220 public static class CustomConstructor extends Constructor {
221+ public CustomConstructor (Class <?> type ) {
222+ super (type );
223+ }
224+
220225 @ Override
221226 protected Object constructObject (Node node ) {
222227 if (node .getType () == IntOrString .class ) {
@@ -225,11 +230,9 @@ protected Object constructObject(Node node) {
225230 if (node .getType () == byte [].class ) {
226231 return constructByteArray ((ScalarNode ) node );
227232 }
228-
229233 if (node .getType () == OffsetDateTime .class ) {
230234 return constructDateTime ((ScalarNode ) node );
231235 }
232-
233236 return super .constructObject (node );
234237 }
235238
@@ -357,8 +360,16 @@ protected NodeTuple representJavaBeanProperty(
357360 }
358361
359362 /** @return An instantiated SnakeYaml Object. */
363+ @ Deprecated
360364 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 ());
362373 }
363374
364375 /**
@@ -381,7 +392,7 @@ private static Object modelMapper(Map<String, Object> data) throws IOException {
381392 throw new IOException (
382393 "Unknown apiVersionKind " + apiVersion + "/" + kind + " is it registered?" );
383394 }
384- return loadAs (new StringReader (getSnakeYaml ().dump (data )), clazz );
395+ return loadAs (new StringReader (getSnakeYaml (clazz ).dump (data )), clazz );
385396 }
386397
387398 @ Deprecated
0 commit comments