Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse field name with dash in snakeyaml?

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?

like image 536
pixel Avatar asked Oct 28 '15 13:10

pixel


1 Answers

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);
like image 163
shyam Avatar answered Nov 03 '22 14:11

shyam