Example usage for org.apache.commons.text StringEscapeUtils unescapeJson

List of usage examples for org.apache.commons.text StringEscapeUtils unescapeJson

Introduction

In this page you can find the example usage for org.apache.commons.text StringEscapeUtils unescapeJson.

Prototype

public static final String unescapeJson(final String input) 

Source Link

Document

Unescapes any Json literals found in the String .

For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\' .

Usage

From source file:net.metanotion.json.JsonPath.java

private static Lexeme lexString(final String cs, final int[] position) {
    final StringBuilder sb = new StringBuilder();
    boolean escape = false;
    while (true) {
        final int c = read(cs, position);
        if (c == -1) {
            throw new IllegalArgumentException("unexpected end of expression in string");
        }//  www  .  j  a  v  a 2s.  c o m
        final String cbuf = new String(Character.toChars(c));
        if (("\'".equals(cbuf)) && !escape) {
            return new Lexeme(Token.STRING, StringEscapeUtils.unescapeJson(sb.toString()));
        } else if ("\\".equals(cbuf)) {
            escape = true;
            sb.append(cbuf);
        } else {
            escape = false;
            sb.append(cbuf);
        }
    }
}

From source file:net.metanotion.json.StreamingParser.java

private Lexeme lexString(final Reader in) throws IOException {
    final StringBuilder sb = new StringBuilder();
    boolean escape = false;
    while (true) {
        final int c = in.read();
        if (c == -1) {
            throw new ParserException("Expected a character instead of end of stream");
        }/*  www .  j  a  va 2s.  c o m*/
        final String cbuf = new String(Character.toChars(c));
        if (("\"".equals(cbuf)) && !escape) {
            return new Lexeme(Token.STRING, StringEscapeUtils.unescapeJson(sb.toString()));
        } else if ("\\".equals(cbuf)) {
            escape = true;
            sb.append(cbuf);
        } else {
            escape = false;
            sb.append(cbuf);
        }
    }
}