public Map<K, V> read(Unpacker u, Map<K, V> to, boolean required)
            throws IOException {
        if (!required && u.trySkipNil()) {
            return null;
        }
        int n = u.readMapBegin();
        Map<K, V> map;
        if (to != null) {
            map = (Map<K, V>) to;
            map.clear();
        } else {
            map = new HashMap<K, V>(n);
        }
        for (int i = 0; i < n; i++) {
            K key = keyTemplate.read(u, null);
            V value = valueTemplate.read(u, null);
            map.put(key, value);
        }
        u.readMapEnd();
        return map;
    }