I am using org.eclipse.jetty.util.ajax.JSON to parse JSON text. But the JSON.parse(string) method produces an Object and I need it as a Map. Internally it is an object of exactly the mentioned class. But how do you cast an Object into a Map without constructing a new one or getting the unchecked cast warning?

Currently, I have found only one solution without the unchecked cast warning, but with constructing a new Map, which is actually of course not a casting at all.

private Map<String,Object> getMap(String string) {
    Object object = JSON.parse(string);
    if (object instanceof Map) {
        Map<?,?> map = (Map)(object);
            String key = entry.getKey().toString();
            Object value = entry.getValue();
    return result;

So whether is there a way to properly cast it without unchecked cast warnings?

  • 1
    type-checking before type-casting might get rid of the warning. Oct 17 '12 at 5:04
  • 1
    @JanDvorak type-checking before doesn't help
    – Bohemian
    Oct 17 '12 at 5:08
  • @JanDvorak - Because of type erasure, you can't type check for a particular type of generic map. (For instance, you can't distinguish Map<String,Object> from Map<Integer,Object> using type checking.)
    – Ted Hopp
    Oct 17 '12 at 5:11
  • I have tried (object instanceof Map<String,Object>) and it doesn't work, while (object instanceof Map) does. Oct 17 '12 at 5:14
  • @TedHopp you can at least check it's a Map. You'll still have a raw types warning. Oct 17 '12 at 5:15

The compiler can't guarantee that the cast is safe. Since you are the one making the guarantee, you should use @SuppressWarnings("unchecked")


As @TedHopp points out, the way that library is supposed to be used is that you cast each value in the returnd Object to the type you know it is (but you would have to cast every property you retrieve) See the mappings here http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/util/ajax/JSON.html

The point that it brings out, is that you are guaranteeing that this JSON object only contains other JSON objects (map to objects)

Therefore, if for some reason you're passed the input

// properties are not quoted for readability
{ a: 2, b : {c:3} }

Your code would fail with an invalid cast exception when you try


So remember you're the one guaranteeing what goes into that string you're parsing into JSON

If you can't guarantee it, you can't create this getMap function you would like. You have to do the casting (and @SupressWarnings) at the place that knows what type a specific object is.

For some type safety when working with JSON, you should learn about

Those classes allow you to read JSON directly into Java classes

  • 1
    Also see this thread for what is, essentially, the same answer. Instead of suppressing warnings, you can use a non-generic Map and cast the return value from any get call.
    – Ted Hopp
    Oct 17 '12 at 5:09
  • But actually there might be cases when I can not guarantee the unchecked cast. The cast works only when the input string is within {}, while in other cases it might be something else than a Map. Oct 17 '12 at 5:18
  • @KoichiSenada you can only use a your getMap function if you can guarantee that the input is in the format {a:{...}, b: {...}}, that is all, the properties at the top level are JSON objects Oct 17 '12 at 5:30
  • @TedHopp, how do you use a non-generic Map without suppressing warnings? Oct 17 '12 at 5:36
  • @KoichiSenada You are asking the same question that everybody has already told you there's no way to do. See my explanation and the edits in my answer. Oct 17 '12 at 5:39

Your Answer

