Giter Site home page Giter Site logo

Comments (5)

joehni avatar joehni commented on June 29, 2024

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.

QIvan avatar QIvan commented on June 29, 2024

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.

joehni avatar joehni commented on June 29, 2024

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.

morvael avatar morvael commented on June 29, 2024

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.

joehni avatar joehni commented on June 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.