Using @JsonDeserialize
at Class Level.
Last active
July 19, 2020 20:26
-
-
Save abhi2495/308724a3ca59cf0c9d32b54cf14bce2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
################################################################################## | |
################################################################################## | |
######### IF YOU FOUND THIS GIST USEFUL, PLEASE LEAVE A STAR. THANKS. ############ | |
################################################################################## | |
################################################################################## | |
*/ | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class MyApp { | |
public static void main(String[] args) throws JsonProcessingException { | |
String json = "{\n" + | |
" \"names\": \"John, Tom\",\n" + | |
" \"values\": \"8, 9\",\n" + | |
" \"statuses\": \"yes, no\"\n" + | |
"}"; | |
MyBean myBean = new ObjectMapper().readValue(json,MyBean.class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
import java.util.List; | |
@JsonDeserialize(using = MyBeanDeserializer.class) | |
public class MyBean { | |
private List<String> names; | |
private List<Integer> values; | |
private List<StatusEnum> statuses; | |
public List<String> getNames() { | |
return names; | |
} | |
public void setNames(List<String> names) { | |
this.names = names; | |
} | |
public List<Integer> getValues() { | |
return values; | |
} | |
public void setValues(List<Integer> values) { | |
this.values = values; | |
} | |
public List<StatusEnum> getStatuses() { | |
return statuses; | |
} | |
public void setStatuses(List<StatusEnum> statuses) { | |
this.statuses = statuses; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.JsonDeserializer; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.node.TextNode; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MyBeanDeserializer extends JsonDeserializer<MyBean> { | |
private static final String DELIMITER = ", "; | |
@Override | |
public MyBean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { | |
JsonNode node = p.getCodec().readTree(p); | |
String names = ((TextNode) node.get("names")).textValue(); | |
String values = ((TextNode) node.get("values")).textValue(); | |
String statuses = ((TextNode) node.get("statuses")).textValue(); | |
MyBean myBean = new MyBean(); | |
myBean.setNames(convertStringToList(names)); | |
myBean.setValues(convertStringToList(values)); | |
myBean.setStatuses(convertStringToList(statuses)); | |
return myBean; | |
} | |
private List convertStringToList(String text) { | |
List myList = new ArrayList(); | |
for (String s : text.split(DELIMITER)) { | |
try { | |
Integer myInt = Integer.parseInt(s); | |
myList.add(myInt);//add int to your integer list | |
} catch (NumberFormatException e) { | |
//Not an integer | |
StatusEnum myEnum = StatusEnum.getStatusEnum(s); | |
if (myEnum == null) { | |
myList.add(s);//add String to your String list | |
} else { | |
myList.add(myEnum);//add enum to your enum list | |
} | |
} | |
} | |
return myList; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum StatusEnum { | |
YES("yes"), | |
NO("no"); | |
private String value; | |
StatusEnum(String value) { | |
this.value = value; | |
} | |
public final String getValue() { | |
return this.value; | |
} | |
public static StatusEnum getStatusEnum(String value) { | |
for (StatusEnum status : values()) { | |
if (status.getValue().equals(value)) { | |
return status; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment