I’m trying to deserialize json to a java entity, but I’m having trouble getting Jackson to deserialize an json array to a Pair[]. Pair is a custom class but it follows the standard form found on SO :
public class Pair<K, V> {
private final K k;
private final V v;
public Pair(K k, V v) {
this.k = k;
this.v = v;
}
public K getKey() {
return k;
}
public V getValue() {
return v;
}
...
}
The Json object can be changed as needed, but I’m trying to use something of the form:
{"priority":1,"account":"012345","tld":"com","name":"domain_check","params":[{"key":"domain","value":"domain.com"}]}
My “DefaultRequest” entity class contains:
@Entity
public class DefaultRequest implements Serializable, Comparable, Request {
//other fields...
private Pair[] params;
@Override
public Pair[] getParams() {
return params;
}
@Override
public void setParams(Pair[] params) {
this.params = params;
//other methods...
}
}
I’m sorry if someone has answered this already. I’m admittedly new to using jackson, but I’ve spent almost 3 days on this and I have a deadline. I also have to believe someone has had to deserialize to an entity containing a Pair[] before. Any ideas are welcome. I have complete flexibility in changing both the json format and the entity class if there is a better alternative.
Thanks in advance!
Question is bit vague, so I might be wrong, but you may be looking for the annotation to tell Jackson how to use a custom constructor to pass data. If so:
This will work for all kinds of types, not just primitives, nor does it try to guess what goes where. Annotations for names are needed only because JDK does not add argument names in bytecode, so they are not available, unlike method names.
EDIT:
Looks like a transformation is needed. So how about this:
Bit more code, but allows conversions to intermediate types. For what it’s worth, Jackson 2.1 will include standard “delegating” serializer/deserializer, to make it possible to define a Converter that encapsulates such logic.