java - Jackson: different XML and JSON format -


in apache wink based rest project, using jackson jax-rs providers handling both json , xml content type. our response object contains hashmap<string, propertyobject>. map key contains whitespaces , hence can't serialized xml element names. json serialization works fine.

json format:

{     "properties": {           "a b c":  {                                "name": "a b c",                             "type": "double",                             "value": "2.0"                     },           "x y z":  {                             "name": "x y z",                             "type": "double",                             "value": "0.0"                     }         } } 

desired xml format

<rootelement>     <property name="a b c" type="double" value="2.0">     <property name="x y z" type="double" value="0.0"> </rootelement> 

how can achieve using jackson jax-rs xml , json providers. possible define custom jackson serializers , extend jax-rs providers. please suggest best possible approach.

one way can change xml use jaxb xmladapter. like

public class propertyadapter extends xmladapter<propertyadapter.adaptedproperties,                                                  hashmap<string, propertyobject>> {      public static class adaptedproperties {         public arraylist<propertyobject> property = new arraylist<>();     }      @override     public hashmap<string, propertyobject> unmarshal(                         propertyadapter.adaptedproperties list) throws exception {          hashmap<string, propertyobject> map = new hashmap<>();         (propertyobject prop: list.property) {             map.put(prop.getname(), prop);         }         return map;     }      @override     public propertyadapter.adaptedproperties marshal(                            hashmap<string, propertyobject> map) throws exception {          arraylist<propertyobject> list = new arraylist<>(map.values());         adaptedproperties props = new adaptedproperties();         props.property = list;         return props;     } } 

then annotate field/property

@xmlrootelement public class dto {      private hashmap<string, propertyobject> properties;      @xmljavatypeadapter(propertyadapter.class)     public hashmap<string, propertyobject> getproperties() {         return properties;     }      // setter } 

this result. it's not how want it, can't figure out how without <properties> wrapper.

<root>     <properties>         <property name="a b c" type="double" value="2.0"/>         <property name="x y z" type="double" value="0.0"/>     </properties> </root> 

one thing need make sure of when using jackson, aren't using provider has jaxb annotation support configured. if use provider, use xmladapter , json array, instead of object/map have. assuming using this provider, has jacksonjaxbjsonprovider , plain jacksonjsonprovider. want make sure register latter.


edit

oh , forgot add propertyobject class used test looks this

@xmlrootelement public class propertyobject {      private string name;      @xmlattribute(name = "name")     public string getname() {         return name;     }      // there `type` , `value` fields also, corresponding     // getters , setters, both using `@xmlattribute` } 

update

let's assume did want jaxb annotation support jackson json serialization. can configure own objectmapper, , use 1 jaxb support 1 class. can see how possible here


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -