private <K, V> void writeMap(Map<K, V> map) throws IOException {
    out.write("{");
    if (prettyPrint) {
      ++indentLevel;
      writeNewline();
    }
    Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
    if (iterator.hasNext()) {
      while(true) {
        Map.Entry<K, V> entry = iterator.next();
        writeString(entry.getKey());
        out.write(":");
        if (prettyPrint) {
          out.write(" ");
        }
        write(entry.getValue());
        if (!iterator.hasNext()) break;
        out.write(",");
        if (prettyPrint) {
          writeNewline();
        }
      }
    }
    if (prettyPrint) {
      --indentLevel;
      writeNewline();
    }
    out.write("}");
  }
Note: The {@link #writeString} and {@link #hex} methods
are based on code from class {@code StringEscapeUtils} in Commons Lang 2.6.
spock-core/src/main/java/org/spockframework/util/JsonWriter.java