Skip to content

Commit b21f74b

Browse files
committed
add testcase & remove unused code
1 parent 75a3045 commit b21f74b

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

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

Lines changed: 10 additions & 4 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 dateFormatPatterm = JSON.DEFFAULT_DATE_FORMAT;
59+
private String dateFormatPattern = JSON.DEFFAULT_DATE_FORMAT;
6060
private DateFormat dateFormat;
6161

6262
private IdentityHashMap<Object, SerialContext> references = null;
@@ -85,23 +85,29 @@ public JSONSerializer(SerializeWriter out, SerializeConfig config){
8585
}
8686

8787
public String getDateFormatPattern() {
88-
return dateFormatPatterm;
88+
if (dateFormat instanceof SimpleDateFormat) {
89+
return ((SimpleDateFormat) dateFormat).toPattern();
90+
}
91+
return dateFormatPattern;
8992
}
9093

9194
public DateFormat getDateFormat() {
9295
if (dateFormat == null) {
93-
dateFormat = new SimpleDateFormat(dateFormatPatterm);
96+
dateFormat = new SimpleDateFormat(dateFormatPattern);
9497
}
9598

9699
return dateFormat;
97100
}
98101

99102
public void setDateFormat(DateFormat dateFormat) {
100103
this.dateFormat = dateFormat;
104+
if (dateFormatPattern != null) {
105+
dateFormatPattern = null;
106+
}
101107
}
102108

103109
public void setDateFormat(String dateFormat) {
104-
this.dateFormatPatterm = dateFormat;
110+
this.dateFormatPattern = dateFormat;
105111
if (this.dateFormat != null) {
106112
this.dateFormat = null;
107113
}

src/main/java/com/alibaba/fastjson/util/ASMUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.alibaba.fastjson.util;
22

3-
import java.lang.reflect.Constructor;
43
import java.lang.reflect.Field;
54
import java.lang.reflect.Method;
65
import java.lang.reflect.Type;

src/main/java/com/alibaba/fastjson/util/TypeUtils.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.alibaba.fastjson.JSONObject;
4848
import com.alibaba.fastjson.annotation.JSONField;
4949
import com.alibaba.fastjson.annotation.JSONType;
50+
import com.alibaba.fastjson.parser.JSONScanner;
5051
import com.alibaba.fastjson.parser.ParserConfig;
5152
import com.alibaba.fastjson.parser.deserializer.FieldDeserializer;
5253

@@ -361,7 +362,22 @@ public static final Long castToLong(Object value) {
361362
return null;
362363
}
363364

364-
return Long.parseLong(strVal);
365+
try {
366+
return Long.parseLong(strVal);
367+
} catch (NumberFormatException ex) {
368+
//
369+
}
370+
371+
JSONScanner dateParser = new JSONScanner(strVal);
372+
Calendar calendar = null;
373+
if (dateParser.scanISO8601DateIfMatch(false)) {
374+
calendar = dateParser.getCalendar();
375+
}
376+
dateParser.close();
377+
378+
if (calendar != null) {
379+
return calendar.getTimeInMillis();
380+
}
365381
}
366382

367383
throw new JSONException("can not cast to long, value : " + value);
@@ -731,13 +747,13 @@ public static final <T> T castToJavaBean(Map<String, Object> map, Class<T> clazz
731747
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
732748
new Class<?>[] { clazz }, object);
733749
}
734-
750+
735751
if (mapping == null) {
736752
mapping = ParserConfig.getGlobalInstance();
737753
}
738754

739755
Map<String, FieldDeserializer> setters = mapping.getFieldDeserializers(clazz);
740-
756+
741757
Constructor<T> constructor = clazz.getDeclaredConstructor();
742758
if (!constructor.isAccessible()) {
743759
constructor.setAccessible(true);
@@ -1030,7 +1046,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String, String>
10301046
propertyName = fieldAnnotation.name();
10311047
}
10321048
}
1033-
1049+
10341050
if (aliasMap != null) {
10351051
propertyName = aliasMap.get(propertyName);
10361052
if (propertyName == null) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.alibaba.json.bvt.parser.deser;
2+
3+
import java.text.SimpleDateFormat;
4+
5+
import junit.framework.TestCase;
6+
7+
import org.junit.Assert;
8+
9+
import com.alibaba.fastjson.JSON;
10+
11+
public class DateParseTest10 extends TestCase {
12+
13+
public void test_date() throws Exception {
14+
String text = "{\"value\":\"1979-07-14\"}";
15+
VO vo = JSON.parseObject(text, VO.class);
16+
Assert.assertEquals(vo.getValue(), new SimpleDateFormat("yyyy-MM-dd").parse("1979-07-14").getTime());
17+
}
18+
19+
public static class VO {
20+
21+
private long value;
22+
23+
public long getValue() {
24+
return value;
25+
}
26+
27+
public void setValue(long value) {
28+
this.value = value;
29+
}
30+
31+
}
32+
}

src/test/java/com/alibaba/json/bvt/serializer/JSONSerializerTest3.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ public void test_0() throws Exception {
1515

1616
serializer.setDateFormat("yyyy");
1717
Assert.assertEquals("yyyy", ((SimpleDateFormat) serializer.getDateFormat()).toPattern());
18+
Assert.assertEquals("yyyy", serializer.getDateFormatPattern());
1819

1920
serializer.setDateFormat("yyyy-MM");
2021
Assert.assertEquals("yyyy-MM", ((SimpleDateFormat) serializer.getDateFormat()).toPattern());
22+
23+
serializer.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
24+
Assert.assertEquals("yyyy-MM-dd", serializer.getDateFormatPattern());
2125
}
2226

2327
}

0 commit comments

Comments
 (0)