Search code examples
javaserializationhashmapyamlsnakeyaml

Java Serialization with YAML (SnakeYaml Library), HashMaps are not shown in serialized output


when i serialize a class object such that:

class MyClass{

   String value;
   public MyClass(){}

   public void setValue(String value){
      this.value = value;
   }

   public String getValue(){
      return value;
   }

}

and serializing it like:

 MyClass c1 = new MyClass();
    c1.setValue("this is a value");
    Map<String, Object> result = new HashMap<String, Object>();

    result.put("MyClass", c1);
    Yaml yaml = new Yaml();
    String output = yaml.dump(result);

works just fine. However, now if in my class i have another value such that:

class MyClass{

   String value;
   Map<Integer, AnotherClass> MyList
   public MyClass(){}

   public void setValue(String value){
      this.value = value;
   }

   Map<Integer, AnotherClass> CList = new HashMap<Integer, AnotherClass>();
   public void setList(AnotherClass AL){
      CList.put(1,AL);
      this.MyList = CList;
   }

   public String getValue(){
      return value;
   }

}

and now i repeat the same, the program works but in serialized output, i do not see this HashMap. What is the problem, is there some different approach used to serialize HashMap type objects?? Please suggest....


Solution

  • There is no getter for the map, that is why it is ignored.