Welcome!
The input listeField property looks like a String that is really an embedded JSON array of String objects. This happens. So you need to do further processing of that String property.
You can use a JsonDeserializer that converts a String into a list of strings, and there are many ways to do this. Since in this example, the input string looks suspiciously like an embedded JSON object, you could use another mapper to convert the value to an array, like so:
public class Deserialize {
static class Example {
@JsonProperty
String stringField;
@JsonProperty()
@JsonDeserialize(using = JsonStringArrayToSTringList.class)
List<String> listField;
@Override
public String toString() {
return "Example [stringField=" + stringField + ", listField=" + listField + "]";
}
}
static class JsonStringArrayToSTringList extends JsonDeserializer<List<String>> {
private ObjectMapper m = new ObjectMapper();
@SuppressWarnings("unchecked")
@Override
public List<String> deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JacksonException {
return m.readValue(ctxt.readValue(p, String.class), ArrayList.class);
}
}
public static void main(String[] args) throws Exception {
String input = "{\n" + "\"stringField\": \"abc\",\n"
+ "\"listField\": \"[\\\"item1\\\", \\\"item2\\\", \\\"item3\\\"]\"\n" + "}";
var om = new ObjectMapper();
var c = om.readValue(input, Example.class);
System.out.println(c);
}
}
The usage of a new ObjectMapper for each instance of the converter may be costly, so you can probably elaborate on a more efficient way to convert the input string to a list of strings. Hopefully you get the idea.