-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathEscapeUtil.java
More file actions
56 lines (50 loc) · 1.55 KB
/
EscapeUtil.java
File metadata and controls
56 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package graphql.util;
import graphql.Internal;
@Internal
public final class EscapeUtil {
private EscapeUtil() {
}
/**
* Encodes the value as a JSON string according to <a href="https://json.org/">https://json.org/</a> rules
*
* @param stringValue the value to encode as a JSON string
*
* @return the encoded string
*/
public static String escapeJsonString(String stringValue) {
StringBuilder sb = new StringBuilder(stringValue.length());
escapeJsonStringTo(sb, stringValue);
return sb.toString();
}
public static void escapeJsonStringTo(StringBuilder output, String stringValue) {
int len = stringValue.length();
for (int i = 0; i < len; i++) {
char ch = stringValue.charAt(i);
switch (ch) {
case '"':
output.append("\\\"");
break;
case '\\':
output.append("\\\\");
break;
case '\b':
output.append("\\b");
break;
case '\f':
output.append("\\f");
break;
case '\n':
output.append("\\n");
break;
case '\r':
output.append("\\r");
break;
case '\t':
output.append("\\t");
break;
default:
output.append(ch);
}
}
}
}