how-to dump and load collection class property
Closed this issue · 9 comments
Hello!
I'm sorry if there isn't place for questions.
I want to use Yaml for serialization of object tree.
I have next class structure
class Entity {
protected String name;
protected Entity parent;
protected Entity product;
}
class CompositeEntity {
protected Object param1;
protected List<Entity> entities = new ArrayList<Entity>()
}
By default behavior I only have top level attributes - name, parent, product
and param1.
How to can I dump CompositeEntity with dumped entities collection?
Original issue reported on code.google.com by nvoy...@gmail.com
on 4 Mar 2015 at 10:48
see attached test case
Original comment by nvoy...@gmail.com
on 4 Mar 2015 at 11:14
As far as I can see the test outputs:
!!org.yaml.snakeyaml.issues.issue208.CompositeEntity
name: CompositeEntity
param1: null
parent: null
What is wrong with it ?
Original comment by py4fun@gmail.com
on 4 Mar 2015 at 1:44
Few month ago I work with Yaml in Ruby and had the same structure in basis. I
expect to see collection of Entities inside Yaml dump and of course its
correctly loading from Yaml-file.
I rewrite test method to show what I wait to see ..
public void testEntityYaml() throws IOException {
CompositeEntity entity = new CompositeEntity();
entity.setName("CompositeEntity");
CompositeEntity e = (CompositeEntity) entity.add(new CompositeEntity());
e.setName("1.1");
e = (CompositeEntity) entity.add(new CompositeEntity());
e.setName("1.2");
CompositeEntity e2 = (CompositeEntity) e.add(new CompositeEntity());
e2.setName("1.2.1");
e2 = (CompositeEntity) e.add(new CompositeEntity());
e2.setName("1.2.2");
YamlDatabase db = new YamlDatabase();
db.save(entity);
CompositeEntity entityFromYaml = (CompositeEntity) db.load(entity.getClass());
assertTrue(entityFromYaml.getName().equals(entity.getName()));
assertTrue(entityFromYaml.getEntitiesSize() == entity.getEntitiesSize());
assertTrue(entityFromYaml.getEntityAt(0).getName().equals(entity.getEntityAt(0).getName()));
CompositeEntity e2fromYaml = (CompositeEntity) entityFromYaml.getEntityAt(1);
e2fromYaml = (CompositeEntity) e2fromYaml.getEntityAt(1);
assertTrue(e2.getName().equals(entity.getEntityAt(0).getName()));
}
and now I wait to see something of this
!!org.yaml.snakeyaml.issues.issue208.CompositeEntity
name: CompositeEntity
param1: null
parent: null
entities:
- !!org.yaml.snakeyaml.issues.issue208.CompositeEntity
name: 1.1
param1: null
parent: null
entities: []
- !!org.yaml.snakeyaml.issues.issue208.CompositeEntity
name: 1.2.1
param1: null
parent: null
entities:[]
- !!org.yaml.snakeyaml.issues.issue208.CompositeEntity
name: 1.2.1
param1: null
parent: null
entities:[]
bellow you can see my old yaml-file example from Ruby
--- !ruby/hash:ISPS::Data::ProductRegistry::Products
p1: &3147000 !ruby/object:ISPS::Core::Product
custom_actions: {}
components:
- !ruby/object:ISPS::Components::Offer
name: Offer
start_date:
end_date:
activation_date:
deactivation_date:
condition:
parent: *3147000 - !ruby/object:ISPS::Components::Offer
name: Offer
start_date:
end_date:
activation_date:
deactivation_date:
condition:
parent: *3147000
instances: []
product: *3147000
name: p1
start_date:
end_date:
activation_date:
deactivation_date:
condition:
p2: &3145284 !ruby/object:ISPS::Core::Product
custom_actions: {}
components: []
instances: []
product: *3145284
name: p2
start_date:
end_date:
activation_date:
deactivation_date:
condition:
Original comment by nvoy...@gmail.com
on 4 Mar 2015 at 2:07
new JUnit test case
passed: assertTrue(entityFromYaml.getName().equals(entity.getName()));
fail: assertTrue(entityFromYaml.getEntitiesSize() == entity.getEntitiesSize());
must fail, because collection haven't saved:
assertTrue(entityFromYaml.getEntityAt(0).getName().equals(entity.getEntityAt(0).
getName()));
CompositeEntity e2fromYaml = (CompositeEntity) entityFromYaml.getEntityAt(1);
e2fromYaml = (CompositeEntity) e2fromYaml.getEntityAt(1);
assertTrue(e2.getName().equals(entity.getEntityAt(0).getName()));
Original comment by nvoy...@gmail.com
on 4 Mar 2015 at 2:13
Attachments:
Let's take a simple example
class Entity {
protected String name;
protected List entities = new ArrayList();
public Entity(String name) {
this.name = name;
entities.add(new Entity("e1"));
entities.add(new Entity("e2"));
}
}
...
System.out.println(yaml.dump(new Entity("Entity")));
what must be outputed?
> !!Entity
> name: Entity
or
> !!Entity
> name: Entity
> entities:
> - !!Entity
> name: e1
> - !!Entity
> name: e2
how to I can get the second alternative output?
Original comment by nvoy...@gmail.com
on 4 Mar 2015 at 2:31
I see the point now.
CompositeEntity is not a JavaBean. It does not define setters and getters for
the exposed fields.
You can either to define your own way to (de)serialise the object or make it a
JavaBean.
Original comment by py4fun@gmail.com
on 4 Mar 2015 at 2:45
Now it works :) Thanks a lot and sorry for stupid question.
Original comment by nvoy...@gmail.com
on 4 Mar 2015 at 3:01
You are welcome. Next time it is better to ask general questions in the mailing
list.
Original comment by py4fun@gmail.com
on 4 Mar 2015 at 3:18
- Changed state: Invalid