title: Writing a java object to a file using SnakeYaml - List of inner objects are not writen out when using Yaml.dump()
body: <p>I am having trouble writing out a java object to a yml file using SnakeYaml. I have setup a spring boot project with a simple application.yml that I convert to a set of java objects. In the real application some objects get added and when the application is done I want to write out the new application.yml.</p>
  
  <p>The input application.yml looks like this:</p>
  
  <pre><code>sample:
    name: toyota
    cars:
       - model: prius
         colour: silver
       - model: avensis
         colour: red
  </code></pre>
  
  <p>The java object that maps this application.yml:</p>
  
  <pre><code>@Configuration
  @ConfigurationProperties(prefix="sample")
  public class Config {
  
      private String name;
      private final List&lt;Car&gt; cars = new ArrayList&lt;&gt;();
  
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public List&lt;Car&gt; getCars() {
          return cars;
      }
  }
  </code></pre>
  
  <p>And the sample class that writes out the yaml:</p>
  
  <pre><code>@Component
  public class Example {
  
      @Autowired
      private Config config;
  
      @PostConstruct
      private void init(){
          Yaml yaml = new Yaml();
          StringWriter writer = new StringWriter();
          yaml.dump(config, writer);      
          System.out.println(writer.toString());          
      }
  }
  </code></pre>
  
  <p>The output is just: <code>name: toyota</code> (ommitted some yaml settings that also get writen out). However I want the output to be exactly the same as the input:</p>
  
  <pre><code>sample:
    name: toyota
    cars:
       - model: prius
         colour: silver
       - model: avensis
         colour: red
  </code></pre>
  
  <p>Is there a way to do this?</p>
  
  <h2>EDIT:</h2>
  
  <p>User flyx correctly pointed out the following <a href="https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-type-safe-collections" rel="nofollow noreferrer">documentation</a>. </p>
  
  <p>My adjusted code:</p>
  
  <pre><code>Constructor constructor = new Constructor(Config.class);
  TypeDescription configDescription = new TypeDescription(Config.class);
  configDescription.putListPropertyType("cars", Car.class);
  constructor.addTypeDescription(configDescription);
  Yaml yaml = new Yaml(constructor);
  StringWriter writer = new StringWriter();
  yaml.dump(config, writer);      
  System.out.println(writer.toString()); 
  </code></pre>
  
  <p>Gives the following output:</p>
  
  <pre><code>$$beanFactory: !!org.springframework.beans.factory.support.DefaultListableBeanFactory
    allowBeanDefinitionOverriding: true
    allowEagerClassLoading: true
    autowireCandidateResolver: !!org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver {}
    beanClassLoader: !!sun.misc.Launcher$AppClassLoader {}
    beanExpressionResolver: !!org.springframework.context.expression.StandardBeanExpressionResolver {}
    cacheBeanMetadata: true
    conversionService: null
    dependencyComparator: !!org.springframework.core.annotation.AnnotationAwareOrderComparator {}
    parentBeanFactory: null
    serializationId: application
    tempClassLoader: null
    typeConverter: !!org.springframework.beans.SimpleTypeConverter {conversionService: null}
  name: toyota
  </code></pre>