* BAEL-2327: Implement simple escaping and tests * BAEL-2327: Rename test to comply with CI rules
42 lines
1.0 KiB
Java
42 lines
1.0 KiB
Java
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;
|
|
}
|
|
}
|
|
}
|