From ad5b54d62d826a257a8d41643769a48cd6863aee Mon Sep 17 00:00:00 2001 From: David Walton Date: Thu, 17 Nov 2022 17:30:48 -0800 Subject: [PATCH 1/2] VELTOOLS-184 $esc.json() escapes ", \, and / --- .../velocity/tools/generic/EscapeTool.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java index a171beb3b..40c0af5b3 100644 --- a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java +++ b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java @@ -44,6 +44,9 @@ * $javascript -> He didn't say, "Stop!" * $esc.javascript($javascript) -> He didn\'t say, \"Stop!\" * + * $json -> He didn't say, "Stop!" + * $esc.json($json) -> He didn't say, \"Stop!\" + * * $html -> "bread" & "butter" * $esc.html($html) -> "bread" & "butter" * @@ -301,6 +304,24 @@ public String javascript(Object string) return StringEscapeUtils.escapeEcmaScript(String.valueOf(string)); } + /** + *

Escapes the characters in a String using JSON String rules.

+ *

Delegates the process to {@link StringEscapeUtils#escapeJson(String)}.

+ * + * @param string the string to escape values, may be null + * @return String with escaped values, null if null string input + * + * @see StringEscapeUtils#escapeJson(String) + */ + public String json(Object string) + { + if (string == null) + { + return null; + } + return StringEscapeUtils.escapeJson(String.valueOf(string)); + } + /** *

Escapes the characters in a String using HTML entities.

*

Delegates the process to {@link StringEscapeUtils#escapeHtml4(String)}.

From 783a34e9be4bc98882d2b97b6f41799b2da8375a Mon Sep 17 00:00:00 2001 From: David Walton Date: Fri, 18 Nov 2022 10:07:40 -0800 Subject: [PATCH 2/2] Make json() a synonym for java() --- .../org/apache/velocity/tools/generic/EscapeTool.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java index 40c0af5b3..a638e1691 100644 --- a/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java +++ b/velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/EscapeTool.java @@ -306,20 +306,14 @@ public String javascript(Object string) /** *

Escapes the characters in a String using JSON String rules.

- *

Delegates the process to {@link StringEscapeUtils#escapeJson(String)}.

+ *

Delegates the process to {@link #java(String)}.

* * @param string the string to escape values, may be null * @return String with escaped values, null if null string input - * - * @see StringEscapeUtils#escapeJson(String) */ public String json(Object string) { - if (string == null) - { - return null; - } - return StringEscapeUtils.escapeJson(String.valueOf(string)); + return java(string); } /**