我想看看如何在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}
我也在尝试:
@Data
final public class D {
public new(Integer s) {
_a = s
}
public var Integer a
}
是否提供了所需的构造函数? 生成的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;
}
答案 0 :(得分:1)
答案似乎是@Data Annotation在序列化的上下文中没有任何意义。 使用snakeyaml进行序列化需要@Property Annotations,如:
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;