@UseDataProvider("createJsonStringData") @Test public void createsJsonString(String sourceString, String expectedJsonString) { assertEquals(expectedJsonString, JsonUtils.getJsonString(sourceString)); }
public static String getJsonString(String string) { if (string == null || string.length() == 0) { return "\"\""; } StringBuilder jsonString = new StringBuilder(); jsonString.append("\""); char[] charArray = string.toCharArray(); for (char c : charArray) { switch (c) { case '\n': jsonString.append("\\n"); break; case '\r': jsonString.append("\\r"); break; case '\t': jsonString.append("\\t"); break; case '\b': jsonString.append("\\b"); break; case '\f': jsonString.append("\\f"); break; case '\\': jsonString.append("\\\\"); break; case '"': jsonString.append("\\\""); break; default: if (c < '\u0010') { jsonString.append("\\u000").append(Integer.toHexString(c)); } else if ((c < '\u0020' && c > '\u0009') || (c >= '\u0080' && c < '\u00a0')) { jsonString.append("\\u00").append(Integer.toHexString(c)); } else if (c >= '\u2000' && c < '\u2100') { jsonString.append("\\u").append(Integer.toHexString(c)); } else { jsonString.append(c); } break; } } jsonString.append("\""); return jsonString.toString(); }
JsonUtils { public static String getJsonString(String string) { if (string == null || string.length() == 0) { return "\"\""; } StringBuilder jsonString = new StringBuilder(); jsonString.append("\""); char[] charArray = string.toCharArray(); for (char c : charArray) { switch (c) { case '\n': jsonString.append("\\n"); break; case '\r': jsonString.append("\\r"); break; case '\t': jsonString.append("\\t"); break; case '\b': jsonString.append("\\b"); break; case '\f': jsonString.append("\\f"); break; case '\\': jsonString.append("\\\\"); break; case '"': jsonString.append("\\\""); break; default: if (c < '\u0010') { jsonString.append("\\u000").append(Integer.toHexString(c)); } else if ((c < '\u0020' && c > '\u0009') || (c >= '\u0080' && c < '\u00a0')) { jsonString.append("\\u00").append(Integer.toHexString(c)); } else if (c >= '\u2000' && c < '\u2100') { jsonString.append("\\u").append(Integer.toHexString(c)); } else { jsonString.append(c); } break; } } jsonString.append("\""); return jsonString.toString(); } }
JsonUtils { public static String getJsonString(String string) { if (string == null || string.length() == 0) { return "\"\""; } StringBuilder jsonString = new StringBuilder(); jsonString.append("\""); char[] charArray = string.toCharArray(); for (char c : charArray) { switch (c) { case '\n': jsonString.append("\\n"); break; case '\r': jsonString.append("\\r"); break; case '\t': jsonString.append("\\t"); break; case '\b': jsonString.append("\\b"); break; case '\f': jsonString.append("\\f"); break; case '\\': jsonString.append("\\\\"); break; case '"': jsonString.append("\\\""); break; default: if (c < '\u0010') { jsonString.append("\\u000").append(Integer.toHexString(c)); } else if ((c < '\u0020' && c > '\u0009') || (c >= '\u0080' && c < '\u00a0')) { jsonString.append("\\u00").append(Integer.toHexString(c)); } else if (c >= '\u2000' && c < '\u2100') { jsonString.append("\\u").append(Integer.toHexString(c)); } else { jsonString.append(c); } break; } } jsonString.append("\""); return jsonString.toString(); } private  JsonUtils(); }
JsonUtils { public static String getJsonString(String string) { if (string == null || string.length() == 0) { return "\"\""; } StringBuilder jsonString = new StringBuilder(); jsonString.append("\""); char[] charArray = string.toCharArray(); for (char c : charArray) { switch (c) { case '\n': jsonString.append("\\n"); break; case '\r': jsonString.append("\\r"); break; case '\t': jsonString.append("\\t"); break; case '\b': jsonString.append("\\b"); break; case '\f': jsonString.append("\\f"); break; case '\\': jsonString.append("\\\\"); break; case '"': jsonString.append("\\\""); break; default: if (c < '\u0010') { jsonString.append("\\u000").append(Integer.toHexString(c)); } else if ((c < '\u0020' && c > '\u0009') || (c >= '\u0080' && c < '\u00a0')) { jsonString.append("\\u00").append(Integer.toHexString(c)); } else if (c >= '\u2000' && c < '\u2100') { jsonString.append("\\u").append(Integer.toHexString(c)); } else { jsonString.append(c); } break; } } jsonString.append("\""); return jsonString.toString(); } private  JsonUtils(); static String getJsonString(String string); static boolean isKnownType(Class<?> clazz); static Types getType(Object o); static Types getType(Class<?> clazz); static Set<String> getTransientFields(Class<?> aClass); @SuppressWarnings({"unchecked"}) static T createProxy(Class<T> anInterface); }
JsonUtils { public static String getJsonString(String string) { if (string == null || string.length() == 0) { return "\"\""; } StringBuilder jsonString = new StringBuilder(); jsonString.append("\""); char[] charArray = string.toCharArray(); for (char c : charArray) { switch (c) { case '\n': jsonString.append("\\n"); break; case '\r': jsonString.append("\\r"); break; case '\t': jsonString.append("\\t"); break; case '\b': jsonString.append("\\b"); break; case '\f': jsonString.append("\\f"); break; case '\\': jsonString.append("\\\\"); break; case '"': jsonString.append("\\\""); break; default: if (c < '\u0010') { jsonString.append("\\u000").append(Integer.toHexString(c)); } else if ((c < '\u0020' && c > '\u0009') || (c >= '\u0080' && c < '\u00a0')) { jsonString.append("\\u00").append(Integer.toHexString(c)); } else if (c >= '\u2000' && c < '\u2100') { jsonString.append("\\u").append(Integer.toHexString(c)); } else { jsonString.append(c); } break; } } jsonString.append("\""); return jsonString.toString(); } private  JsonUtils(); static String getJsonString(String string); static boolean isKnownType(Class<?> clazz); static Types getType(Object o); static Types getType(Class<?> clazz); static Set<String> getTransientFields(Class<?> aClass); @SuppressWarnings({"unchecked"}) static T createProxy(Class<T> anInterface);  }