org.apache.commons.lang3.StringEscapeUtils.escapeJson()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(318)

本文整理了Java中org.apache.commons.lang3.StringEscapeUtils.escapeJson()方法的一些代码示例,展示了StringEscapeUtils.escapeJson()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringEscapeUtils.escapeJson()方法的具体详情如下:
包路径:org.apache.commons.lang3.StringEscapeUtils
类名称:StringEscapeUtils
方法名:escapeJson

StringEscapeUtils.escapeJson介绍

[英]Escapes the characters in a String using Json String rules.

Escapes any values it finds into their Json String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\' and 't'.

The only difference between Java strings and Json strings is that in Json, forward-slash (/) is escaped.

See http://www.ietf.org/rfc/rfc4627.txt for further details.

Example:

input string: He didn't say, "Stop!" 
output string: He didn't say, \"Stop!\"

[中]使用Json字符串规则转义字符串中的字符。
将找到的任何值转义为Json字符串形式。正确处理引号和控制字符(制表符、反斜杠、cr、ff等)
所以一个标签变成了字符“\”和“t”。
Java字符串和Json字符串之间的唯一区别是,在Json中,正斜杠(/)被转义。
看见http://www.ietf.org/rfc/rfc4627.txt了解更多细节。
例子:

input string: He didn't say, "Stop!" 
output string: He didn't say, \"Stop!\"

代码示例

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Appends the given String enclosed in double-quotes to the given StringBuffer.
 *
 * @param buffer the StringBuffer to append the value to.
 * @param value the value to append.
 */
private void appendValueAsString(final StringBuffer buffer, final String value) {
  buffer.append('"').append(StringEscapeUtils.escapeJson(value)).append('"');
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Override
protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
  if (fieldName == null) {
    throw new UnsupportedOperationException(
        "Field names are mandatory when using JsonToStringStyle");
  }
  super.appendFieldStart(buffer, FIELD_NAME_QUOTE + StringEscapeUtils.escapeJson(fieldName)
      + FIELD_NAME_QUOTE);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEscapeJson() {
  assertNull(StringEscapeUtils.escapeJson(null));
  try {
    StringEscapeUtils.ESCAPE_JSON.translate(null, null);
    fail();
  } catch (final IOException ex) {
    fail();
  } catch (final IllegalArgumentException ex) {
  }
  try {
    StringEscapeUtils.ESCAPE_JSON.translate("", null);
    fail();
  } catch (final IOException ex) {
    fail();
  } catch (final IllegalArgumentException ex) {
  }
  assertEquals("He didn't say, \\\"stop!\\\"", StringEscapeUtils.escapeJson("He didn't say, \"stop!\""));
  final String expected = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
  final String input ="\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
  assertEquals(expected, StringEscapeUtils.escapeJson(input));
}

代码示例来源:origin: org.eclipse.dirigible/dirigible-api-facade-utils

/**
 * Escape JSON.
 *
 * @param input
 *            the input
 * @return the escaped input
 */
public static final String escapeJson(String input) {
  return StringEscapeUtils.escapeJson(input);
}

代码示例来源:origin: future-architect/uroborosql

/**
 * JSON化
 *
 * @return JSON
 */
public String toJSON() {
  return "{\"sqlName\":" + StringEscapeUtils.escapeJson(sqlName) + ",\"md5\":" + md5 + ",\"passRoute\":"
      + passRoute + "}";
}

代码示例来源:origin: org.eclipse.dirigible/dirigible-api-utils

/**
 * Escape JSON.
 *
 * @param input
 *            the input
 * @return the escaped input
 */
public static final String escapeJson(String input) {
  return StringEscapeUtils.escapeJson(input);
}

代码示例来源:origin: Jasig/uPortal

/**
   * This will return an escaped String value for Json stream, it's to avoid to break Json.
   *
   * @param text String to escape
   * @return
   */
  public static String escapeForJson(String text) {
    return StringEscapeUtils.escapeJson(text);
  }
}

代码示例来源:origin: org.jasig.portal/uPortal-security-xslt

/**
   * This will return an escaped String value for Json stream, it's to avoid to break Json.
   *
   * @param text String to escape
   * @return
   */
  public static String escapeForJson(String text) {
    return StringEscapeUtils.escapeJson(text);
  }
}

代码示例来源:origin: HubSpot/jinjava

@Override
 public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
  return StringEscapeUtils.escapeJson(Objects.toString(var));
 }
}

代码示例来源:origin: alipay/sofa-lookout

/**
 * 非数值或者String类型的对象进行,Json化处理(包括转义);
 *
 * @param value
 * @param <T>
 * @return
 */
