java - How to delegate to default deserialization in custom deserializer in Jackson? -


suppose writing custom serialization class, process 1 of field default methods.

how that?

while serializing have jsongenerator#writeobjectfield().

but corresponding method deserialization?

regard code below:

import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.jsondeserialize; import com.fasterxml.jackson.databind.annotation.jsonserialize;  import java.io.ioexception; import java.util.objects;  public class trydelegate {     public static class myouterclassserializer extends jsonserializer<myouterclass> {        @override       public void serialize(myouterclass value, jsongenerator gen, serializerprovider serializers) throws ioexception, jsonprocessingexception {          gen.writestartobject();           gen.writeobjectfield("inner", value.getinner());           gen.writeendobject();       }    }     public static class myouterclassdeserializer extends jsondeserializer<myouterclass> {       @override       public myouterclass deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception, jsonprocessingexception {            myouterclass ans = new myouterclass();           jsontoken token;           token = p.getcurrenttoken();          if( token != jsontoken.start_object ) {             throw new jsonparseexception("start object expected", p.getcurrentlocation());          }           if( !"inner".equals(p.nextfieldname() ) ) {             throw new jsonparseexception("'inner; field expected", p.getcurrentlocation());          }           myinnerclass inner = null;// how desrialize inner here default processing???          ans.setinner(inner);           token = p.nexttoken();          if( token != jsontoken.end_object ) {             throw new jsonparseexception("end object expected", p.getcurrentlocation());          }           return ans;        }    }     public static class myinnerclass {       private int value;        public int getvalue() {          return value;       }        public void setvalue(int value) {          this.value = value;       }        @override       public string tostring() {          return "{\"value\":" + value + "}";       }    }     @jsondeserialize(using = myouterclassdeserializer.class)    @jsonserialize(using = myouterclassserializer.class)    public static class myouterclass {        private myinnerclass inner;        public myinnerclass getinner() {          return inner;       }        public void setinner(myinnerclass inner) {          this.inner = inner;       }        @override       public string tostring() {          return "{\"inner\":" + objects.tostring(inner) + "}";       }    }     public static void main(string[] args) throws ioexception {        objectmapper mapper = new objectmapper();       string string;        myinnerclass inner = new myinnerclass();       inner.setvalue(12);        myouterclass outer = new myouterclass();       outer.setinner(inner);        string = mapper.writevalueasstring(outer);       system.out.println(string);        myouterclass outer2 = mapper.readvalue(string, myouterclass.class);       system.out.println(outer2); // inner not deserialized      } } 

how implement myouterdeserializer?

the deserializationcontext offers these tools.

after checking field name "inner", move next token, beginning of json object , use deserializationcontext deserialize json object myinnerclass object.

if (!"inner".equals(p.nextfieldname())) {     throw new jsonparseexception("'inner; field expected", p.getcurrentlocation()); } p.nexttoken(); // consumes field name token  myinnerclass inner = ctxt.readvalue(p, myinnerclass.class); 

the javadoc states

convenience method may used composite or container deserializers, reading one-off values contained (for sequences, more efficient fetch deserializer once whole collection).


careful while using deserializationcontext. don't try recursively deserialize types have have registered custom deserializers.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -