Mause / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

how-to dump and load collection class property #208

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
see attached test case

Original comment by nvoy...@gmail.com on 4 Mar 2015 at 11:14

GoogleCodeExporter commented 9 years ago

Original comment by nvoy...@gmail.com on 4 Mar 2015 at 11:15

Attachments:

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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:

Original comment by nvoy...@gmail.com on 4 Mar 2015 at 2:07

GoogleCodeExporter commented 9 years ago
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:

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
Now it works :) Thanks a lot and sorry for stupid question. 

Original comment by nvoy...@gmail.com on 4 Mar 2015 at 3:01

GoogleCodeExporter commented 9 years ago
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