public static <T> Object printValue(T value) {
  return value == null ? value : (value instanceof String || value instanceof Number ? value
    : StringEscapeUtils.escapeJson(JSON.toJSONString(value)));
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

/**
 * Appends the given String enclosed in double-quotes to the given StringBuffer.
 *
 * @param buffer the StringBuffer to append the value to.
 * @param value the value to append.
 */
private void appendValueAsString(final StringBuffer buffer, final String value) {
  buffer.append('"').append(StringEscapeUtils.escapeJson(value)).append('"');
}

代码示例来源:origin: io.virtdata/virtdata-lib-curves4

/**
 * Appends the given String enclosed in double-quotes to the given StringBuffer.
 *
 * @param buffer the StringBuffer to append the value to.
 * @param value the value to append.
 */
private void appendValueAsString(final StringBuffer buffer, final String value) {
  buffer.append('"').append(StringEscapeUtils.escapeJson(value)).append('"');
}

代码示例来源:origin: com.hurence.logisland/logisland-hbase-plugin

private void appendString(final StringBuilder jsonBuilder, final String str, final boolean base64encode) {
  jsonBuilder.append("\"");
  // only escape the value when not doing base64
  if (!base64encode) {
    jsonBuilder.append(StringEscapeUtils.escapeJson(str));
  } else {
    jsonBuilder.append(str);
  }
  jsonBuilder.append("\"");
}

代码示例来源:origin: com.hurence.logisland/logisland-hbase-plugin

private void appendString(final StringBuilder jsonBuilder, final String str, final boolean base64encode) {
  jsonBuilder.append("\"");
  // only escape the value when not doing base64
  if (!base64encode) {
    jsonBuilder.append(StringEscapeUtils.escapeJson(str));
  } else {
    jsonBuilder.append(str);
  }
  jsonBuilder.append("\"");
}

代码示例来源:origin: io.virtdata/virtdata-lib-curves4

@Override
protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
  if (fieldName == null) {
    throw new UnsupportedOperationException(
        "Field names are mandatory when using JsonToStringStyle");
  }
  super.appendFieldStart(buffer, FIELD_NAME_QUOTE + StringEscapeUtils.escapeJson(fieldName)
      + FIELD_NAME_QUOTE);
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

@Override
protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
  if (fieldName == null) {
    throw new UnsupportedOperationException(
        "Field names are mandatory when using JsonToStringStyle");
  }
  super.appendFieldStart(buffer, FIELD_NAME_QUOTE + StringEscapeUtils.escapeJson(fieldName)
      + FIELD_NAME_QUOTE);
}

代码示例来源:origin: de.knightsoft-net/gwt-commons-lang3

/**
 * Appends the given String enclosed in double-quotes to the given StringBuffer.
 *
 * @param buffer the StringBuffer to append the value to.
 * @param value the value to append.
 */
@GwtIncompatible("incompatible method")
private void appendValueAsString(final StringBuffer buffer, final String value) {
 buffer.append('"').append(StringEscapeUtils.escapeJson(value)).append('"');
}

代码示例来源:origin: de.knightsoft-net/gwt-commons-lang3

@Override
@GwtIncompatible("incompatible method")
protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
  if (fieldName == null) {
    throw new UnsupportedOperationException(
        "Field names are mandatory when using JsonToStringStyle");
  }
  super.appendFieldStart(buffer, FIELD_NAME_QUOTE + StringEscapeUtils.escapeJson(fieldName)
      + FIELD_NAME_QUOTE);
}

代码示例来源:origin: discord-java/discord.jar

public void editMessage(String edit) {
  edited = true;
  message = edit;
  PacketBuilder pb = new PacketBuilder(api);
  pb.setType(RequestType.PATCH);
  pb.setData(new JSONObject().put("content", StringEscapeUtils.escapeJson(edit)).put("embed", embeds.isEmpty() ? new JSONObject() : embeds.get(0).toJson()).put("mentions", mentions).toString());
  pb.setUrl("https://discordapp.com/api/channels/" + groupId + "/messages/" + id);
  pb.makeRequest();
}

代码示例来源:origin: discord-java/discord.jar

@Override
public void changeAvatar(String avatar) {
  PacketBuilder pb = new PacketBuilder(api);
  pb.setType(RequestType.PATCH);
  pb.setData(new JSONObject().put("avatar", StringEscapeUtils.escapeJson(avatar)).toString());
  pb.setUrl("https://discordapp.com/api/webhooks/" + id);
  System.out.println(pb.makeRequest());
}

相关文章