@Test public void testEscape() { String jsonRequest = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"isAlive\": true,\"age\": 27,\"address\": {\"streetAddress\": \"21 2nd Street\",\"city\": \"New York\",\"state\": \"NY\",\"postalCode\": \"10021-3100\"},\"phoneNumbers\": [ { \"type\": \"home\", \"number\": \"212 555-1234\" }, { \"type\": \"office\", \"number\": \"646 555-4567\" }, { \"type\": \"mobile\", \"number\": \"123 456-7890\" } ],\"children\": [],\"spouse\": null}"; assertEquals("hello", jsonHandler.escape("hello", true)); assertEquals("\\{\"responseControl\":\\{\"profile\":\\{\"profileType\":\"ANONYMOUS_USER\"\\}\\}\\}", jsonHandler.escape("{\"responseControl\":{\"profile\":{\"profileType\":\"ANONYMOUS_USER\"}}}", true)); assertEquals("\\[\"Ford\",\"BMW\",\"Fiat\"\\]", jsonHandler.escape("[ \"Ford\", \"BMW\", \"Fiat\" ]", true)); assertEquals("\\[\\{\"id\":\".*\",\"name\":\"Privatkon[a-z]{2}\",\"balance\":34251.15766987801\\}\\]", jsonHandler.escape("[{\"id\":\".*\",\"name\":\"Privatkon[a-z]{2}\",\"balance\":34251.15766987801}]", true)); assertEquals("\\{\"firstName\":\"John\",\"lastName\":\"Smith\",\"isAlive\":true,\"age\":27,\"address\":\\{\"streetAddress\":\"21 2nd Street\",\"city\":\"New York\",\"state\":\"NY\",\"postalCode\":\"10021-3100\"\\},\"phoneNumbers\":\\[\\{\"type\":\"home\",\"number\":\"212 555-1234\"\\},\\{\"type\":\"office\",\"number\":\"646 555-4567\"\\},\\{\"type\":\"mobile\",\"number\":\"123 456-7890\"\\}\\],\"children\":\\[\\],\"spouse\":null\\}", jsonHandler.escape(jsonRequest, true)); }
private void escape(StringBuilder sb, JsonNode node, String name) { if (node.isArray()) { sb.append("\\["); Iterator<JsonNode> iterator = node.iterator(); int n = 0; while (iterator.hasNext()) { if (n > 0) { sb.append(","); } n++; escape(sb, iterator.next(), null); } sb.append("\\]"); } else if (node.isObject()) { sb.append("\\{"); Iterator<Entry<String, JsonNode>> fields = node.getFields(); int n = 0; while (fields.hasNext()) { if (n > 0) { sb.append(","); } n++; Entry<String, JsonNode> field = fields.next(); sb.append("\"").append(field.getKey()).append("\":"); escape(sb, field.getValue(), field.getKey()); } sb.append("\\}"); } else { sb.append(node.toString()); } }
JsonHandler extends AbstractHandler { private void escape(StringBuilder sb, JsonNode node, String name) { if (node.isArray()) { sb.append("\\["); Iterator<JsonNode> iterator = node.iterator(); int n = 0; while (iterator.hasNext()) { if (n > 0) { sb.append(","); } n++; escape(sb, iterator.next(), null); } sb.append("\\]"); } else if (node.isObject()) { sb.append("\\{"); Iterator<Entry<String, JsonNode>> fields = node.getFields(); int n = 0; while (fields.hasNext()) { if (n > 0) { sb.append(","); } n++; Entry<String, JsonNode> field = fields.next(); sb.append("\"").append(field.getKey()).append("\":"); escape(sb, field.getValue(), field.getKey()); } sb.append("\\}"); } else { sb.append(node.toString()); } } }
JsonHandler extends AbstractHandler { private void escape(StringBuilder sb, JsonNode node, String name) { if (node.isArray()) { sb.append("\\["); Iterator<JsonNode> iterator = node.iterator(); int n = 0; while (iterator.hasNext()) { if (n > 0) { sb.append(","); } n++; escape(sb, iterator.next(), null); } sb.append("\\]"); } else if (node.isObject()) { sb.append("\\{"); Iterator<Entry<String, JsonNode>> fields = node.getFields(); int n = 0; while (fields.hasNext()) { if (n > 0) { sb.append(","); } n++; Entry<String, JsonNode> field = fields.next(); sb.append("\"").append(field.getKey()).append("\":"); escape(sb, field.getValue(), field.getKey()); } sb.append("\\}"); } else { sb.append(node.toString()); } } JsonHandler(); }
JsonHandler extends AbstractHandler { private void escape(StringBuilder sb, JsonNode node, String name) { if (node.isArray()) { sb.append("\\["); Iterator<JsonNode> iterator = node.iterator(); int n = 0; while (iterator.hasNext()) { if (n > 0) { sb.append(","); } n++; escape(sb, iterator.next(), null); } sb.append("\\]"); } else if (node.isObject()) { sb.append("\\{"); Iterator<Entry<String, JsonNode>> fields = node.getFields(); int n = 0; while (fields.hasNext()) { if (n > 0) { sb.append(","); } n++; Entry<String, JsonNode> field = fields.next(); sb.append("\"").append(field.getKey()).append("\":"); escape(sb, field.getValue(), field.getKey()); } sb.append("\\}"); } else { sb.append(node.toString()); } } JsonHandler(); String escape(String request, boolean isCandidate); }
JsonHandler extends AbstractHandler { private void escape(StringBuilder sb, JsonNode node, String name) { if (node.isArray()) { sb.append("\\["); Iterator<JsonNode> iterator = node.iterator(); int n = 0; while (iterator.hasNext()) { if (n > 0) { sb.append(","); } n++; escape(sb, iterator.next(), null); } sb.append("\\]"); } else if (node.isObject()) { sb.append("\\{"); Iterator<Entry<String, JsonNode>> fields = node.getFields(); int n = 0; while (fields.hasNext()) { if (n > 0) { sb.append(","); } n++; Entry<String, JsonNode> field = fields.next(); sb.append("\"").append(field.getKey()).append("\":"); escape(sb, field.getValue(), field.getKey()); } sb.append("\\}"); } else { sb.append(node.toString()); } } JsonHandler(); String escape(String request, boolean isCandidate);  }