Example usage for org.apache.commons.lang3 StringEscapeUtils escapeJson

List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeJson

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringEscapeUtils escapeJson.

Prototype

public static final String escapeJson(final String input) 

Source Link

Document

Escapes the characters in a String using Json String rules.

Escapes any values it finds into their Json String form.

Usage

From source file:com.crushpaper.JsonBuilder.java

/** Returns the value JSON encoded and quoted. */
static String quote(String value) {
    if (value == null) {
        return "null";
    }//w w  w  .java 2s  .  c  o m

    return "\"" + StringEscapeUtils.escapeJson(value) + "\"";
}

From source file:com.twosigma.beaker.shared.module.util.ControlCharacterUtils.java

public static String escapeControlCharacters(final String value) {
    if (StringUtils.isNotEmpty(value)) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < value.length(); i++) {
            if (Character.isISOControl(value.charAt(i))) {
                sb.append(StringEscapeUtils.escapeJson(value.substring(i, i + 1)));
            } else {
                sb.append(value.charAt(i));
            }/*  w  w w .  j a v a 2 s. c o  m*/
        }
        return sb.toString();
    }
    return StringUtils.EMPTY;
}

From source file:de.hska.ld.core.util.EscapeUtil.java

public static String escapeJsonForLogging(String unescapedJson) {
    String escapedString = StringEscapeUtils.escapeJson(unescapedJson);
    return "\"" + escapedString + "\"";
}

From source file:com.mirth.connect.util.MirthJsonUtil.java

public static String escape(String input) {
    return StringEscapeUtils.escapeJson(input);
}

From source file:com.igormaznitsa.jcp.expression.functions.FunctionSTR2JSON.java

@Override
@Nonnull//www .  ja va2s .c  o m
public Value executeStr(@Nonnull final PreprocessorContext context, @Nonnull final Value value) {
    final String escaped = StringEscapeUtils.escapeJson(value.asString());
    return Value.valueOf(escaped);
}

From source file:com.clxcommunications.xms.api.ApiErrorTest.java

@Property
public void canSerializeJson(String code, String text) throws Exception {
    ApiError input = ApiError.of(code, text);

    String expected = Utils.join("\n", "{", "  \"code\" : \"" + StringEscapeUtils.escapeJson(code) + "\",",
            "  \"text\" : \"" + StringEscapeUtils.escapeJson(text) + "\"", "}");

    String actual = json.writeValueAsString(input);

    assertThat(actual, is(TestUtils.jsonEqualTo(expected)));
}

From source file:com.hurence.logisland.processor.hbase.io.JsonRowSerializer.java

@Override
public String serialize(byte[] rowKey, ResultCell[] cells) {
    final StringBuilder jsonBuilder = new StringBuilder();
    jsonBuilder.append("{");

    final String row = new String(rowKey, charset);
    jsonBuilder.append("\"row\":").append("\"").append(StringEscapeUtils.escapeJson(row)).append("\"");

    jsonBuilder.append(", \"cells\": {");
    int i = 0;/*from  ww  w .j  a v a  2s .  c o  m*/
    for (final ResultCell cell : cells) {
        final String cellFamily = new String(cell.getFamilyArray(), cell.getFamilyOffset(),
                cell.getFamilyLength(), charset);
        final String cellQualifier = new String(cell.getQualifierArray(), cell.getQualifierOffset(),
                cell.getQualifierLength(), charset);

        if (i > 0) {
            jsonBuilder.append(", ");
        }
        jsonBuilder.append("\"").append(StringEscapeUtils.escapeJson(cellFamily)).append(":")
                .append(StringEscapeUtils.escapeJson(cellQualifier)).append("\":\"")
                .append(StringEscapeUtils.escapeJson(new String(cell.getValueArray(), cell.getValueOffset(),
                        cell.getValueLength(), charset)))
                .append("\"");
        i++;
    }

    jsonBuilder.append("}}");
    return jsonBuilder.toString();
}

From source file:com.clxcommunications.xms.api.TagsUpdateTest.java

@Property
public void canSerializeJson(List<String> toAdd, List<String> toRemove) throws Exception {
    TagsUpdate input = ClxApi.tagsUpdate().addAllTagInsertions(toAdd).addAllTagRemovals(toRemove).build();

    List<String> escapedToAdd = new ArrayList<String>();
    for (String tag : toAdd) {
        escapedToAdd.add("\"" + StringEscapeUtils.escapeJson(tag) + "\"");
    }/*from   ww w  . j  a va 2  s . c  o  m*/

    List<String> escapedToRemove = new ArrayList<String>();
    for (String tag : toRemove) {
        escapedToRemove.add("\"" + StringEscapeUtils.escapeJson(tag) + "\"");
    }

    String expected = Utils.join("\n", "{", "  \"add\" : [" + Utils.join(",", escapedToAdd) + "],",
            "  \"remove\" : [" + Utils.join(",", escapedToRemove) + "]", "}");

    String actual = json.writeValueAsString(input);

    assertThat(actual, is(TestUtils.jsonEqualTo(expected)));
}

From source file:com.clxcommunications.xms.api.TagsTest.java

@Property
public void canSerializeJson(List<String> tags) throws Exception {
    Tags input = TagsImpl.builder().tags(tags).build();

    List<String> escapedTags = new ArrayList<String>();
    for (String tag : tags) {
        escapedTags.add("\"" + StringEscapeUtils.escapeJson(tag) + "\"");
    }//from   www.j  a v  a  2s.c om

    String expected = Utils.join("\n", "{", "  \"tags\" : [" + Utils.join(",", escapedTags) + "]", "}");

    String actual = json.writeValueAsString(input);

    assertThat(actual, is(TestUtils.jsonEqualTo(expected)));
}

From source file:com.wegas.log.neo4j.Neo4jUtils.java

/**
 * Sends a given query to the neo4j database and gets a response. The
 * response is received as a string containing JSON format data.
 *
 * @param query the query to be submitted
 * @return a string containing the query's answer
 *//*from ww w  . jav a2s.c  o  m*/
protected static String queryDBString(String query) {
    final String qURL = NEO4J_SERVER_URL + "transaction/commit";
    String entity = "{ \"statements\" : [ { \"statement\" : \"" + StringEscapeUtils.escapeJson(query)
            + "\" } ] }";
    String result = null;
    Response response = getBuilder(qURL).post(Entity.json(entity));
    if (checkValidHttpResponse(response.getStatus())) {
        result = response.readEntity(String.class);
    }
    response.close();
    return result;
}