How to parse field name with dash in snakeyaml?

本秂侑毒 提交于 2020-06-08 19:02:21

问题


I have fragment of yaml file:

field-name: my/data

but I can't create pojo with method name setField-name

Is there any way to parse such yaml file?


回答1:


You can pass a custom PropertyUtils to handle such cases

Constructor c = new Constructor(MyClass.class);
c.setPropertyUtils(new PropertyUtils() {
    @Override
    public Property getProperty(Class<? extends Object> type, String name) throws IntrospectionException {
      if ( name.indexOf('-') > -1 ) {
        name = toCameCase(name);
      }
      return super.getProperty(type, name);
    }
  });
Yaml yaml = new Yaml(c);
MyClass obj = (MyClass) yaml.load(input);


来源:https://stackoverflow.com/questions/33391769/how-to-parse-field-name-with-dash-in-snakeyaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!