java - Jackson xml module: deserializing immutable type with a @JacksonXmlText property -


i want serialize immutable type both json , xml:

the serialized json like:

{     "text" : "... text..." } 

and serialized xml like:

 <astext>_text_</astext> 

(note text xml's element text)

the java object like:

@jsonrootname("astext") @accessors(prefix="_") public static class astext {      @jsonproperty("text") @jacksonxmltext     @getter private final string _text;      public astext(@jsonproperty("text") final string text) {         _text = text;     } } 

beware _text property final (so object immutable) , it's annotated @jacksonxmltext in order serialized xml element's text

being object immutable, constructor text needed , constructor's argument must annotated @jsonproperty

    public astext(@jsonproperty("text") final string text) {         _text = text;     } 

when serializing / deserializing to/from json works fine ... problem arises when serializing / deserializing to/from xml:

 // create object  astext obj = new astext("_text_");   // init mapper  xmlmapper mapper = new xmlmapper();   // write xml  string xml = mapper.writevalueasstring(obj);  log.warn("serialized xml\n{}",xml);   // read xml  log.warn("read xml:");  astext objreadedfromxml = mapper.readvalue(xml,                                               astext.class);  log.warn("obj readed serialized xml: {}",           objreadedfromxml.getclass().getname()); 

the exception is:

     com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field "" (class r01f.types.url.urlquerystringparam), not marked ignorable (2 known properties: "value", "name"]) 

it seems xml module needs object's constructor annotated like:

    public astext(@jsonproperty("") final string text) {         _text = text;     } 

but not works:

     com.fasterxml.jackson.databind.exc.invaliddefinitionexception: cannot construct instance of `test.types.serializeasxmlelementtexttest$astext` (no creators, default construct, exist): cannot deserialize object value (no delegate- or property-based creator) 

the annotation @jsonproperty("text") @ constructor's argument needed deserialize json

... how can make work

try adding public getter property. believe should fix deserialization issue.

@jsonrootname("astext") @accessors(prefix = "_") public static class astext {      @jsonproperty("text")     @jacksonxmltext     @getter     private final string _text;      public astext(@jsonproperty("text") final string text) {         _text = text;     }      public string gettext() {         return _text;     } } 

actually, works without adding getter too, these versions of lombak & jackson.

<dependencies>     <dependency>         <groupid>com.fasterxml.jackson.dataformat</groupid>         <artifactid>jackson-dataformat-xml</artifactid>         <version>2.9.0</version>     </dependency>      <dependency>         <groupid>org.projectlombok</groupid>         <artifactid>lombok</artifactid>         <version>1.16.18</version>     </dependency> </dependencies> 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -