Details
-
Type: New Feature
-
Status: Closed
-
Priority: Major
-
Resolution: Duplicate
-
Affects Version/s: None
-
Fix Version/s: 1.x Maintenance
-
Component/s: None
-
Labels:None
Description
Currently, the xml XStream is created is quite straight forward and sometimes it is producing unnecessary tags for the object properties. For example:
class Student
{
String name="blah";
String lastname = "bloh";
}
public class School
{
String name = "My school";
int numberOfStudents = 1234;
List students;
}
AFAIK, current XSteam is producing this XML for such classes (lets assume we make necessary aliases)
<school>
<name>My School</name>
<numberOfStudents>1234</numberOfStudents>
<students>
<student>
<name>blah</name>
<surname>bloh</name>
</student>
....
</students>
</school>
But if you make primitive and String (and perhaps StringBuffer) properties of a class as attributes of an xml element, resulting xml qould be much shorter. like:
<school name="My School" numberOfStudents="1234">
<students>
<student name="blah" surname="bloh" />
....
</students>
</school>
i gues it does not disturb the background work of the XStream becuase there still is no configuration file whatsoever. only a set value like "MAKE_PRIMITIVES_ATTRIBUTES" would be enough for the user.
Issue Links
- duplicates
-
XSTR-613 CLONE -Make an option to create primitive and String properties as attributes.
The options to set specific fields to be represented as an XML attributes essentially enables us to solve our problems, but it is not convenient since it must be done on a per field basis.
We currently have a couple of options.
1) @XStreamAsAttribute on every single primative, in every Bean.
2) Looping through our full set of beans and calling xstream.useAttributeFor(clazz, field.getName()); on all primatives.
Because we use both the JSON output and the XML output from the same set of beans we can not use @XStreamAsAttribute since that adds @ signs to all JSON output.
We need to be able to determine the output for XML and JSON by modifying the XStream instance and not the bean.
Side note, It would be nice to be able to disable "@" notation in the JsonHierarchicalStreamDriver since in our case these @'s add not value to the JavaScript that reads the JSON.
However, it would be really better to have something similar to
XStream.primativesAsAttributes(true)
XStream.stringsAsAttributes(true)
XStream.datesAsAttributes(true)
and the ocasional @XStreamAsElement, for Strings that have problems being represented as XML attributes.
We currently do option 2) but can not resolve the occasional arbitrary Strings issue.
There is rarely a situation where it is better to represent an int or boolean as a nested XML element as opposed to an attribute; if you are concerned about the final size of the XML, as we are.
So I think the original request for "MAKE_PRIMITIVES_ATTRIBUTES" really still should be open.