I have a class Box
which has a field elements
which is of type Elements
that implements Iterable
(see the code below). The latter contains a List
which is also called elements
If I set the field elements
of Elements
to be private
, then the serialization would treat it as an array rather than a POJO
Serialization done by Version A
{
"elements" : [ "a", "b", "c" ]
}
However, I could not deserialize this string. The error was:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `ser_deser_test.Elements` out of START_ARRAY token
at [Source: (String)"{
"elements" : [ "a", "b", "c" ]
}"; line: 2, column: 16] (through reference chain:
If I, instead, set elements
to be public
, then elements
of Box
would be treated as a POJO, and I had this funny two levels of elements
in it. The deserialization worked in this case, however.
Serialization by Version B
{
"elements" : {
"elements" : [ "a", "b", "c" ]
}
}
My question is: how can I get deserializatio to work for Version A?
Code
Box.java
package ser_deser_test;
public class Box {
public Box() {
super();
}
public Elements elements;
public Box(Elements elements) {
super();
this.elements = elements;
}
}
Elements.java
package ser_deser_test;
import java.util.Iterator;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Elements implements Iterable<String> {
// private List<String> elements; // Version A
// public List<String> elements; // Version B
public Elements() {
super();
}
public Elements(List<String> elements) {
super();
this.elements = elements;
}
@Override
public Iterator<String> iterator() {
return elements.iterator();
}
}
TestSerDeser.java
package ser_deser_test;
import java.io.IOException;
import java.util.Arrays;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestSerDeser {
public static void main(String[] args) {
Elements elements = new Elements(Arrays.asList("a", "b", "c"));
Box box = new Box(elements);
ObjectMapper om = new ObjectMapper();
om.enable(SerializationFeature.INDENT_OUTPUT);
try {
// Serialize
String s = om.writeValueAsString(box);
// Deserialize
Box box2 = om.readValue(s, Box.class);
boolean dummy = false;
} catch (JsonProcessingException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}