Skip to content

Commit 85b9812

Browse files
committed
Complete the strategy in JsonTool.escapeStrValue().
1 parent 92f6455 commit 85b9812

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

streamis-jobmanager/streamis-job-log/streamis-job-log-common/src/main/java/com/webank/wedatasphere/streamis/jobmanager/log/entities/StreamisLogEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void setSequenceId(int sequenceId){
7676
public String toJson(){
7777
return "{" +
7878
"\"logTimeStamp\":" + logTimeInMills +
79-
",\"content\":" + (Objects.isNull(content)? null : "\"" + JsonTool.encodeStrValue(content) + "\"") +
79+
",\"content\":" + (Objects.isNull(content)? null : "\"" + JsonTool.escapeStrValue(content) + "\"") +
8080
",\"sequenceId\":0"
8181
+ "}";
8282

streamis-jobmanager/streamis-job-log/streamis-job-log-common/src/main/java/com/webank/wedatasphere/streamis/jobmanager/log/entities/StreamisLogEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void setSequenceId(int sequenceId){
9191
public String toJson(){
9292
return "{" +
9393
"\"logTimeStamp\":" + logTimeInMills +
94-
",\"appName\":" + (Objects.isNull(appName)? null : "\"" + JsonTool.encodeStrValue(appName) + "\"") +
94+
",\"appName\":" + (Objects.isNull(appName)? null : "\"" + JsonTool.escapeStrValue(appName) + "\"") +
9595
",\"events\":[" +
9696
(Objects.isNull(events) || events.length <=0 ? "" : joinEvents(events, ",") ) + "]" +
9797
",\"sequenceId\":0"

streamis-jobmanager/streamis-job-log/streamis-job-log-common/src/main/java/com/webank/wedatasphere/streamis/jobmanager/log/json/JsonTool.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.webank.wedatasphere.streamis.jobmanager.log.json;
22

3+
import java.util.Locale;
4+
35
public class JsonTool {
6+
static final char[] HEX_DIGITS = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
47

58
/**
69
* Avoid the special char
710
* @param input input string
811
* @return output string
912
*/
10-
public static String encodeStrValue(String input){
13+
public static String escapeStrValue(String input){
1114
char[] chars = input.toCharArray();
1215
StringBuilder sb = new StringBuilder();
1316
for (char c : chars) {
@@ -37,9 +40,24 @@ public static String encodeStrValue(String input){
3740
sb.append("\\t");
3841
break;
3942
default:
40-
sb.append(c);
43+
sb.append((c < 32) ? escapeUnicode(c) : c);
4144
}
4245
}
4346
return sb.toString();
4447
}
48+
49+
/**
50+
* Escape unicode
51+
* @param code char code
52+
* @return escaped string
53+
*/
54+
private static String escapeUnicode(int code){
55+
if (code > 0xffff){
56+
return "\\u" + Integer.toHexString(code).toUpperCase(Locale.ENGLISH);
57+
} else {
58+
return "\\u" + HEX_DIGITS[(code >> 12) & 15]
59+
+ HEX_DIGITS[(code >> 8) & 15] + HEX_DIGITS[(code >> 4) & 15] + HEX_DIGITS[code & 15];
60+
}
61+
}
62+
4563
}

0 commit comments

Comments
 (0)