在xtend项目中使用snakeyaml

在xtend项目中使用snakeyaml,yaml,xtend,snakeyaml,Yaml,Xtend,Snakeyaml,我想看看如何在xtend项目中使用snakeyaml 如何将数据转储到yaml并从中加载 package test ... @Data final public class D { public var Integer a } ... val d = new D(2); val constructor = new Constructor(D) val y = new Yaml(constructor); val o = y.dump(new D(2)) val l = new Yaml

我想看看如何在xtend项目中使用snakeyaml

如何将数据转储到yaml并从中加载

package test
...
@Data
final public class D {
  public var Integer a
}

...

val d = new D(2);
val constructor = new Constructor(D)

val y = new Yaml(constructor);
val o = y.dump(new D(2))
val l = new Yaml(constructor).load(o);
println("load: " + l)
错误消息:

Exception in thread "main" Can't construct a java object for tag:yaml.org,2002:test.D; exception=java.lang.NoSuchMethodException: test.D.<init>()
 in 'string', line 1, column 1:
    !!test.D {_a: 2}
不是提供了所需的构造函数吗? 生成的Java类如下所示:

@Data
@SuppressWarnings("all")
public final class D {
  public D(final Integer s) {
    this._a = s;
  }

  public final Integer _a;

  public Integer getA() {
    return this._a;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((_a== null) ? 0 : _a.hashCode());
    return result;
  }

  @Override
  public boolean equals(final Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    D other = (D) obj;
    if (_a == null) {
      if (other._a != null)
        return false;
    } else if (!_a.equals(other._a))
      return false;
    return true;
  }

  @Override
  public String toString() {
    String result = new ToStringHelper().toString(this);
    return result;
  }
}
作为一个构造器,这应该足够了:

  public D(final Integer s) {
    this._a = s;
  }

答案似乎是@Data Annotation在序列化上下文中毫无意义。 使用snakeyaml进行序列化需要@Property注释,如:

public class D {
    @Property String year;
    @Property Map<String, Integer> map;

}
public class D {
    @Property String year;
    @Property Map<String, Integer> map;

}
val constructor = new Constructor(D); val  yaml = new
Yaml(constructor); val car = yaml.load(...) as D;