I am trying to write an abstract class which will map the string values to the object member-variables and vice versa using ObjectMapper of jackson-databind. This abstract class will be extended by each pojo of json.
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public abstract class JsonToString {
public JsonToString toObject(String jsonString){
ObjectMapper mapper = new ObjectMapper();
try {
mapper.readValue(jsonString, this.getClass());//problem
System.out.println("inside object function of jsontostring : "+this);
} catch (JsonParseException e) {
System.out.println("Exception occured in mapping jsonString received to object" + e);
} catch (JsonMappingException e) {
System.out.println("Exception occured in mapping jsonString received to object" + e);
} catch (IOException e) {
System.out.println("Exception occured in mapping jsonString received to object" + e);
}
return this;
}
public String toString(){
ObjectMapper mapper = new ObjectMapper();
String json = new String();
try {
json = mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
System.out.println("Trouble in mapping object to string in json class : "+this.getClass().getName());
}
return json;
}
}
this class will be extended by each json pojo.
So here I want to return the object of child class for which the mappings has been done. Can someone please help me get the object and return it.
Aucun commentaire:
Enregistrer un commentaire