3535import org .slf4j .LoggerFactory ;
3636import org .yaml .snakeyaml .DumperOptions ;
3737import org .yaml .snakeyaml .constructor .Constructor ;
38+ import org .yaml .snakeyaml .constructor .SafeConstructor ;
3839import org .yaml .snakeyaml .introspector .Property ;
3940import org .yaml .snakeyaml .nodes .MappingNode ;
4041import org .yaml .snakeyaml .nodes .Node ;
@@ -79,7 +80,7 @@ public static Object load(File f) throws IOException {
7980 * @throws IOException If an error occurs while reading the YAML.
8081 */
8182 public static Object load (Reader reader ) throws IOException {
82- Map <String , Object > data = getSnakeYaml ().load (reader );
83+ Map <String , Object > data = getSnakeYaml (null ).load (reader );
8384 return modelMapper (data );
8485 }
8586
@@ -93,7 +94,7 @@ public static Object load(Reader reader) throws IOException {
9394 * @throws IOException If an error occurs while reading the YAML.
9495 */
9596 public static <T > T loadAs (String content , Class <T > clazz ) {
96- return getSnakeYaml ().loadAs (new StringReader (content ), clazz );
97+ return getSnakeYaml (clazz ).loadAs (new StringReader (content ), clazz );
9798 }
9899
99100 /**
@@ -105,7 +106,7 @@ public static <T> T loadAs(String content, Class<T> clazz) {
105106 * @throws IOException If an error occurs while reading the YAML.
106107 */
107108 public static <T > T loadAs (File f , Class <T > clazz ) throws IOException {
108- return getSnakeYaml ().loadAs (new FileReader (f ), clazz );
109+ return getSnakeYaml (clazz ).loadAs (new FileReader (f ), clazz );
109110 }
110111
111112 /**
@@ -118,7 +119,7 @@ public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
118119 * @throws IOException If an error occurs while reading the YAML.
119120 */
120121 public static <T > T loadAs (Reader reader , Class <T > clazz ) {
121- return getSnakeYaml ().loadAs (reader , clazz );
122+ return getSnakeYaml (clazz ).loadAs (reader , clazz );
122123 }
123124
124125 /**
@@ -161,7 +162,7 @@ public static List<Object> loadAll(File f) throws IOException {
161162 * @throws IOException If an error occurs while reading the YAML.
162163 */
163164 public static List <Object > loadAll (Reader reader ) throws IOException {
164- Iterable <Object > iterable = getSnakeYaml ().loadAll (reader );
165+ Iterable <Object > iterable = getSnakeYaml (null ).loadAll (reader );
165166 List <Object > list = new ArrayList <Object >();
166167 for (Object object : iterable ) {
167168 if (object != null ) {
@@ -183,7 +184,7 @@ public static List<Object> loadAll(Reader reader) throws IOException {
183184 * @return A YAML String representing the API object.
184185 */
185186 public static String dump (Object object ) {
186- return getSnakeYaml ().dump (object );
187+ return getSnakeYaml (object . getClass () ).dump (object );
187188 }
188189
189190 /**
@@ -193,7 +194,7 @@ public static String dump(Object object) {
193194 * @param writer The writer to write the YAML to.
194195 */
195196 public static void dump (Object object , Writer writer ) {
196- getSnakeYaml ().dump (object , writer );
197+ getSnakeYaml (object . getClass () ).dump (object , writer );
197198 }
198199
199200 /**
@@ -203,7 +204,7 @@ public static void dump(Object object, Writer writer) {
203204 * @return A String representing the list of YAML API objects.
204205 */
205206 public static String dumpAll (Iterator <? extends KubernetesType > data ) {
206- return getSnakeYaml ().dumpAll (data );
207+ return getSnakeYaml (null ).dumpAll (data );
207208 }
208209
209210 /**
@@ -213,11 +214,15 @@ public static String dumpAll(Iterator<? extends KubernetesType> data) {
213214 * @param output The writer to output the YAML String to.
214215 */
215216 public static void dumpAll (Iterator <? extends KubernetesType > data , Writer output ) {
216- getSnakeYaml ().dumpAll (data , output );
217+ getSnakeYaml (null ).dumpAll (data , output );
217218 }
218219
219220 /** Defines constructor logic for custom types in this library. */
220221 public static class CustomConstructor extends Constructor {
222+ public CustomConstructor (Class <?> type ) {
223+ super (type );
224+ }
225+
221226 @ Override
222227 protected Object constructObject (Node node ) {
223228 if (node .getType () == IntOrString .class ) {
@@ -230,7 +235,6 @@ protected Object constructObject(Node node) {
230235 if (node .getType () == org .joda .time .DateTime .class ) {
231236 return constructDateTime ((ScalarNode ) node );
232237 }
233-
234238 return super .constructObject (node );
235239 }
236240
@@ -358,8 +362,16 @@ protected NodeTuple representJavaBeanProperty(
358362 }
359363
360364 /** @return An instantiated SnakeYaml Object. */
365+ @ Deprecated
361366 public static org .yaml .snakeyaml .Yaml getSnakeYaml () {
362- return new org .yaml .snakeyaml .Yaml (new CustomConstructor (), new CustomRepresenter ());
367+ return getSnakeYaml (null );
368+ }
369+
370+ private static org .yaml .snakeyaml .Yaml getSnakeYaml (Class <?> type ) {
371+ if (type != null ) {
372+ return new org .yaml .snakeyaml .Yaml (new CustomConstructor (type ), new CustomRepresenter ());
373+ }
374+ return new org .yaml .snakeyaml .Yaml (new SafeConstructor (), new CustomRepresenter ());
363375 }
364376
365377 /**
@@ -382,7 +394,7 @@ private static Object modelMapper(Map<String, Object> data) throws IOException {
382394 throw new IOException (
383395 "Unknown apiVersionKind " + apiVersion + "/" + kind + " is it registered?" );
384396 }
385- return loadAs (new StringReader (getSnakeYaml ().dump (data )), clazz );
397+ return loadAs (new StringReader (getSnakeYaml (clazz ).dump (data )), clazz );
386398 }
387399
388400 @ Deprecated
0 commit comments