Skip to content

Commit 0abf2d2

Browse files
mchugunovEugen
authored andcommitted
BAEL-2327: Implement simple escaping and tests (eugenp#5745)
* BAEL-2327: Implement simple escaping and tests * BAEL-2327: Rename test to comply with CI rules
1 parent 0b0aedc commit 0abf2d2

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

json/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
<groupId>org.json</groupId>
3434
<artifactId>json</artifactId>
3535
<version>20171018</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.google.code.gson</groupId>
39+
<artifactId>gson</artifactId>
40+
<version>2.8.5</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.fasterxml.jackson.core</groupId>
44+
<artifactId>jackson-databind</artifactId>
45+
<version>2.9.7</version>
3646
</dependency>
3747
<dependency>
3848
<groupId>javax.json.bind</groupId>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.escape;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.google.gson.JsonObject;
6+
import org.json.JSONObject;
7+
8+
class JsonEscape {
9+
10+
String escapeJson(String input) {
11+
JSONObject jsonObject = new JSONObject();
12+
jsonObject.put("message", input);
13+
return jsonObject.toString();
14+
}
15+
16+
String escapeGson(String input) {
17+
JsonObject gsonObject = new JsonObject();
18+
gsonObject.addProperty("message", input);
19+
return gsonObject.toString();
20+
}
21+
22+
String escapeJackson(String input) throws JsonProcessingException {
23+
return new ObjectMapper().writeValueAsString(new Payload(input));
24+
}
25+
26+
static class Payload {
27+
String message;
28+
29+
Payload(String message) {
30+
this.message = message;
31+
}
32+
33+
public String getMessage() {
34+
return message;
35+
}
36+
37+
public void setMessage(String message) {
38+
this.message = message;
39+
}
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.escape;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class JsonEscapeUnitTest {
10+
11+
private JsonEscape testedInstance;
12+
private static final String EXPECTED = "{\"message\":\"Hello \\\"World\\\"\"}";
13+
14+
@BeforeEach
15+
void setUp() {
16+
testedInstance = new JsonEscape();
17+
}
18+
19+
@Test
20+
void escapeJson() {
21+
String actual = testedInstance.escapeJson("Hello \"World\"");
22+
assertEquals(EXPECTED, actual);
23+
}
24+
25+
@Test
26+
void escapeGson() {
27+
String actual = testedInstance.escapeGson("Hello \"World\"");
28+
assertEquals(EXPECTED, actual);
29+
}
30+
31+
@Test
32+
void escapeJackson() throws JsonProcessingException {
33+
String actual = testedInstance.escapeJackson("Hello \"World\"");
34+
assertEquals(EXPECTED, actual);
35+
}
36+
}

0 commit comments

Comments
 (0)