2

I have a Json response that looks like this:

[
    { "name":"A" },
    { "name":"B" }
]

I have Java classes representing a single ResponseDto and contains a List of Person:

public class GetPersonsResponseDto {
    public List<Person> persons;
}

public class Person {
    public String name;
}

I would like to deserialize the by JSON using ObjectMapper but without use of a custom Deserializer and without collection type (no Persons[].class or TypeReference<List<Person>>(){}). What I really want is

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(in, GetPersonsResponseDto.class);

But I get:

jackson.map.JsonMappingException: 
Can not deserialize instance of com.project.my.GetPersonsResponseDto out of START_ARRAY token

I tried several Annotations but without success.

No3x
  • 470
  • 1
  • 8
  • 28
  • what annotations did you try? – Tan Yu Hau Sean Feb 03 '23 at 16:38
  • `@JsonFormat(shape=JsonFormat.Shape.ARRAY)`, `@JsonValue`, `@JsonProperty` without change – No3x Feb 03 '23 at 16:44
  • 2
    have you tried this? https://stackoverflow.com/questions/69846490/how-to-fix-error-start-array-token-when-deserializing-json-with-modelmapper – kerbermeister Feb 03 '23 at 17:09
  • thanks for pointing us in the right direction kerbermeister, all the searches i was making either didnt have a solution or was using custom deserializer and was quite convoluted – Tan Yu Hau Sean Feb 03 '23 at 17:17
  • @kerbermeister I also tried `@JsonCreator` but it did not work. Now I tried again on my non-corporate machine and it works! Maybe there is a difference in the versions. Please post it as answer. – No3x Feb 03 '23 at 17:20
  • you're welcome, posted an answer, however, I agree it depends on the version of jackson you're using – kerbermeister Feb 03 '23 at 17:25
  • @kerbermeister so i might not able to use this approach because I'm tied to the platform versions in corporate field. Maybe someone knows an alternative to `@JsonCreator`? – No3x Feb 03 '23 at 17:27
  • I'll try to research this – kerbermeister Feb 03 '23 at 17:32
  • Seems to work on both machines/versions. Tested 2.13.4, 2.14.1 – No3x Feb 03 '23 at 18:13

1 Answers1

3

Actually it is quite simple to serialize your models to the target json, but might be tricky to deserialize.

So, the solution for deserialization in this case could be using @JsonCreator annotation from com.fasterxml.jackson.annotation package above constructor:

@Data
public class GetPersonsReponseDto {

    public List<Person> persons;

    @JsonCreator // use this annotation for deserialization on constructor
    public GetPersonsReponseDto(List<Person> persons) {
        this.persons = persons;
    }

    public GetPersonsReponseDto() {
    }
}

However, it might not work with some versions of jackson.

kerbermeister
  • 2,985
  • 3
  • 11
  • 30
  • Unfortunately I'm able to add annotations to fields, getter, setter or class only because of the limitations of code generation framework for beans. I'm looking for an alternative. – No3x Feb 03 '23 at 18:15