@Test public void testToJsonStr() { Assert.assertEquals("dal\\\"?[]}das\\\\aa\\n33\\r24\\tkkh\\r86gg\\f11\\b", JsonUtil.toJsonStr("dal\"?[]}das\\aa\n33\r24\tkkh\r86gg\f11\b")); }
public static String toJsonStr(String value) { if (value == null) return null; StringBuilder buf = new StringBuilder(value.length()); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch(c) { case '"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; case '\n': buf.append("\\n"); break; case '\r': buf.append("\\r"); break; case '\t': buf.append("\\t"); break; case '\f': buf.append("\\f"); break; case '\b': buf.append("\\b"); break; default: if (c < 32 || c == 127) { buf.append(" "); } else { buf.append(c); } } } return buf.toString(); }
JsonUtil { public static String toJsonStr(String value) { if (value == null) return null; StringBuilder buf = new StringBuilder(value.length()); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch(c) { case '"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; case '\n': buf.append("\\n"); break; case '\r': buf.append("\\r"); break; case '\t': buf.append("\\t"); break; case '\f': buf.append("\\f"); break; case '\b': buf.append("\\b"); break; default: if (c < 32 || c == 127) { buf.append(" "); } else { buf.append(c); } } } return buf.toString(); } }
JsonUtil { public static String toJsonStr(String value) { if (value == null) return null; StringBuilder buf = new StringBuilder(value.length()); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch(c) { case '"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; case '\n': buf.append("\\n"); break; case '\r': buf.append("\\r"); break; case '\t': buf.append("\\t"); break; case '\f': buf.append("\\f"); break; case '\b': buf.append("\\b"); break; default: if (c < 32 || c == 127) { buf.append(" "); } else { buf.append(c); } } } return buf.toString(); }  }
JsonUtil { public static String toJsonStr(String value) { if (value == null) return null; StringBuilder buf = new StringBuilder(value.length()); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch(c) { case '"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; case '\n': buf.append("\\n"); break; case '\r': buf.append("\\r"); break; case '\t': buf.append("\\t"); break; case '\f': buf.append("\\f"); break; case '\b': buf.append("\\b"); break; default: if (c < 32 || c == 127) { buf.append(" "); } else { buf.append(c); } } } return buf.toString(); }  static String toJsonStr(String value); static boolean isValidJsonObject(String json); static boolean isValidJsonObject(String json, boolean allowBlank); static boolean isValidJsonArray(String json); static boolean isValidJsonArray(String json, boolean allowBlank); static String toJson(long[] ids); static String toJson(Jsonable[] values); }
JsonUtil { public static String toJsonStr(String value) { if (value == null) return null; StringBuilder buf = new StringBuilder(value.length()); for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch(c) { case '"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; case '\n': buf.append("\\n"); break; case '\r': buf.append("\\r"); break; case '\t': buf.append("\\t"); break; case '\f': buf.append("\\f"); break; case '\b': buf.append("\\b"); break; default: if (c < 32 || c == 127) { buf.append(" "); } else { buf.append(c); } } } return buf.toString(); }  static String toJsonStr(String value); static boolean isValidJsonObject(String json); static boolean isValidJsonObject(String json, boolean allowBlank); static boolean isValidJsonArray(String json); static boolean isValidJsonArray(String json, boolean allowBlank); static String toJson(long[] ids); static String toJson(Jsonable[] values);  }