StringUtils { @SuppressWarnings("ConstantConditions") @Nonnull public static String encodeToJsonString(@Nonnull final String input) { assert input != null; final int len = input.length(); final StringBuilder sb = new StringBuilder(len + 2); sb.append('"'); for (int i = 0; i < len; ++i) { final char ch = input.charAt(i); switch (ch) { case '\\': case '"': case '/': sb.append('\\'); sb.append(ch); break; case '\b': sb.append("\\b"); break; case '\t': sb.append("\\t"); break; case '\n': sb.append("\\n"); break; case '\f': sb.append("\\f"); break; case '\r': sb.append("\\r"); break; default: if (((ch >= '\u0000') && (ch <= '\u001F')) || ((ch >= '\u007F') && (ch <= '\u009F')) || (((ch >= '\u2000') && (ch <= '\u20FF')))) { sb.append("\\u").append(Integer.toHexString(((int) ch) + 0x10000).substring(1)); } else { sb.append(ch); } break; } } sb.append('"'); return sb.toString(); } private  StringUtils(); @Nonnull static String nullToEmpty(@Nullable final String s); @Nullable static String emptyToNull(@Nullable final String s); @Nullable static String trim(@Nullable final String s); @Nullable static List<String> trim(@Nullable final List<String> sList); @Nullable static Set<String> trim(@Nullable final Set<String> sSet); @Nullable static String literalToLowerCase(@Nullable final String s); @Nonnull static int[] toCodePoints(@Nonnull final String s); @Nonnull static String mkString(@Nonnull final String separator, @Nonnull final Collection<?> values); @Nonnull static String mkRevString(@Nonnull final String separator, @Nonnull final Collection<T> values); @SafeVarargs @Nonnull static String mkString(@Nonnull final String separator, @Nonnull final T... values); @SafeVarargs @Nonnull static String mkRevString(@Nonnull final String separator, @Nonnull final T... values); @Nonnull static String generateRandomStringFromChars(@Nonnull final char[] chars, final int length); @SuppressWarnings("ConstantConditions") @Nonnull static String encodeToJsonString(@Nonnull final String input); @Nullable static String decodeFromJsonString(@Nonnull final String input);  }
@Test public void testEncodeToJsonString() { LOG.info("testEncodeToJsonString"); String json = "\"\""; String java = ""; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\""; java = "a"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"ab\""; java = "ab"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\\"a\""; java = "\"a"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\\\"\""; java = "a\""; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\\\"b\""; java = "a\"b"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\\\\""; java = "\\"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\\\a\""; java = "\\a"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\\\\\""; java = "a\\"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\\\\b\""; java = "a\\b"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\\\\\\\a\""; java = "\\\\a"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\\\\\\\\\""; java = "a\\\\"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"a\\\\\\\\b\""; java = "a\\\\b"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\\"\""; java = "\""; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\b\""; java = "\b"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\t\""; java = "\t"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\n\""; java = "\n"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\f\""; java = "\f"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); json = "\"\\r\""; java = "\r"; Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); final String controlChars = "\b\t\n\f\r"; for (int i = 0; i < 32; ++i) { final char ch = (char) i; final String code = ("000" + Integer.toHexString(i)); if (controlChars.indexOf(ch) == -1) { json = "\"\\u" + code.substring(code.length() - 4) + '"'; java = String.valueOf(ch); Assert.assertEquals(json, StringUtils.encodeToJsonString(java)); Assert.assertEquals(java, StringUtils.decodeFromJsonString(json)); } } Assert.assertNull(StringUtils.decodeFromJsonString("")); Assert.assertNull(StringUtils.decodeFromJsonString("\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"x")); Assert.assertNull(StringUtils.decodeFromJsonString("x\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"\\\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"\\u\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"\\u0\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"\\u00\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"\\u000\"")); Assert.assertNull(StringUtils.decodeFromJsonString("\"\\u000X\"")); }