Использование змейки в проекте xtend

Я хотел бы посмотреть, как использовать змею в проекте xtend.

Как я могу сбросить в 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;
  }

person user3596456    schedule 02.05.2014    source источник


Ответы (1)


Ответ, похоже, заключается в том, что аннотация @Data не имеет смысла в контексте сериализации. Сериализация с использованием snapyaml требует аннотаций @Property, таких как:

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;
person user3596456    schedule 03.05.2014