0

This is the yaml file. I am trying this to load into pojo class. but it shows me cannot create property for listMap. Please help me in digging out this problem. In this yaml file i have a listMap which contains multiple key value pairs. Also i need advice whether this approach is better or i should load it into Map.

android: dfeiei
driver: dfkejifein
list:
  - aabra
  - ka
  - dabra
listMap:
  key: value
  name: paras
  sirname: porwal

code to load this yaml file into pojo.

YamlLoader yamlFile=yaml.loadAs(in, YamlLoader.class);

Here is the POJO class .

 import java.util.List;
    import java.util.Map;

    public class YamlLoader {
    private String android;
    private String driver;
    private List<String>list;
    private Map<String,String>map;
    private List listMap;
    public String getAndroid() {
        return android;
    }
    public void setAndroid(String android) {
        this.android = android;
    }
    public String getDriver() {
        return driver;
    }
    public void setDriver(String driver) {
        this.driver = driver;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "YamlLoader [android=" + android + ", driver=" + driver + ", list=" + list + ", map=" + map + ", listMap="
                + listMap + "]";
    }
    public Map<String,String> getMap() {
        return map;
    }
    public void setMap(Map<String,String> map) {
        this.map = map;
    }
    public List getListMap() {
        return listMap;
    }
    public void setListMap(List listMap) {
        this.listMap = listMap;
    }
paras
  • 17
  • 3
  • 6
  • Simply because the yaml doesn't hold anything for `listmap`. remove `private List listMap;` and anything depending on it or search the library if there is an annotation to use for yaml parsing and ignoring empty variables. – Nico Aug 16 '17 at 07:46

1 Answers1

0

listMap is a List, which is why YAML cannot fill it from a mapping. If you want a list, change your YAML like this:

listMap:
  - key: value
  - name: paras
  - sirname: porwal

This will get you a list of single-entry mappings. If you want to keep the mapping on the YAML side, load it into a Map.

If you want to have more control over how YAML entities map to Java types, use the SnakeYaml API instead of Jackson. I never get why people use Jackson for YAML processing when it offers fewer customizations than SnakeYaml itself and at the same time is not at any higher level.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • Thanks and agreed. But i am using snake yaml api and also tried what you have mentioned but it is not working. – paras Aug 16 '17 at 17:38