You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2013/04/12 10:39:02 UTC
svn commit: r1467206 - in /commons/proper/lang/trunk/src: changes/changes.xml
main/java/org/apache/commons/lang3/StringEscapeUtils.java
test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java
Author: bayard
Date: Fri Apr 12 08:39:02 2013
New Revision: 1467206
URL: http://svn.apache.org/r1467206
Log:
Added escape/unescapeJson methods per Maurizio Cucchiara's patch in LANG-797
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1467206&r1=1467205&r2=1467206&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Fri Apr 12 08:39:02 2013
@@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
+ <action issue="LANG-797" type="add">Added escape/unescapeJson to StringEscapeUtils</action>
<action issue="LANG-875" type="add">Added appendIfMissing and prependIfMissing methods to StringUtils</action>
<action issue="LANG-881" type="fix">NumberUtils.createNumber() Javadoc says it does not work for octal numbers</action>
<action type="fix">Fixed URLs in javadoc to point to new oracle.com pages</action>
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java?rev=1467206&r1=1467205&r2=1467206&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java Fri Apr 12 08:39:02 2013
@@ -81,7 +81,28 @@ public class StringEscapeUtils {
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
JavaUnicodeEscaper.outsideOf(32, 0x7f)
);
-
+
+ /**
+ * Translator object for escaping Json.
+ *
+ * While {@link #escapeJson(String)} is the expected method of use, this
+ * object allows the Json escaping functionality to be used
+ * as the foundation for a custom translator.
+ *
+ * @since 3.2
+ */
+ public static final CharSequenceTranslator ESCAPE_JSON =
+ new AggregateTranslator(
+ new LookupTranslator(
+ new String[][] {
+ {"\"", "\\\""},
+ {"\\", "\\\\"},
+ {"/", "\\/"}
+ }),
+ new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
+ JavaUnicodeEscaper.outsideOf(32, 0x7f)
+ );
+
/**
* Translator object for escaping XML.
*
@@ -206,6 +227,17 @@ public class StringEscapeUtils {
public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT = UNESCAPE_JAVA;
/**
+ * Translator object for unescaping escaped Json.
+ *
+ * While {@link #unescapeJson(String)} is the expected method of use, this
+ * object allows the Json unescaping functionality to be used
+ * as the foundation for a custom translator.
+ *
+ * @since 3.2
+ */
+ public static final CharSequenceTranslator UNESCAPE_JSON = UNESCAPE_JAVA;
+
+ /**
* Translator object for unescaping escaped HTML 3.0.
*
* While {@link #unescapeHtml3(String)} is the expected method of use, this
@@ -371,6 +403,35 @@ public class StringEscapeUtils {
}
/**
+ * <p>Escapes the characters in a {@code String} using Json String rules.</p>
+ * <p>Escapes any values it finds into their Json String form.
+ * Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p>
+ *
+ * <p>So a tab becomes the characters {@code '\\'} and
+ * {@code 't'}.</p>
+ *
+ * <p>The only difference between Java strings and Json strings
+ * is that in Json, forward-slash (/) is escaped.</p>
+ *
+ * <p>See http://www.ietf.org/rfc/rfc4627.txt for further details. </p>
+ *
+ * <p>Example:
+ * <pre>
+ * input string: He didn't say, "Stop!"
+ * output string: He didn't say, \"Stop!\"
+ * </pre>
+ * </p>
+ *
+ * @param input String to escape values in, may be null
+ * @return String with escaped values, {@code null} if null string input
+ *
+ * @since 3.2
+ */
+ public static final String escapeJson(final String input) {
+ return ESCAPE_JSON.translate(input);
+ }
+
+ /**
* <p>Unescapes any Java literals found in the {@code String}.
* For example, it will turn a sequence of {@code '\'} and
* {@code 'n'} into a newline character, unless the {@code '\'}
@@ -400,6 +461,23 @@ public class StringEscapeUtils {
return UNESCAPE_ECMASCRIPT.translate(input);
}
+ /**
+ * <p>Unescapes any Json literals found in the {@code String}.</p>
+ *
+ * <p>For example, it will turn a sequence of {@code '\'} and {@code 'n'}
+ * into a newline character, unless the {@code '\'} is preceded by another
+ * {@code '\'}.</p>
+ *
+ * @see #unescapeJava(String)
+ * @param input the {@code String} to unescape, may be null
+ * @return A new unescaped {@code String}, {@code null} if null string input
+ *
+ * @since 3.2
+ */
+ public static final String unescapeJson(final String input) {
+ return UNESCAPE_JSON.translate(input);
+ }
+
// HTML and XML
//--------------------------------------------------------------------------
/**
Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java?rev=1467206&r1=1467205&r2=1467206&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringEscapeUtilsTest.java Fri Apr 12 08:39:02 2013
@@ -529,4 +529,31 @@ public class StringEscapeUtilsTest {
final String escaped = StringEscapeUtils.escapeXml(input);
assertEquals(input, escaped);
}
+
+ @Test
+ public void testEscapeJson() {
+ assertEquals(null, 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!\""));
+
+ String expected = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
+ String input ="\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
+
+ assertEquals(expected, StringEscapeUtils.escapeJson(input));
+ }
+
}