1

I am trying to convert a Map to Map. I have a json and Object :

{
        A: { availableInfo: { isAvailable : true} },
        VV: { availableInfo: { isAvailable : false} },
        B45: { availableInfo: { isAvailable : null} }
}

And the Object :

@Builder
@Getter
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AvailableInfo {

    @JsonProperty("isAvailable")
    private Boolean isAvailable;

    public AvailableInfo() {
    }
}

I tried :

Map<String, AvailableInfo>  response = getResponse(query, Map.class);

But I am getting Error:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to AvailableInfo

Below is the getResponse() method :

private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = client.call(GET, query);

        if (isResponseOK(tempResponse, query)) {
            byte[] payloads = {};

            for (Record rec : tempResponse.getResponses()) {
                final byte[] payload = rec.getPayload();
                payloads = ArrayUtils.addAll(payloads, payload);
            }
            final Reader reader = newReader(payloads)
            response = jsonObjectMapper.readValue(reader, responseClass);
        }

        return response; //returns Map<String, LinkedHashMap>
    }

Any idea how it can be resolved? I tried changing the input json to :

   {
            A:  { isAvailable : true} ,
            VV:  { isAvailable : false} ,
            B45:  { isAvailable : null} 
    }

But still not working.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
user3407267
  • 1,524
  • 9
  • 30
  • 57
  • The way I read that error message it's trying to cast the LinkedHashMap into the value. So you might actually have a Map ... (left as an exercise for the reader to determine the types of the LinkedHashMap parameter - e.g. is it Map> or something else? – Rick Aug 22 '18 at 04:51
  • @Yeah. the json is Map which i am trying to convert to Map – user3407267 Aug 22 '18 at 04:53
  • How are you calling `getResponse`? What is the value you pass to `responseClass`? – Thiyagu Aug 22 '18 at 04:57
  • Possible duplicate of [Jackson - Deserialize using generic class](https://stackoverflow.com/questions/11664894/jackson-deserialize-using-generic-class) – Alex Taylor Aug 22 '18 at 04:59
  • This json: { A: { isAvailable : true} , VV: { isAvailable : false} , B45: { isAvailable : null} } should return you Map. What's the error you are getting with this? – S.K. Aug 22 '18 at 05:06
  • @S.K. It saying java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to AvailableInfo – user3407267 Aug 22 '18 at 05:08
  • Hope you are calling getResponse(query, Map.class) with Map.class only. Can you point the line where error is seen? – S.K. Aug 22 '18 at 05:10

3 Answers3

1

try to change your method signature to be

private <T> T getResponse(final RestURI query, final TypeReference typeReference) throws IOException {
    T response = null;

    final RestResponse<Record> tempResponse = client.call(GET, query);

    if (isResponseOK(tempResponse, query)) {
        byte[] payloads = {};

        for (Record rec : tempResponse.getResponses()) {
            final byte[] payload = rec.getPayload();
            payloads = ArrayUtils.addAll(payloads, payload);
        }
        final Reader reader = newReader(payloads)
        response = jsonObjectMapper.readValue(reader, typeReference);
    }

    return response; //returns Map<String, AvailableInfo>
}

and call it in the following manner

TypeReference typeReference  = new TypeReference<Map<String, AvailableInfo>>() {
    };
Map<String, AvailableInfo> map = (Map<String, AvailableInfo>) getResponse(typeReference);

using the following json example

 {
        A:  { isAvailable : true} ,
        VV:  { isAvailable : false} ,
        B45:  { isAvailable : null} 
}

i tried it and worked for me

Mohamed Fouad
  • 111
  • 1
  • 2
  • 10
0

First convert it using below line and then you can play with the data:

List<? extends Map<String, String>> resultList = new ArrayList<LinkedHashMap<String, String>>();
Aditya
  • 818
  • 1
  • 10
  • 21
0

To convert the json into Map, the json has to be in the form of :

{
        A:  { isAvailable : true} ,
        VV:  { isAvailable : false} ,
        B45:  { isAvailable : null} 
}

After that following code returns Map<String, AvailableInfo> :

Map<String, AvailableInfo> readValue = mapper.readValue(json, Map.class);

Output of System.out.println(readValue);:

{A={isAvailable=true}, VV={isAvailable=false}, B45={isAvailable=null}}
S.K.
  • 3,597
  • 2
  • 16
  • 31