Avoid TAGs type dumping a class

1,244 views
Skip to first unread message

Jose Carrasco

unread,
Oct 22, 2014, 4:45:10 PM10/22/14
to snakeya...@googlegroups.com
When I dump a instance of the following class:


    class BrooklynApplicationEntity{
       
private String id;
       
private String location;
       
private String name;
       
List<BrooklynServiceEntity> services;
       
       
//getters and setters
       
...
   
}


Using the next code:

     
     DumperOptions options = new DumperOptions();
     options
.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
     options
.setCanonical(false);
     options
.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
     
SkipEmptyAndNullRepresenter skipEmptyAndNullRepresenter=
         
new SkipEmptyAndNullRepresenter();
     
     
Yaml yaml=new Yaml(skipEmptyAndNullRepresenter, options);
     yaml
.dump(this.getBrooklynApplicationEntity(), file);



I obtain the next yaml.

    !!org.tomat.translate.brooklyn.entity.BrooklynApplicationEntity
    id: dbApp
    location: localhost
    name: DatabaseApp
    services:
    - !!org.tomat.translate.brooklyn.entity.JBossBrooklynService
      brooklynConfigProperties:
        port.http: 80+
      id: JBossMainWebServer
      location: AWS
      name: JBoss Main Web Server
    - !!org.tomat.translate.brooklyn.entity.JBossBrooklynService
      id: JBossSecondWebServer
      location: localhost
      name: JBoss

I want to avoid the TAGs in the output YAML, so I have added the next instructions,


        skipEmptyAndNullRepresenter.addClassTag(JBossAgnosticElement.class, Tag.MAP);
        skipEmptyAndNullRepresenter
.addClassTag(JBossAgnosticElement.class, Tag.SEQ);



However, the TAGs !!org.tomat.translate.brooklyn.entity.BrooklynApplicationEntity and !!org.tomat.translate.brooklyn.entity.JBossBrooklynService are not deleted.

Any idea??

Andrey

unread,
Oct 23, 2014, 2:51:40 AM10/23/14
to snakeya...@googlegroups.com
What is JBossAgnosticElement ?

Try this:

        skipEmptyAndNullRepresenter.addClassTag(org.tomat.translate.brooklyn.entity.BrooklynApplicationEntity.class, Tag.MAP);
        skipEmptyAndNullRepresenter
.addClassTag(org.tomat.translate.brooklyn.entity.JBossBrooklynService.class, Tag.MAP);

Cheers,
Andrey


Jose Carrasco

unread,
Oct 23, 2014, 3:43:31 AM10/23/14
to snakeya...@googlegroups.com
Hi Andrey,

Thanks for your reply.
I have just check this solution, and it is correct.

Why this works? I think because we add the JBossBrooklynServiceTag like a TAG.MAP which is a implicit tag. So the representer (by default) builds the scalar (Represent.representData(Object o)), but it is not added the TAG, or in any case JBossBrooklynServiceTag is substituted by TAG.MAP, and it is not dumped. am I wrong? ( I am not sure of this is the process to dump the YAML).

In any case I have a new problem. When the scalar of a node object is build, compare the name of the class of the current object with the class which have a TagClass (in my example org.tomat.translate.brooklyn.entity.JBossBrooklynService). If the current class has the same name that org.tomat.translate.brooklyn.entity.JBossBrooklynService, the TAG is changed using a implicit tag.
If I am not wrong, I will not be able to add a hierarchy class to avoid the implicit TAGs, e.g:

JBossBrooklynService and TomcatBrooklynService extend the same super class, JavaWebApplicationServer.

In this case, I would like add any something

skipEmptyAndNullRepresenter.addClassTag(Class<? extends org.tommat.translate.brooklyn.entity. JavaWebApplicationServer>, Tag.Map);

However, I do not find wat to accomplish this task.

Best,
Jose.
Reply all
Reply to author
Forward
0 new messages