Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

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

Open
lathil opened this issue May 30, 2013 · 1 comment
Assignees
Milestone

Comments

@lathil
Copy link
Contributor

lathil commented May 30, 2013

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
Copy link
Owner

hpehl commented May 30, 2013

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 [email protected]

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//issues/10
.


Harald Pehl
http://hpehl.info

@ghost ghost assigned hpehl May 30, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants