Comments (5)
Every field is normally written as own child element with the field name as name of the XML tag. If the field is actually a Collection (resp. Map or Array) you can configure that the tag with the field name is omitted and the elements of the collection are written directly as children.
However, this does not apply to the ToAttributedValueConverter. The value field is never surrounded by an own tag with the field's name. Therefore defining an implicit collection for the value field is simply ignored ... the value field is already implicit.
So you define either an alias for the type NumberOfPersonnel or you do not use the ToAttributedValueConverter:
@XStreamAlias("container")
public class Container {
@XStreamAsAttribute
private String name;
// any element named numberOfEmployees should go into this list
@XStreamImplicit(itemFieldName = "numberOfEmployees")
protected List<NumberOfPersonnel> numberOfEmployees;
public Container(String name, List<NumberOfPersonnel> noEmp) {
this.name = name;
this.numberOfEmployees = noEmp;
}
public String toString() {
return name + ", " + numberOfEmployees;
}
}
from xstream.
Ok, I've added alias for NumberOfPersonnel
, but marshalling looks like this:
<container name="World" class="java.util.Arrays$ArrayList">
<a class="personnel-array">
<personnel year="2001">1000.0</personnel>
<personnel year="2002">500.0</personnel>
</a>
</container>
and it still have a
tag and information about class (class="java.util.Arrays$ArrayList"
)
Is there no way to do marshalling a little bit cleaner? Like this:
<container name="World">
<personnel year="2001">1000.0</personnel>
<personnel year="2002">500.0</personnel>
</container>
or
<container name="World">
<personnel>
<year>2001</year>
<value>1000.0</value>
</personnel>
<personnel>
<year>2002</year>
<value>500.0</value>
</personnel>
</container>
My code now is
public void testName() throws Exception {
XStream xs = new XStream();
xs.processAnnotations(Container.class);
System.out.println("Marshalling:");
System.out.println(xs.toXML(new Container("World",
Arrays.asList(new NumberOfPersonnel(2001, 1000),
new NumberOfPersonnel(2002, 500)))));
}
@XStreamAlias("personnel")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = {"value"})
public static class NumberOfPersonnel {
public NumberOfPersonnel(int year, double value) {
this.year = year;
this.value = value;
}
private int year;
private double value;
}
@XStreamAlias("container")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = "numberOfEmployees")
public static class Container {
private String name;
// I understood this annotation is useless
@XStreamImplicit(itemFieldName = "numberOfEmployees")
protected List<NumberOfPersonnel> numberOfEmployees;
public Container(String name, List<NumberOfPersonnel> noEmp) {
this.name = name;
this.numberOfEmployees = noEmp;
}
}
from xstream.
It marshals what you put in. List is an interface and XStream's default implementation is ArrayList. You set this field to an instance of a different implementation, therefore XStream adds a class attribute to keep the type (java.util.Arrays$ArrayList) and since there is no special converter for it, it is handled by the ReflectionConverter. That's what it looks like.
See, XStream's promise is to turn an object graph into XML and back again. It puts a great deal into the recreated graph to provide again objects with the same type and values. If numberOfEmployees contains a CopyOnWriteArrayList in the original graph, I am quite sure, you would expect to have the same list type after the marshalling.
from xstream.
Perhaps the XStreamImplicit annotation could be expanded by adding the ability to declare default collection class to be used upon deserialization? It should then write the "class" attribute only when the actual and defined types do not match.
from xstream.
Actually XStream could support local default implementations in general like it does for local converters. But that's a separate issue.
from xstream.
Related Issues (20)
- how to remove the class attribute when serializing Java Bean to xml? HOT 1
- `PrettyPrintWriter` cannot write emoji in XML 1.1 mode HOT 1
- how to parse xml string into class with genericity HOT 3
- How to set byte array not to do conversion?Thanks. HOT 1
- Unmodifiable Lists converter HOT 1
- how both text and sub tags exist HOT 3
- DomReader: slow performance $O(n^2)$ when deserialize on millions of elements to list HOT 3
- B
- Is there any annotations can support both attribute and sub tags HOT 1
- Support Record Classes HOT 1
- com.thoughtworks.xstream.core.util is not exported in OSGI manifest HOT 2
- Avoid SimpleDateFormat
- Maven errors with java 21 HOT 1
- Problem of TreeSet/TreeMap HOT 4
- The vulnerabilities database NVD only finds xstream component with VENDOR= xstream_project HOT 1
- When will 1.5 released with hibernate 6 support? HOT 6
- Deserializing large files is slow on low-performance devices HOT 1
- CustomObjectInputStream's readInt throws a cast error if the read data is a Byte. HOT 6
- Alias using serialization proxy pattern ? HOT 2
- Posting/verifying signing keys used for artifacts in Maven Central? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xstream.