BAEL-2327: Implement simple escaping and tests (#5745)
* BAEL-2327: Implement simple escaping and tests * BAEL-2327: Rename test to comply with CI rules
This commit is contained in:
parent
0b0aedc287
commit
0abf2d243a
10
json/pom.xml
10
json/pom.xml
|
@ -33,6 +33,16 @@
|
||||||
<groupId>org.json</groupId>
|
<groupId>org.json</groupId>
|
||||||
<artifactId>json</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>20171018</version>
|
<version>20171018</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.9.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.json.bind</groupId>
|
<groupId>javax.json.bind</groupId>
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.baeldung.escape;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
class JsonEscape {
|
||||||
|
|
||||||
|
String escapeJson(String input) {
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("message", input);
|
||||||
|
return jsonObject.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
String escapeGson(String input) {
|
||||||
|
JsonObject gsonObject = new JsonObject();
|
||||||
|
gsonObject.addProperty("message", input);
|
||||||
|
return gsonObject.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
String escapeJackson(String input) throws JsonProcessingException {
|
||||||
|
return new ObjectMapper().writeValueAsString(new Payload(input));
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Payload {
|
||||||
|
String message;
|
||||||
|
|
||||||
|
Payload(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.escape;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class JsonEscapeUnitTest {
|
||||||
|
|
||||||
|
private JsonEscape testedInstance;
|
||||||
|
private static final String EXPECTED = "{\"message\":\"Hello \\\"World\\\"\"}";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
testedInstance = new JsonEscape();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void escapeJson() {
|
||||||
|
String actual = testedInstance.escapeJson("Hello \"World\"");
|
||||||
|
assertEquals(EXPECTED, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void escapeGson() {
|
||||||
|
String actual = testedInstance.escapeGson("Hello \"World\"");
|
||||||
|
assertEquals(EXPECTED, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void escapeJackson() throws JsonProcessingException {
|
||||||
|
String actual = testedInstance.escapeJackson("Hello \"World\"");
|
||||||
|
assertEquals(EXPECTED, actual);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue