2

I've got a JSON, which I parse to a List of Result objects. This object contains 5 string fields, called photo1, photo2 etc. (based on the JSON) Is it possible to read them directly into a List field?

JSON likes:

{
    "ErrorMessage": null,
    "Result": [{
        "id": "462290",
        "name_English": "name in english",
        "name_Local": "külföldiül a név",
        "zipcode": "5463",
        "photo1": "dfglkj.com/blabla",
        "photo2": "dfglkj.com/blabla",
        "photo3": "dfglkj.com/blabla",
        "photo4": "dfglkj.com/blabla",
        "photo5": "dfglkj.com/blabla"
    }]
}

and my object:

static final class ApiResponse
    {
        public String ErrorMessage;
        public List<Result> Result = new ArrayList<Result>();
    }

 static final class Result
    {
        public String id;
        public String name_English;
        public String name_Local;
        public List<String> photos;
        public String zipcode;
    }

I have an ObjectMapper:

private static ObjectMapper newObjectMapper()
        {
            final ObjectMapper om =
                new ObjectMapper()  //
                .configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)  //
                .configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);

            om.registerSubtypes(ApiResponse.class);

            return om;
        }

And in the parser:

final ApiResponse ret = OM.readValue(inputStream, ApiResponse.class);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
krtsz
  • 75
  • 8

3 Answers3

2

Based on Gaetano's answer I wrote a more general solution (any number of photos, any number of other fields

@Override public List<Result> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
        {
            JsonNode node = jp.getCodec().readTree(jp);
            List<Result> listResult = new ArrayList<Result>();

            for (JsonNode interNode : node)
            {
                Result result = new Result();

                for (int i = 1; i < 30; i++)
                {
                    if (interNode.get("photo" + i) != null)
                    {
                        result.photos.add(interNode.get("photo" + i).getTextValue());
                    }
                    else
                    {
                        break;
                    }
                }

                for (Field field : result.getClass().getDeclaredFields())
                {
                    if (interNode.get(field.getName()) != null)
                    {
                        try
                        {
                            field.set(result, interNode.get(field.getName()).getTextValue());
                        }
                        catch (IllegalAccessException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }

                listResult.add(result);
            }

            return listResult;
        }
krtsz
  • 75
  • 8
  • This is a nice solution. Please pay some attention to the performance . when using reflection the things slow down a lot: https://stackoverflow.com/questions/435553/java-reflection-performance – Gaetano Piazzolla Oct 05 '17 at 08:14
1

The best solution I've found is to define a Custom Deserializer for your ObjectMapper.

You can tell JACKSON to use the custom deserializer with a specific annotation of the list of "Result" objects on the ApiResponse class:

public class ApiResponse {

    private String errorMessage;

    @JsonDeserialize(using = CustomDeserializer.class)
    private List<Result> result = new ArrayList<Result>();

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String arg) {
        errorMessage = arg;
    }

    public List<Result> getResult() {
        if(result==null)
        {
            result = new ArrayList<Result>();
        }
        return result;
    }

    public void setResult(List<Result> arg) {
        result = arg;
    }
}

public class Result {
     private String id;
     private String name_English;
     private String name_Local;
     private List<String> photos;
     private String zipcode;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName_English() {
        return name_English;
    }
    public void setName_English(String name_English) {
        this.name_English = name_English;
    }
    public String getName_Local() {
        return name_Local;
    }
    public void setName_Local(String name_Local) {
        this.name_Local = name_Local;
    }
    public List<String> getPhotos() {
        if(photos == null)
        {
            photos = new ArrayList<String>();
        }
        return photos;
    }
    public void setPhotos(List<String> photos) {
        this.photos = photos;
    }
    public String getZipcode() {
        return zipcode;
    }
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
}

Please pay some attention to the java naming convenction : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

The custom deserializer will look like this:

public class CustomDeserializer extends StdDeserializer<List<Result>> {

private static final long serialVersionUID = -3483096770025118080L;

public CustomDeserializer() {
    this(null);
}

public CustomDeserializer(Class<?> vc) {
    super(vc);
}

@Override
public List<Result> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.getCodec().readTree(jp);

    List<Result> listResult = new ArrayList<Result>();
    for (JsonNode interNode : node) {

        Result result = new Result();           

        if (interNode.get("id") != null) {
            result.setId(interNode.get("id").asText());
        }
        if (interNode.get("name_English") != null) {
            result.setName_English(interNode.get("name_English").asText());
        }
        if (interNode.get("name_Local")!= null) {
            result.setName_Local(interNode.get("name_Local").asText());
        }
        if (interNode.get("zipcode") !=null ) {
            result.setZipcode(interNode.get("zipcode").asText());
        }

        // photo array
        if (interNode.get("photo1") != null) {
            result.getPhotos().add(interNode.get("photo1").asText());
        }
        if (interNode.get("photo2") != null) {
            result.getPhotos().add(interNode.get("photo2").asText());
        }
        if (interNode.get("photo3") != null) {
            result.getPhotos().add(interNode.get("photo3").asText());
        }
        if (interNode.get("photo4") != null) {
            result.getPhotos().add(interNode.get("photo4").asText());
        }
        if (interNode.get("photo5") != null) {
            result.getPhotos().add(interNode.get("photo5").asText());
        }

        listResult.add(result);
    }

    return listResult;
}
Gaetano Piazzolla
  • 1,388
  • 1
  • 16
  • 31
1

If you can change your POJO code then the easiest option is to use @JsonAnySetter and @JsonAnyGetter and write custom code there.(Like @Oleg suggested) But don't forget to @JsonIgnore List<String> photos.

@Data // lombok.Data; for getters and setters
public static final class Result {
    public String id;
    public String name_English;
    public String name_Local;
    @JsonIgnore
    public List<String> photos = new ArrayList<>();
    public String zipcode;

    @JsonAnySetter
    public void setOther(String key, String value){
        photos.add(value);
    }
    @JsonAnyGetter
    public Map<String,String> getOther(){
        Map<String,String> map = new HashMap<>();
        for (int i = 0;i<photos.size();i++)
            map.put("photo" + i + 1, photos.get(i));
        return map;
    }
}

But if you can't change POJO code you will have to write custom serializer and deserializer

zafrost
  • 576
  • 3
  • 3