hpehl / piriti

JSON and XML Mapper for GWT
40 stars 16 forks source link

Serializing collection in XML while keeping class name of collection item as tag name in produced XML #10

Open lathil opened 11 years ago

lathil commented 11 years ago

Hello Harald,

I am using Piriti on a Restlet / Gwt project fro sole time now. I had a probleme while serializing a list of objects ex: List with the objects in the list all extending the original type. In the XML produced I need to have the list elements with theirs tag name to be the name of the extending class. Actually the list elements names are derived from the list name ....

Per example I have a List, and objects Volkswagen, Saab and Renault all extend Car. Say I have such a list and objects Volkswagen, Saab and Renault in it. I'll need the poduced XMl to look like:

To manage that, I had to modify the Velocity templates in pririt-dev, in /src/main/resources/name.pehl.pririti.rebind.xml.writer.property.collection.vm, begining in lines 20, from

if (currentValue != null) {

parse($property.templates.elementType)

    }
    else
    {
        collectionXmlBuilder.append(nestedPath);
    }

to:

if (currentValue != null) {

if (!$property.xpath)

            String nestedElementName = currentValue.getClass().getName();
            nestedPath = nestedElementName.substring(nestedElementName.lastIndexOf(".")+1).toLowerCase();
        #end
        #parse($property.templates.elementType)
    }
    else
    {
        collectionXmlBuilder.append("$property.pathOrName");
    }

This way, if in a XMLWrtiter mapping for a collection element, I do not indicate the path, the class name of each collection element is used as the name of the tage, example: @Mapping( value="childrens", path="" ),

I do not have the same problem with the XMLReader part, because I can use this expression: @Mapping( value="childrens", path="child::*", createWith= ObjCreator.class),

Do you think it is something we could push back into piriti ?

Thanks Laurent.

hpehl commented 11 years ago

If this does not break existing tests, seems to be a good idea to have this. Just open a pull request and I will merge that into the codebase asap.

2013/5/30 lathil notifications@github.com

Hello Harald,

I am using Piriti on a Restlet / Gwt project fro sole time now. I had a probleme while serializing a list of objects ex: List with the objects in the list all extending the original type. In the XML produced I need to have the list elements with theirs tag name to be the name of the extending class. Actually the list elements names are derived from the list name ....

Per example I have a List, and objects Volkswagen, Saab and Renault all extend Car. Say I have such a list and objects Volkswagen, Saab and Renault in it. I'll need the poduced XMl to look like:

To manage that, I had to modify the Velocity templates in pririt-dev, in /src/main/resources/name.pehl.pririti.rebind.xml.writer.property.collection.vm, begining in lines 20, from

if (currentValue != null) {

parse($property.templates.elementType)

} else { collectionXmlBuilder.append(nestedPath); }

to:

if (currentValue != null) {

if (!$property.xpath)

String nestedElementName = currentValue.getClass().getName(); nestedPath = nestedElementName.substring(nestedElementName.lastIndexOf(".")+1).toLowerCase();

end

parse($property.templates.elementType)

} else { collectionXmlBuilder.append("$property.pathOrName"); }

This way, if in a XMLWrtiter mapping for a collection element, I do not indicate the path, the class name of each collection element is used as the name of the tage, example: @Mapping https://github.com/Mapping( value="childrens", path="" ),

I do not have the same problem with the XMLReader part, because I can use this expression: @Mapping https://github.com/Mapping( value="childrens", path="child::*", createWith= ObjCreator.class),

Do you think it is something we could push back into piriti ?

Thanks Laurent.

— Reply to this email directly or view it on GitHubhttps://github.com/hpehl/piriti/issues/10 .


Harald Pehl http://hpehl.info