java - sane representation of lists in XML and JSON using MoXY -


i need expose jax-rs resource in xml , json, part of include passing in (potentially large) lists of integers. i'm using moxy jaxb / json provider.

the problem run cannot figure out how expose list of integers works in both xml , json.

if use

@xmllist @xmlelement(name = "values", type = integer.class) protected list<integer> values; 

then json (un)marshaled

... "values" : "0 1 2 4" ... 

which undesirable when dealing large numbers of numbers. if @xmllist annotation omitted, json dealt properly, xml

... <values>0 1 2 4</values> ... 

is parsed [124] instead of [0, 1, 2, 4].

by using @xmlelementwrapper (and telling moxy "usewrapperasarrayname")

@xmlelementwrapper(name = "values", required = true) @xmlelement(name = "value") protected list<object> values = new arraylist<object>(); 

good json achieved

... "values" : [0, 1, 2, 4] ... 

but imposes tedious xml format of

... <values>   <value>0</value>   <value>1</value>   <value>2</value>   <value>4</value> </values>  ... 

anyone have suggestions how proceed? can't imagine uncommon use-case, yet thoroughly stumped.

below example of how can handle use case:

java model (foo)

this sample class hold property described in question.

package forum23939109;  import java.util.list; import javax.xml.bind.annotation.*;  @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class foo {      @xmllist     @xmlelement(name = "values", type = integer.class)     protected list<integer> values;      public list<integer> getvalues() {         return values;     }      public void setvalues(list<integer> values) {         this.values = values;     }  } 

external metadata (oxm.xml)

<?xml version="1.0"?> <xml-bindings     xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"     package-name="forum23939109">     <java-types>         <java-type name="foo">             <java-attributes>                 <xml-element java-attribute="values" name="values"/>             </java-attributes>         </java-type>     </java-types> </xml-bindings> 

demo code

demo

the demo code below demonstrates how leverage external mapping document in creation of jaxbcontext used handle json content.

package forum23939109;  import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.jaxbcontextproperties;  public class demo {      public static void main(string[] args) throws exception {         // create instance of domain model         foo foo = new foo();         arraylist<integer> values = new arraylist<integer>();         values.add(0);         values.add(1);         values.add(2);         values.add(4);         foo.setvalues(values);          // marshal object xml based on annotations         jaxbcontext jcxml = jaxbcontext.newinstance(foo.class);         marshaller marshallerxml = jcxml.createmarshaller();         marshallerxml.setproperty(marshaller.jaxb_formatted_output, true);         marshallerxml.marshal(foo, system.out);          // marshal object json based on annotations & external mappings         map<string, object> properties = new hashmap<string, object>(3);         properties.put(jaxbcontextproperties.oxm_metadata_source, "forum23939109/oxm.xml");         properties.put(jaxbcontextproperties.media_type, org.eclipse.persistence.oxm.mediatype.application_json);         properties.put(jaxbcontextproperties.json_include_root, false);         jaxbcontext jcjson = jaxbcontext.newinstance(new class[] {foo.class}, properties);         marshaller marshallerjson = jcjson.createmarshaller();         marshallerjson.setproperty(marshaller.jaxb_formatted_output, true);         marshallerjson.marshal(foo, system.out);     }  } 

output

below output running demo code:

<?xml version="1.0" encoding="utf-8"?> <foo>    <values>0 1 2 4</values> </foo> {    "values" : [ 0, 1, 2, 4 ] } 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -