【问题标题】:How to parse field name with dash in snakeyaml?如何在snakeyaml中用破折号解析字段名称?
【发布时间】:2016-01-28 06:27:10
【问题描述】:

我有 yaml 文件的片段:

field-name: my/data

但我无法使用方法名称 setField-name 创建 pojo

有没有办法解析这样的yaml文件?

【问题讨论】:

    标签: java field snakeyaml


    【解决方案1】:

    你可以传递一个自定义的 PropertyUtils 来处理这种情况

    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);
    

    【讨论】:

    • toCamelCase 方法从何而来?
    • 您必须根据该键所需的相应属性名称来实现它
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多