Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Marshal list of objects to json with Moxy 2.7.3 problem


Marshaling a list of objects with Moxy 2.7.3 (and glassfish javax.json 1.1.4) does not work as expected. Only the first object is marshaled as a single object. It woks as expected with Moxy 2.7.2. Is this a bug or my code is not quite right?

With Moxy 2.7.2 the json is
{"elements":[{"name":"min","value":"1"},{"name":"max","value":"100"}]}

With Moxy 2.7.3 the json is
{"elements":{"name":"max","value":"100"}}

Code
Root root = new Root();
root.addElement("min", "1");
root.addElement("max", "100");
JsonObjectBuilder builder = Json.createObjectBuilder();
JsonObjectBuilderResult result = new JsonObjectBuilderResult(builder);
JAXBContext jc = JAXBContext.newInstance(root.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(root, result);
JsonWriterFactory writerFactory = Json.createWriterFactory(null);
JsonWriter writer = writerFactory.createWriter(System.out);
writer.writeObject(builder.build());
writer.close();

// Root class:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Root {
@XmlElement private List<Element> elements = new ArrayList<>();
public void addElement(String name, String value) {
Element e = new Element();
e.setName(name);
e.setValue(value);
elements.add(e);
}
}

// Element class:
import javax.xml.bind.annotation.XmlElement;
public class Element {
@XmlElement private String name;
@XmlElement private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Thank you


Back to the top