I have a json like
{
"key" : ["key1", "key2", "key3"],
"value" : "v1";
}
I am De-serializing it to my Pojo
class using jackson, While De-serializing I want variable value
to be type of List<String>
whose size will depend on the size of variable key
. So that final object will represent this Json.
{
"key" : ["key1", "key2", "key3"],
"value" : ["v1", "v1", "v1"];
}
So far my Pojo
class is like this
public class Pojo {
@JsonProperty("key")
private List<String> key;
@JsonProperty("value")
private List<String> value;
@JsonProperty("key")
public List<String> getKey() {
return key;
}
@JsonProperty("key")
public void setKey(List<String> key) {
this.key = key;
}
@JsonProperty("value")
public List<String> getValue() {
return value;
}
@JsonProperty("value")
public void setValue(String val) {
List<String> arr = new ArrayList<String>();
for (int i=0; i<key.size(); i++) {
arr.add(val);
}
this.value = arr;
}
}
but I am getting JsonMappingException
. While debugging I found that inside the setValue method variable key
is null
. Is there a way to set the value of variable key
first?( before variable value
)
You should hide this ugly conversion in custom deserializer class. It could look like this:
class PojoJsonDeserializer extends JsonDeserializer<Pojo> {
@Override
public Pojo deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
InnerPojo innerPojo = jp.readValueAs(InnerPojo.class);
return innerPojo.toPojo();
}
private static class InnerPojo {
public List<String> key;
public String value;
Pojo toPojo() {
Pojo pojo = new Pojo();
pojo.setKey(new ArrayList<String>(key));
pojo.setValue(valueNTimes(value, key.size()));
return pojo;
}
private List<String> valueNTimes(String value, int nTimes) {
List<String> result = new ArrayList<String>(nTimes);
for (int index = 0; index < nTimes; index++) {
result.add(value);
}
return result;
}
}
}
Your POJO class could look "natural" now:
@JsonDeserialize(using = PojoJsonDeserializer.class)
class Pojo {
private List<String> key;
private List<String> value;
public List<String> getKey() {
return key;
}
public void setKey(List<String> key) {
this.key = key;
}
public List<String> getValue() {
return value;
}
public void setValue(List<String> value) {
this.value = value;
}
@Override
public String toString() {
return "Pojo [key=" + key + ", value=" + value + "]";
}
}
Simple test program:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public class JacksonProgram {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(json, Pojo.class));
}
}
prints:
Pojo [key=[key1, key2, key3], value=[v1, v1, v1]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With