0
@Builder
@Getter
public class POJOClass {
    @NonNull
    private List<String> states;

    @NonNull
    private String country;

    @NonNull
    private String capital;

    public Map<String,String> convertToMap() {
        TypeReference<HashMap<String, String>> hashMapType
                = new TypeReference<HashMap<String, String>>() {};
        return new ObjectMapper().convertValue(this, hashMapType);
    }
}

i am trying to convert an instance of the above class into a HashMap<String,String> using instance.convertToMap(), but this fails with error

Can not deserialize instance of java.lang.String out of START_ARRAY token
    at [Source: N/A; line: -1, column: -1] (through reference chain: java.util.HashMap["states"])
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3605)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3546)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3534)

I believe this is because of the List when deserialising to map isnt getting converted to String.

Is there a way where i can convert a POJO to Map<String,String> and i dont want a type of Map<String, Object> ?

4
  • 1
    You need to past in the relevant json. Commented Dec 27, 2017 at 19:54
  • Can you elaborate a little more ? I am trying to convert an instance of the class to the Map<String,String> without me dealing directly with the json of it. Commented Dec 27, 2017 at 20:00
  • what output are you expecting? a List doesn't naturally convert into a Map irrespective of the mechanism Commented Dec 27, 2017 at 20:37
  • I need the output of the method convertToMap be a HasMap<String,String> . I need the list to be converted into a string and be stored in the HasMap<String,string> Commented Dec 28, 2017 at 4:44

1 Answer 1

1

You cannot do the TypeReference of type Map<String, String> since states is not a string, but a list, so you can change the TypeReference into Map<String,Object> and this should over even if you have a list or if you want in the future to add a complex object

public Map<String, String> convertToMap() {
    TypeReference<HashMap<String, Object>> hashMapType = new TypeReference<HashMap<String, Object>>() {
    };
    return new ObjectMapper().convertValue(this, hashMapType);
}

UPDATE:

You can still leave the Map<String, String> as the return type of the method convertToMap()

Full example:

@Builder
@Getter
public class POJOClass {
    @NonNull
    private List<String> states;

    @NonNull
    private String country;

    @NonNull
    private String capital;

    public Map<String, String> convertToMap() {
        TypeReference<HashMap<String, Object>> hashMapType = new TypeReference<HashMap<String, Object>>() {
        };
        return new ObjectMapper().convertValue(this, hashMapType);
    }


    public static void main(String[] args) {
        POJOClass pojoClass = new POJOClass(Lists.newArrayList("a", "b", "c"), "England", "London");
        System.out.println(pojoClass.convertToMap());
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

if I have the type reference as map<string,object> . then wouldn't the map returned by the convertToMap method also be a HashMap<String,Object> ?
but there is a constraint that i need to return a HashMap<String, String> irrespective of the type of fields the POJOClass has. So, is there any way i can return a HashMap<String, String> class, like some how can i convert the list to string while creating the HashMap ?
@achut1993, as you can see, I am returning Map<String, String>. I am just doing the convert to Map<String, Object> only when doing the TypeReference. This works fine. I will update my answer to show you an example
This doesnt work as if you do pojoClass.convertToMap().get("states") it is a List not String.
@achut1993 List<String> states = Collections.singletonList(pojoClass.convertToMap().get("states"));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.