JSON { public static String encode(Map<String, ?> map) { StringBuilder b = new StringBuilder("{"); for (Map.Entry<String,?> entry : map.entrySet()) { b.append("\"").append(escape(entry.getKey())).append("\":"); if (entry.getValue() instanceof String) b.append("\"").append(escape(entry.getValue().toString())).append("\""); else b.append(entry.getValue()); b.append(","); } if (b.length()>1) b.setLength(b.length()-1); b.append("}"); return b.toString(); } private  JSON(); static String encode(Map<String, ?> map); static String escape(String s); static boolean equals(String left, String right);  }
@Test public void testMapToString() { Map<String,Object> map = new LinkedHashMap<>(); map.put("a \"key\"", 3); map.put("key2", "value"); map.put("key3", 3.3); assertEquals("{\"a \\\"key\\\"\":3,\"key2\":\"value\",\"key3\":3.3}", JSON.encode(map)); }