@Test
public void testEncodeToJsonString() {

    LOG.info("testEncodeToJsonString");
    String json = "\"\"";       // JSON string.
    String java = "";           // Java string.
    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) {
        //noinspection NumericCastThatLosesPrecision
        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));
        }
    }
    // Failing decoder test.
    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\""));
}