EscapeTool extends org.apache.velocity.tools.generic.EscapeTool { public String json(Object string) { if (string == null) { return null; } return StringEscapeUtils.escapeJson(String.valueOf(string)); }  @Override String xml(Object content); String json(Object string); String quotedPrintable(Object content); String q(Object content); String b(Object content); String url(Map<String, ?> parametersMap); String css(String identifier); @Override String url(Object string);  }
@Test void escapeJSON() { String escapedText = this.tool.json("\"'\\/\b\f\n\r\t\u1234 plain text"); assertTrue(escapedText.contains("\\\""), "Failed to escape [\"]"); assertTrue(escapedText.contains("'"), "Wrongly escaped [']"); assertTrue(escapedText.contains("\\\\"), "Failed to escape [\\]"); assertTrue(escapedText.contains("\\/"), "Failed to escape [/]"); assertTrue(escapedText.contains("\\b"), "Failed to escape [\\b]"); assertTrue(escapedText.contains("\\f"), "Failed to escape [\\f]"); assertTrue(escapedText.contains("\\n"), "Failed to escape [\\n]"); assertTrue(escapedText.contains("\\r"), "Failed to escape [\\r]"); assertTrue(escapedText.contains("\\t"), "Failed to escape [\\t]"); assertTrue(escapedText.contains("\\u1234"), "Failed to escape [\\u1234]"); assertTrue(escapedText.contains(" plain text"), "Wrongly escaped plain text"); }
@Test void escapeJSONWithNullInput() { assertNull(this.tool.json(null), "Unexpected non-null output for null input"); }
@Test void escapeJSONWithNonStringInput() { assertEquals("true", this.tool.json(true)); assertEquals("42", this.tool.json(42)); assertEquals(this.tool.toString(), this.tool.json(this.tool)); }