unchecked call to add(E) as a member of the raw type ArrayList and HashMap

ant2009 picture ant2009 · Jul 3, 2014 · Viewed 13.5k times · Source

I have the following function that uses org.json.simple to create JSON object.

public JSONObject createJSONRequest() {
    // /* Create JSON Objects */
    JSONObject jsonObject = new JSONObject();
    Map<String, String> map = new HashMap<String, String>();

    map.put(ACCESS_TOKEN_KEY, mAccessToken);            
    map.put(SESSION_ID_KEY, mSessionId);
    map.put(SNAPZ_ID_KEY, mSnapzId);
    map.put(EMAIL_KEY, mEmail);
    map.put(EMAIL_PWD_KEY, mEmailPwd);

    JSONArray list = new JSONArray();
    list.add(map);
    jsonObject.put("parameters", list);
    jsonObject.put("function", "verifyEmail");

    return jsonObject;
}

However, I keep getting this warning when I use lint checker.

[unchecked] unchecked call to add(E) as a member of the raw type ArrayList
        list.add(map);
                ^
where E is a type-variable: E extends Object declared in class ArrayList

 warning: [unchecked] unchecked call to put(K,V) as a member of the raw type HashMap
        jsonObject.put("parameters", list);
                      ^
where K,V are type-variables:
    K extends Object declared in class HashMap
    V extends Object declared in class HashMap

warning: [unchecked] unchecked call to put(K,V) as a member of the raw type HashMap
        jsonObject.put("function", "verifyEmail");

I have tried to use the generic type. HashMap uses generics, but the other objects JSONObject JSONArray don't.

Many thanks for any suggestions,

Answer

bitkot picture bitkot · Jul 3, 2014

You are getting this warning because the library uses raw type collections internally. To hide this warning you annotate your method with @SuppressWarnings("unchecked").

If you want to use json library with generics support. You can go for Google GSOn. I have used it myself in many projects. It is easy and uses generic collections.