--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snakeyaml-cor...@googlegroups.com.
To post to this group, send email to snakeya...@googlegroups.com.
Visit this group at https://groups.google.com/group/snakeyaml-core.
For more options, visit https://groups.google.com/d/optout.
@Test
void testYamlSerializationAndDeserialization()
{
Yaml yaml = new Yaml( new OptionalRepresenter() );
yaml.setBeanAccess( BeanAccess.FIELD );
Person person = new Person( 101, Optional.of( "green" ) );
String dump = yaml.dump(person);
System.out.println( dump );
Person loadedPerson = yaml.load( dump);
assertEquals( person, loadedPerson );
}
Hi Sven,you can check the test to see how to work with Optional: https://bitbucket.org/asomov/snakeyaml/src/default/src/test/java8/org/yaml/snakeyaml/issues/issue310/See BeanAccess.FIELD for the variables.Andrey
On Fri, Apr 12, 2019 at 2:37 PM Sven Rathgeber <sven.r...@web.de> wrote:
--Hello,I'm looking for a way to serialize (to yaml) such a class:public class Person
{
public final int age;
public final Optional< String > hairColor;
public Person(
int age,
Optional< String > hairColor )
{
this.age = age;
this.hairColor = hairColor;
}}Is there a way to do that with snakeyaml.Regards.Sven
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snakeya...@googlegroups.com.
To post to this group, send email to snakeya...@googlegroups.com.
Visit this group at https://groups.google.com/group/snakeyaml-core.
For more options, visit https://groups.google.com/d/optout.
--Andrey Somov
public class MyRepresenter extends Representer {
public MyRepresenter() {
this.representers.put(Optional.class, new RepresentOptional());
this.representers.put(Person.class, new RepresentPerson());
}
private class RepresentOptional implements Represent {
@Override
public Node representData(Object data) {
Optional<?> opt = (Optional<?>) data;
List<Object> seq = new ArrayList<>(1);
seq.add(opt.get());
return representSequence(Tag.SEQ, seq, DumperOptions.FlowStyle.FLOW);
}
}
private class RepresentPerson implements Represent {
@Override
public Node representData(Object data) {
Person person = (Person) data;
List<Object> seq = new ArrayList<>(2);
seq.add(person.age);
seq.add(person.hairColor);
return representSequence(Tag.SEQ, seq, DumperOptions.FlowStyle.FLOW);
}
}
}