@Test
public void testEscapeBadIdentifierCodePoints() throws ScriptException, NoSuchMethodException
{

    Map<Object,Object> jsonObj = new LinkedHashMap<>();
    StringBuilder buf = new StringBuilder("c");
    StringBuilder cmpBuf = new StringBuilder();
    JSONConfig cfg = new JSONConfig().setEscapeBadIdentifierCodePoints(true);
    jsonObj.put("x\u0005", 0);
    String json = JSONUtil.toJSON(jsonObj, cfg);
    validateJSON(json);
    assertThat(json, is("{\"x\\u0005\":0}"));
    // test octal/hex unescape.
    for ( int i = 0; i < 256; i++ ){
        buf.setLength(0);
        buf.append("c").append((char)i);
        jsonObj.clear();
        jsonObj.put(String.format("a\\%o", i), 0);
        jsonObj.put(String.format("b\\x%02X", i), 0);
        jsonObj.put(buf, 0);                            // raw.
        json = JSONUtil.toJSON(jsonObj, cfg);
        validateJSON(json);
        String r = JSONUtil.isValidIdentifierPart(i, cfg) ? String.format("%c", (char)i) : String.format("\\u%04X", i);
        assertThat(json, is("{\"a"+r+"\":0,\"b"+r+"\":0,\"c"+r+"\":0}"));
    }
    int maxLen = 512;
    buf.setLength(0);
    buf.append("c");
    cmpBuf.setLength(0);
    cmpBuf.append("{\"c");
    for ( int i = 256, ct = 0; i <= Character.MAX_CODE_POINT; i++ ){
        if ( isNormalCodePoint(i) ){
            buf.appendCodePoint(i);
            addCmp(i, cmpBuf, cfg);
            ++ct;
        }
        if ( ct % maxLen == 0 || i == Character.MAX_CODE_POINT ){
            jsonObj.clear();
            jsonObj.put(buf, 0);                            // raw.
            json = JSONUtil.toJSON(jsonObj, cfg);
            validateJSON(json);
            cmpBuf.append("\":0}");
            assertThat(json, is(cmpBuf.toString()));
            buf.setLength(0);
            buf.append("c");
            cmpBuf.setLength(0);
            cmpBuf.append("{\"c");
        }
    }
    cfg.setFullJSONIdentifierCodePoints(true);
    // test octal/hex unescape.
    for ( int i = 0; i < 256; i++ ){
        buf.setLength(0);
        buf.append("c").append((char)i);
        jsonObj.clear();
        jsonObj.put(String.format("a\\%o", i), 0);
        jsonObj.put(String.format("b\\x%02X", i), 0);
        jsonObj.put(buf, 0);                            // raw.
        json = JSONUtil.toJSON(jsonObj, cfg);
        validateJSON(json);
        String r = StringProcessor.getEscape((char)i);
        if ( r == null ){
            r = JSONUtil.isValidIdentifierPart(i, cfg) ? String.format("%c", (char)i) : String.format("\\u%04X", i);
        }
        assertThat(json, is("{\"a"+r+"\":0,\"b"+r+"\":0,\"c"+r+"\":0}"));
    }
    buf.setLength(0);
    buf.append("c");
    for ( int i = 256, ct = 0; i <= Character.MAX_CODE_POINT; i++ ){
        if ( isNormalCodePoint(i) ){
            buf.appendCodePoint(i);
            addCmp(i, cmpBuf, cfg);
            ++ct;
        }
        if ( ct % maxLen == 0 || i == Character.MAX_CODE_POINT ){
            jsonObj.clear();
            jsonObj.put(buf, 0);
            json = JSONUtil.toJSON(jsonObj, cfg);
            parseJSON(json);
            cmpBuf.append("\":0}");
            assertThat(json, is(cmpBuf.toString()));
            buf.setLength(0);
            buf.append("c");
            cmpBuf.setLength(0);
            cmpBuf.append("{\"c");
        }
    }
}