Skip to content

Commit fc3c5c5

Browse files
committed
improve support invisible chars
1 parent 96e322f commit fc3c5c5

6 files changed

Lines changed: 149 additions & 61 deletions

File tree

src/main/java/com/alibaba/fastjson/parser/CharTypes.java

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,63 @@ public final class CharTypes {
5252
}
5353
}
5454

55-
public final static boolean[] specicalFlags_doubleQuotes = new boolean[128];
56-
public final static boolean[] specicalFlags_singleQuotes = new boolean[128];
57-
58-
public static boolean isSpecial_doubleQuotes(char ch) {
59-
return ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch];
60-
}
55+
public final static byte[] specicalFlags_doubleQuotes = new byte[256];
56+
public final static byte[] specicalFlags_singleQuotes = new byte[256];
6157

6258
public final static char[] replaceChars = new char[128];
6359
static {
64-
specicalFlags_doubleQuotes['\0'] = true;
65-
specicalFlags_doubleQuotes['\1'] = true;
66-
specicalFlags_doubleQuotes['\2'] = true;
67-
specicalFlags_doubleQuotes['\3'] = true;
68-
specicalFlags_doubleQuotes['\4'] = true;
69-
specicalFlags_doubleQuotes['\5'] = true;
70-
specicalFlags_doubleQuotes['\6'] = true;
71-
specicalFlags_doubleQuotes['\7'] = true;
72-
specicalFlags_doubleQuotes['\b'] = true; // 8
73-
specicalFlags_doubleQuotes['\t'] = true; // 9
74-
specicalFlags_doubleQuotes['\n'] = true; // 10
75-
specicalFlags_doubleQuotes['\u000B'] = true; // 11
76-
specicalFlags_doubleQuotes['\f'] = true;
77-
specicalFlags_doubleQuotes['\r'] = true;
78-
specicalFlags_doubleQuotes['\"'] = true;
79-
specicalFlags_doubleQuotes['\\'] = true;
80-
81-
specicalFlags_singleQuotes['\0'] = true;
82-
specicalFlags_singleQuotes['\1'] = true;
83-
specicalFlags_singleQuotes['\2'] = true;
84-
specicalFlags_singleQuotes['\3'] = true;
85-
specicalFlags_singleQuotes['\4'] = true;
86-
specicalFlags_singleQuotes['\5'] = true;
87-
specicalFlags_singleQuotes['\6'] = true;
88-
specicalFlags_singleQuotes['\7'] = true;
89-
specicalFlags_singleQuotes['\b'] = true; // 8
90-
specicalFlags_singleQuotes['\t'] = true; // 9
91-
specicalFlags_singleQuotes['\n'] = true; // 10
92-
specicalFlags_singleQuotes['\u000B'] = true; // 11
93-
specicalFlags_singleQuotes['\f'] = true; // 12
94-
specicalFlags_singleQuotes['\r'] = true;
95-
specicalFlags_singleQuotes['\''] = true;
96-
specicalFlags_singleQuotes['\\'] = true;
60+
specicalFlags_doubleQuotes['\0'] = 1;
61+
specicalFlags_doubleQuotes['\1'] = 1;
62+
specicalFlags_doubleQuotes['\2'] = 1;
63+
specicalFlags_doubleQuotes['\3'] = 1;
64+
specicalFlags_doubleQuotes['\4'] = 1;
65+
specicalFlags_doubleQuotes['\5'] = 1;
66+
specicalFlags_doubleQuotes['\6'] = 1;
67+
specicalFlags_doubleQuotes['\7'] = 1;
68+
specicalFlags_doubleQuotes['\b'] = 1; // 8
69+
specicalFlags_doubleQuotes['\t'] = 1; // 9
70+
specicalFlags_doubleQuotes['\n'] = 1; // 10
71+
specicalFlags_doubleQuotes['\u000B'] = 1; // 11
72+
specicalFlags_doubleQuotes['\f'] = 1;
73+
specicalFlags_doubleQuotes['\r'] = 1;
74+
specicalFlags_doubleQuotes['\"'] = 1;
75+
specicalFlags_doubleQuotes['\\'] = 1;
9776

77+
specicalFlags_singleQuotes['\0'] = 1;
78+
specicalFlags_singleQuotes['\1'] = 1;
79+
specicalFlags_singleQuotes['\2'] = 1;
80+
specicalFlags_singleQuotes['\3'] = 1;
81+
specicalFlags_singleQuotes['\4'] = 1;
82+
specicalFlags_singleQuotes['\5'] = 1;
83+
specicalFlags_singleQuotes['\6'] = 1;
84+
specicalFlags_singleQuotes['\7'] = 1;
85+
specicalFlags_singleQuotes['\b'] = 1; // 8
86+
specicalFlags_singleQuotes['\t'] = 1; // 9
87+
specicalFlags_singleQuotes['\n'] = 1; // 10
88+
specicalFlags_singleQuotes['\u000B'] = 1; // 11
89+
specicalFlags_singleQuotes['\f'] = 1; // 12
90+
specicalFlags_singleQuotes['\r'] = 1; // 13
91+
specicalFlags_singleQuotes['\u000E'] = 4; // 14
92+
specicalFlags_singleQuotes['\u000F'] = 4; // 15
93+
specicalFlags_singleQuotes['\u0010'] = 4; // 16
94+
specicalFlags_singleQuotes['\u0011'] = 4; // 17
95+
specicalFlags_singleQuotes['\u0012'] = 4; // 18
96+
specicalFlags_singleQuotes['\u0013'] = 4; // 19
97+
specicalFlags_singleQuotes['\u0014'] = 4; // 20
98+
specicalFlags_singleQuotes['\u0015'] = 4; // 21
99+
specicalFlags_singleQuotes['\u0016'] = 4; // 22
100+
specicalFlags_singleQuotes['\u0017'] = 4; // 23
101+
specicalFlags_singleQuotes['\u0018'] = 4; // 24
102+
specicalFlags_singleQuotes['\u0019'] = 4; // 25
103+
specicalFlags_singleQuotes['\u0020'] = 4; // 26
104+
specicalFlags_singleQuotes['\\'] = 1;
105+
specicalFlags_singleQuotes['\''] = 1;
106+
107+
for (int i = 0x7F; i <= 0xA0; ++i) {
108+
specicalFlags_doubleQuotes[i] = 4;
109+
specicalFlags_singleQuotes[i] = 4;
110+
}
111+
98112
replaceChars['\0'] = '0';
99113
replaceChars['\1'] = '1';
100114
replaceChars['\2'] = '2';

src/main/java/com/alibaba/fastjson/parser/deserializer/CollectionDeserializer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.AbstractCollection;
66
import java.util.ArrayList;
77
import java.util.Collection;
8+
import java.util.EnumSet;
89
import java.util.HashSet;
910
import java.util.LinkedHashSet;
1011
import java.util.TreeSet;
@@ -37,6 +38,14 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
3738
list = new TreeSet();
3839
} else if (rawClass.isAssignableFrom(ArrayList.class)) {
3940
list = new ArrayList();
41+
} else if (rawClass.isAssignableFrom(EnumSet.class)) {
42+
Type itemType;
43+
if (type instanceof ParameterizedType) {
44+
itemType = ((ParameterizedType) type).getActualTypeArguments()[0];
45+
} else {
46+
itemType = Object.class;
47+
}
48+
list = EnumSet.noneOf((Class<Enum>)itemType);
4049
} else {
4150
try {
4251
list = (Collection) rawClass.newInstance();

src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ private void writeStringWithDoubleQuote(String text, final char seperator, boole
659659
}
660660
} else {
661661
if (ch < CharTypes.specicalFlags_doubleQuotes.length
662-
&& CharTypes.specicalFlags_doubleQuotes[ch] //
662+
&& CharTypes.specicalFlags_doubleQuotes[ch] != 0 //
663663
|| (ch == '/' && isEnabled(SerializerFeature.WriteSlashAsSpecial))) {
664664
write('\\');
665665
write(replaceChars[(int) ch]);
@@ -816,7 +816,7 @@ private void writeStringWithDoubleQuote(String text, final char seperator, boole
816816
continue;
817817
}
818818

819-
if (ch < CharTypes.specicalFlags_doubleQuotes.length && CharTypes.specicalFlags_doubleQuotes[ch] //
819+
if (ch < CharTypes.specicalFlags_doubleQuotes.length && CharTypes.specicalFlags_doubleQuotes[ch] != 0 //
820820
|| (ch == '/' && isEnabled(SerializerFeature.WriteSlashAsSpecial))) {
821821
specialCount++;
822822
lastSpecialIndex = i;
@@ -861,7 +861,8 @@ private void writeStringWithDoubleQuote(String text, final char seperator, boole
861861
for (int i = textIndex; i < text.length(); ++i) {
862862
char ch = text.charAt(i);
863863

864-
if (ch < CharTypes.specicalFlags_doubleQuotes.length && CharTypes.specicalFlags_doubleQuotes[ch] //
864+
if (ch < CharTypes.specicalFlags_doubleQuotes.length //
865+
&& CharTypes.specicalFlags_doubleQuotes[ch] != 0 //
865866
|| (ch == '/' && isEnabled(SerializerFeature.WriteSlashAsSpecial))) {
866867
buf[bufIndex++] = '\\';
867868
buf[bufIndex++] = replaceChars[(int) ch];
@@ -1265,8 +1266,8 @@ private void writeFieldValueStringWithDoubleQuote(char seperator, String name, S
12651266
for (int i = textIndex; i < value.length(); ++i) {
12661267
char ch = value.charAt(i);
12671268

1268-
if (ch < CharTypes.specicalFlags_doubleQuotes.length
1269-
&& CharTypes.specicalFlags_doubleQuotes[ch] //
1269+
if (ch < CharTypes.specicalFlags_doubleQuotes.length //
1270+
&& CharTypes.specicalFlags_doubleQuotes[ch] != 0 //
12701271
|| (ch == '/' && isEnabled(SerializerFeature.WriteSlashAsSpecial))) {
12711272
buf[bufIndex++] = '\\';
12721273
buf[bufIndex++] = replaceChars[(int) ch];
@@ -1467,7 +1468,7 @@ public void writeFieldName(String key, boolean checkSpecial) {
14671468
}
14681469

14691470
private void writeKeyWithDoubleQuoteIfHasSpecial(String text) {
1470-
final boolean[] specicalFlags_doubleQuotes = CharTypes.specicalFlags_doubleQuotes;
1471+
final byte[] specicalFlags_doubleQuotes = CharTypes.specicalFlags_doubleQuotes;
14711472

14721473
int len = text.length();
14731474
int newcount = count + len + 1;
@@ -1483,7 +1484,7 @@ private void writeKeyWithDoubleQuoteIfHasSpecial(String text) {
14831484
boolean hasSpecial = false;
14841485
for (int i = 0; i < len; ++i) {
14851486
char ch = text.charAt(i);
1486-
if (ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch]) {
1487+
if (ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch] != 0) {
14871488
hasSpecial = true;
14881489
break;
14891490
}
@@ -1494,7 +1495,7 @@ private void writeKeyWithDoubleQuoteIfHasSpecial(String text) {
14941495
}
14951496
for (int i = 0; i < len; ++i) {
14961497
char ch = text.charAt(i);
1497-
if (ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch]) {
1498+
if (ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch] != 0) {
14981499
write('\\');
14991500
write(replaceChars[(int) ch]);
15001501
} else {
@@ -1531,7 +1532,7 @@ private void writeKeyWithDoubleQuoteIfHasSpecial(String text) {
15311532

15321533
for (int i = start; i < end; ++i) {
15331534
char ch = buf[i];
1534-
if (ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch]) {
1535+
if (ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch] != 0) {
15351536
if (!hasSpecial) {
15361537
newcount += 3;
15371538
if (newcount > buf.length) {
@@ -1567,7 +1568,7 @@ private void writeKeyWithDoubleQuoteIfHasSpecial(String text) {
15671568
}
15681569

15691570
private void writeKeyWithSingleQuoteIfHasSpecial(String text) {
1570-
final boolean[] specicalFlags_singleQuotes = CharTypes.specicalFlags_singleQuotes;
1571+
final byte[] specicalFlags_singleQuotes = CharTypes.specicalFlags_singleQuotes;
15711572

15721573
int len = text.length();
15731574
int newcount = count + len + 1;
@@ -1583,7 +1584,7 @@ private void writeKeyWithSingleQuoteIfHasSpecial(String text) {
15831584
boolean hasSpecial = false;
15841585
for (int i = 0; i < len; ++i) {
15851586
char ch = text.charAt(i);
1586-
if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch]) {
1587+
if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch] != 0) {
15871588
hasSpecial = true;
15881589
break;
15891590
}
@@ -1594,7 +1595,7 @@ private void writeKeyWithSingleQuoteIfHasSpecial(String text) {
15941595
}
15951596
for (int i = 0; i < len; ++i) {
15961597
char ch = text.charAt(i);
1597-
if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch]) {
1598+
if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch] != 0) {
15981599
write('\\');
15991600
write(replaceChars[(int) ch]);
16001601
} else {
@@ -1632,7 +1633,7 @@ private void writeKeyWithSingleQuoteIfHasSpecial(String text) {
16321633

16331634
for (int i = start; i < end; ++i) {
16341635
char ch = buf[i];
1635-
if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch]) {
1636+
if (ch < specicalFlags_singleQuotes.length && specicalFlags_singleQuotes[ch] != 0) {
16361637
if (!hasSpecial) {
16371638
newcount += 3;
16381639
if (newcount > buf.length) {

src/test/java/com/alibaba/json/bvt/CharTypesTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
import com.alibaba.fastjson.parser.CharTypes;
88

99
public class CharTypesTest extends TestCase {
10+
static byte[] specicalFlags_singleQuotes = CharTypes.specicalFlags_singleQuotes;
11+
static byte[] specicalFlags_doubleQuotes = CharTypes.specicalFlags_doubleQuotes;
1012

1113
public void test_0() throws Exception {
12-
Assert.assertTrue(CharTypes.isSpecial_doubleQuotes('\n'));
13-
Assert.assertTrue(CharTypes.isSpecial_doubleQuotes('\r'));
14-
Assert.assertTrue(CharTypes.isSpecial_doubleQuotes('\b'));
15-
Assert.assertTrue(CharTypes.isSpecial_doubleQuotes('\f'));
16-
Assert.assertTrue(CharTypes.isSpecial_doubleQuotes('\"'));
17-
Assert.assertFalse(CharTypes.isSpecial_doubleQuotes('0'));
18-
Assert.assertTrue(CharTypes.isSpecial_doubleQuotes('\0'));
19-
Assert.assertFalse(CharTypes.isSpecial_doubleQuotes('中'));
20-
Assert.assertFalse(CharTypes.isSpecial_doubleQuotes('中'));
14+
15+
Assert.assertTrue(isSpecial_doubleQuotes('\n'));
16+
Assert.assertTrue(isSpecial_doubleQuotes('\r'));
17+
Assert.assertTrue(isSpecial_doubleQuotes('\b'));
18+
Assert.assertTrue(isSpecial_doubleQuotes('\f'));
19+
Assert.assertTrue(isSpecial_doubleQuotes('\"'));
20+
Assert.assertFalse(isSpecial_doubleQuotes('0'));
21+
Assert.assertTrue(isSpecial_doubleQuotes('\0'));
22+
Assert.assertFalse(isSpecial_doubleQuotes('中'));
23+
Assert.assertFalse(isSpecial_doubleQuotes('中'));
24+
}
25+
26+
public static boolean isSpecial_doubleQuotes(char ch) {
27+
return ch < specicalFlags_doubleQuotes.length && specicalFlags_doubleQuotes[ch] != 0;
2128
}
2229
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.alibaba.json.bvt;
2+
3+
import java.util.Currency;
4+
import java.util.Locale;
5+
6+
import junit.framework.TestCase;
7+
8+
import com.alibaba.fastjson.JSON;
9+
10+
public class CurrencyTest_2 extends TestCase {
11+
12+
public void test_0() throws Exception {
13+
VO vo = new VO();
14+
vo.setValue(Currency.getInstance(Locale.CHINA));
15+
vo.setValue1(Currency.getInstance(Locale.CHINA));
16+
String text = JSON.toJSONString(vo);
17+
System.out.println(text);
18+
JSON.parseObject(text, VO.class);
19+
}
20+
21+
public static class VO {
22+
23+
private Currency value;
24+
private Currency value1;
25+
26+
public Currency getValue() {
27+
return value;
28+
}
29+
30+
public void setValue(Currency value) {
31+
this.value = value;
32+
}
33+
34+
public Currency getValue1() {
35+
return value1;
36+
}
37+
38+
public void setValue1(Currency value1) {
39+
this.value1 = value1;
40+
}
41+
42+
}
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.alibaba.json.bvt.serializer;
2+
3+
import java.util.Collections;
4+
5+
import com.alibaba.fastjson.JSON;
6+
7+
import junit.framework.TestCase;
8+
9+
public class UnicodeTest extends TestCase {
10+
public void test_unicode() throws Exception {
11+
String text = JSON.toJSONString(Collections.singletonMap("v", "\u0018"));
12+
System.out.println(text);
13+
}
14+
}

0 commit comments

Comments
 (0)