mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
add EscapeJSON: explicit JSON contente excape function
This commit is contained in:
parent
0777048002
commit
330cc6e0ad
@ -0,0 +1,32 @@
|
||||
package io.nosqlbench.virtdata.library.basics.shared.from_string;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Escape all special characters which are required to be escaped when found within
|
||||
* JSON content according to the JSON spec
|
||||
* <pre>{@code
|
||||
* \b Backspace (ascii code 08)
|
||||
* \f Form feed (ascii code 0C)
|
||||
* \n New line
|
||||
* \r Carriage return
|
||||
* \t Tab
|
||||
* \" Double quote
|
||||
* \\ Backslash character
|
||||
* \/ Forward slash
|
||||
* }</pre>
|
||||
*/
|
||||
@ThreadSafeMapper
|
||||
public class EscapeJSON implements Function<String,String> {
|
||||
Gson gson = new GsonBuilder().create();
|
||||
|
||||
@Override
|
||||
public String apply(String s) {
|
||||
return StringEscapeUtils.escapeJson(s);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package io.nosqlbench.virtdata.library.basics.shared.from_string;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EscapeJSONTest {
|
||||
|
||||
@Test
|
||||
public void testEscapes() {
|
||||
EscapeJSON escapeJSON = new EscapeJSON();
|
||||
assertThat(escapeJSON.apply("\t")).isEqualTo("\\t");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user