2

I'd like to see how to use snakeyaml in a xtend project.

How can I dump to yaml and load from ?

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)

Error message:

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}

I'm also trying:

@Data
final public class D {
    public new(Integer s) {
        _a = s
    }

    public var Integer a

}

Isn't the required constructor provided? The resulting Java class looks like this:

@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;
  }
}

This should be enough of a constructor:

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

1 Answers1

1

The answer seems to be that @Data Annotation makes no sense in the context of serialization. Serialization using snakeyaml requires @Property Annotations like:

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

}

and a lot of work in terms of type casting which has to be done be hand.

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