Skip to content

Commit

Permalink
fix serialize String with '\n' unescaped, for issue #1387
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 7, 2023
1 parent a0f246a commit a88bb14
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,7 @@ public final void writeString(final char[] chars) {
boolean special = false;
for (int i = 0; i < chars.length; ++i) {
char c = chars[i];
if (c == '\\' || c == quote) {
if (c == '\\' || c == quote || c < ' ') {
special = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.alibaba.fastjson2.issues_1000;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue1387 {
@Test
public void test() {
String json = "{\"date\":\"2021\\nNOV\"}";
Bean params = JSON.parseObject(json, Bean.class);
assertEquals(json, JSON.toJSONString(params));
assertEquals(json, new String(JSON.toJSONBytes(params)));
}

public static class Bean {
private String date;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}
}
}

0 comments on commit a88bb14

Please sign in to comment.