Make Gson throw exception on parsing JSON with duplicated key

You can try this way:

String json = "{\"a\":2, \"a\":3}";
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> map = gson.fromJson(json, mapType);

And if json is more complex than JsonObject can be used as map value type:

Type mapType = new TypeToken<Map<String, JsonObject>>() {}.getType();

1) You may edit the source of gson a little bit. This is just a suggestion to understand how things work. I don't advice you to use this on a real/production environment.

Gson uses com.google.gson.internal.LinkedTreeMap while parsing a json string to a JsonObject. For testing issues you can copy that class into your project with the same name and package name. And edit its put method to not allow duplicate keys.

    @Override
    public V put(K key, V value) {
    if (key == null) {
      throw new NullPointerException("key == null");
    }

    // my edit here
    if(find(key, false) != null) {
        throw new IllegalArgumentException("'" + key.toString() + "' is duplicate key for json!");
    }

    Node<K, V> created = find(key, true);
    V result = created.value;
    created.value = value;
    return result;
  }

2) Another clean solution is to define custom classes which are going to map to your json strings. Then write their custom TypeAdapters

3) Do it by using a Deserializer? I don't think it is possible. If you try to use it you'll see that you already have a jsonObject there which your duplicate keys are handled as one.