Skip to content

Commit 7b56d43

Browse files
committed
add testcase & bug fixed
1 parent 2fd7b25 commit 7b56d43

File tree

13 files changed

+461
-43
lines changed

13 files changed

+461
-43
lines changed

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

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -661,38 +661,78 @@ public final String scanSymbol(final SymbolTable symbolTable, final char quote)
661661
chLocal = charAt(++bp);
662662

663663
switch (chLocal) {
664-
case '"':
665-
hash = 31 * hash + (int) '"';
666-
putChar('"');
664+
case '0':
665+
hash = 31 * hash + (int) chLocal;
666+
putChar('\0');
667667
break;
668-
case '\\':
669-
hash = 31 * hash + (int) '\\';
670-
putChar('\\');
668+
case '1':
669+
hash = 31 * hash + (int) chLocal;
670+
putChar('\1');
671671
break;
672-
case '/':
673-
hash = 31 * hash + (int) '/';
674-
putChar('/');
672+
case '2':
673+
hash = 31 * hash + (int) chLocal;
674+
putChar('\2');
675675
break;
676-
case 'b':
676+
case '3':
677+
hash = 31 * hash + (int) chLocal;
678+
putChar('\3');
679+
break;
680+
case '4':
681+
hash = 31 * hash + (int) chLocal;
682+
putChar('\4');
683+
break;
684+
case '5':
685+
hash = 31 * hash + (int) chLocal;
686+
putChar('\5');
687+
break;
688+
case '6':
689+
hash = 31 * hash + (int) chLocal;
690+
putChar('\6');
691+
break;
692+
case '7':
693+
hash = 31 * hash + (int) chLocal;
694+
putChar('\7');
695+
break;
696+
case 'b': // 8
677697
hash = 31 * hash + (int) '\b';
678698
putChar('\b');
679699
break;
680-
case 'f':
681-
case 'F':
682-
hash = 31 * hash + (int) '\f';
683-
putChar('\f');
700+
case 't': // 9
701+
hash = 31 * hash + (int) '\t';
702+
putChar('\t');
684703
break;
685-
case 'n':
704+
case 'n': // 10
686705
hash = 31 * hash + (int) '\n';
687706
putChar('\n');
688707
break;
689-
case 'r':
708+
case 'v': // 11
709+
hash = 31 * hash + (int) '\u000B';
710+
putChar('\u000B');
711+
break;
712+
case 'f': // 12
713+
case 'F':
714+
hash = 31 * hash + (int) '\f';
715+
putChar('\f');
716+
break;
717+
case 'r': // 13
690718
hash = 31 * hash + (int) '\r';
691719
putChar('\r');
692720
break;
693-
case 't':
694-
hash = 31 * hash + (int) '\t';
695-
putChar('\t');
721+
case '"': // 34
722+
hash = 31 * hash + (int) '"';
723+
putChar('"');
724+
break;
725+
case '\'': // 39
726+
hash = 31 * hash + (int) '\'';
727+
putChar('\'');
728+
break;
729+
case '/': // 47
730+
hash = 31 * hash + (int) '/';
731+
putChar('/');
732+
break;
733+
case '\\': // 92
734+
hash = 31 * hash + (int) '\\';
735+
putChar('\\');
696736
break;
697737
case 'u':
698738
char c1 = chLocal = charAt(++bp);
@@ -797,7 +837,7 @@ public final void scanString() {
797837
if (ch == '\"') {
798838
break;
799839
}
800-
840+
801841
if (ch == EOI) {
802842
throw new JSONException("unclosed string : " + ch);
803843
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ private <T> T toObjectArray(DefaultJSONParser parser, Class<?> componentType, JS
8383
for (int i = 0; i < size; ++i) {
8484
Object value = array.get(i);
8585

86+
if (value == array) {
87+
Array.set(objArray, i, objArray);
88+
continue;
89+
}
90+
8691
if (componentType.isArray()) {
8792
Object element;
8893
if (componentType.isInstance(value)) {
@@ -93,8 +98,28 @@ private <T> T toObjectArray(DefaultJSONParser parser, Class<?> componentType, JS
9398

9499
Array.set(objArray, i, element);
95100
} else {
96-
Object element = TypeUtils.cast(value, componentType, parser.getConfig());
101+
Object element = null;
102+
if (value instanceof JSONArray) {
103+
boolean contains = false;
104+
JSONArray valueArray = (JSONArray) value;
105+
int valueArraySize = valueArray.size();
106+
for (int y = 0; y < valueArraySize; ++y) {
107+
Object valueItem = valueArray.get(y);
108+
if (valueItem == array) {
109+
valueArray.set(i, objArray);
110+
contains = true;
111+
}
112+
}
113+
if (contains) {
114+
element = valueArray.toArray();
115+
}
116+
}
117+
118+
if (element == null) {
119+
element = TypeUtils.cast(value, componentType, parser.getConfig());
120+
}
97121
Array.set(objArray, i, element);
122+
98123
}
99124
}
100125

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.lang.reflect.Type;
2020
import java.text.DateFormat;
21+
import java.text.SimpleDateFormat;
2122
import java.util.Calendar;
2223
import java.util.Date;
2324

@@ -59,6 +60,9 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
5960

6061
if (out.isEnabled(SerializerFeature.WriteDateUseDateFormat)) {
6162
DateFormat format = serializer.getDateFormat();
63+
if (format == null) {
64+
format = new SimpleDateFormat(JSON.DEFFAULT_DATE_FORMAT);
65+
}
6266
String text = format.format(date);
6367
out.writeString(text);
6468
return;

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class JSONSerializer {
5656
private int indentCount = 0;
5757
private String indent = "\t";
5858

59-
private String dateFormatPattern = JSON.DEFFAULT_DATE_FORMAT;
59+
private String dateFormatPattern;
6060
private DateFormat dateFormat;
6161

6262
private IdentityHashMap<Object, SerialContext> references = null;
@@ -93,7 +93,9 @@ public String getDateFormatPattern() {
9393

9494
public DateFormat getDateFormat() {
9595
if (dateFormat == null) {
96-
dateFormat = new SimpleDateFormat(dateFormatPattern);
96+
if (dateFormatPattern != null) {
97+
dateFormat = new SimpleDateFormat(dateFormatPattern);
98+
}
9799
}
98100

99101
return dateFormat;
@@ -367,7 +369,11 @@ public final void writeWithFieldName(Object object, Object fieldName, Type field
367369

368370
public final void writeWithFormat(Object object, String format) {
369371
if (object instanceof Date) {
370-
String text = new SimpleDateFormat(format).format((Date) object);
372+
DateFormat dateFormat = this.getDateFormat();
373+
if (dateFormat == null) {
374+
dateFormat = new SimpleDateFormat(format);
375+
}
376+
String text = dateFormat.format((Date) object);
371377
out.writeString(text);
372378
return;
373379
}

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ public class ObjectArraySerializer implements ObjectSerializer {
2828
public ObjectArraySerializer(){
2929
}
3030

31-
public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
31+
public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
32+
throws IOException {
3233
SerializeWriter out = serializer.getWriter();
3334

3435
Object[] array = (Object[]) object;
35-
36+
3637
if (object == null) {
3738
if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
3839
out.write("[]");
@@ -41,7 +42,7 @@ public final void write(JSONSerializer serializer, Object object, Object fieldNa
4142
}
4243
return;
4344
}
44-
45+
4546
int size = array.length;
4647

4748
int end = size - 1;
@@ -53,12 +54,12 @@ public final void write(JSONSerializer serializer, Object object, Object fieldNa
5354

5455
SerialContext context = serializer.getContext();
5556
serializer.setContext(context, object, fieldName);
56-
57+
5758
try {
5859
Class<?> preClazz = null;
5960
ObjectSerializer preWriter = null;
6061
out.append('[');
61-
62+
6263
if (out.isEnabled(SerializerFeature.PrettyFormat)) {
6364
serializer.incrementIndent();
6465
serializer.println();
@@ -74,34 +75,41 @@ public final void write(JSONSerializer serializer, Object object, Object fieldNa
7475
out.write(']');
7576
return;
7677
}
77-
78+
7879
for (int i = 0; i < end; ++i) {
7980
Object item = array[i];
80-
81+
8182
if (item == null) {
8283
out.append("null,");
8384
} else {
84-
Class<?> clazz = item.getClass();
85-
86-
if (clazz == preClazz) {
87-
preWriter.write(serializer, item, null, null);
85+
if (serializer.containsReference(item)) {
86+
serializer.writeReference(item);
8887
} else {
89-
preClazz = clazz;
90-
preWriter = serializer.getObjectWriter(clazz);
91-
92-
preWriter.write(serializer, item, null, null);
88+
Class<?> clazz = item.getClass();
89+
90+
if (clazz == preClazz) {
91+
preWriter.write(serializer, item, null, null);
92+
} else {
93+
preClazz = clazz;
94+
preWriter = serializer.getObjectWriter(clazz);
95+
96+
preWriter.write(serializer, item, null, null);
97+
}
9398
}
94-
9599
out.append(',');
96100
}
97101
}
98-
102+
99103
Object item = array[end];
100-
104+
101105
if (item == null) {
102106
out.append("null]");
103107
} else {
104-
serializer.write(item);
108+
if (serializer.containsReference(item)) {
109+
serializer.writeReference(item);
110+
} else {
111+
serializer.write(item);
112+
}
105113
out.append(']');
106114
}
107115
} finally {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.alibaba.json.bvt;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
import junit.framework.TestCase;
7+
8+
import org.junit.Assert;
9+
10+
import com.alibaba.fastjson.JSON;
11+
import com.alibaba.fastjson.annotation.JSONField;
12+
13+
public class DateFieldTest8 extends TestCase {
14+
15+
public void test_0() throws Exception {
16+
Entity object = new Entity();
17+
object.setValue(new Date());
18+
String text = JSON.toJSONStringWithDateFormat(object, "yyyy");
19+
Assert.assertEquals("{\"value\":\"" + new SimpleDateFormat("yyyy").format(object.getValue()) + "\"}",
20+
text);
21+
}
22+
23+
public void test_1() throws Exception {
24+
Entity object = new Entity();
25+
object.setValue(new Date());
26+
String text = JSON.toJSONString(object);
27+
Assert.assertEquals("{\"value\":\"" + new SimpleDateFormat("yyyy-MM-dd").format(object.getValue()) + "\"}",
28+
text);
29+
}
30+
31+
public static class Entity {
32+
33+
@JSONField(format = "yyyy-MM-dd")
34+
private Date value;
35+
36+
public Date getValue() {
37+
return value;
38+
}
39+
40+
public void setValue(Date value) {
41+
this.value = value;
42+
}
43+
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.alibaba.json.bvt.bug;
2+
3+
import junit.framework.TestCase;
4+
5+
import org.junit.Assert;
6+
7+
import com.alibaba.fastjson.JSONObject;
8+
9+
public class Bug_for_gongwenhua extends TestCase {
10+
11+
public void test_0() throws Exception {
12+
String text = "{\"FH2\\\"\u0005\\v\u0010\u000e\u0011\u0000\":0,\"alipa9_login\":0,\"alipay_login\":14164,\"durex\":317,\"intl.datasky\":0,\"taobao_refund\":880}";
13+
14+
JSONObject obj = JSONObject.parseObject(text);
15+
Assert.assertNotNull(obj);
16+
Assert.assertEquals(0, obj.get("FH2\"\u0005\u000B\u0010\u000e\u0011\u0000"));
17+
18+
}
19+
20+
}

0 commit comments

Comments
 (0)