Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1493 #1507

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public abstract class JSON implements JSONStreamAware, JSONAware {
static final SerializeFilter[] emptyFilters = new SerializeFilter[0];

public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static String DEFFAULT_LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

public static int DEFAULT_PARSER_FEATURE;
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
}
write(out, dateTime, format);
} else {
out.writeString(object.toString());
format = JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT;
write(out, dateTime, format);
}

} else {
out.writeString(object.toString());
}
Expand Down
28 changes: 23 additions & 5 deletions src/test/java/com/alibaba/json/bvt/issue_1400/Issue1493.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,45 @@

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.json.bvt.issue_1500.Issue1500;
import junit.framework.TestCase;
import org.junit.Assert;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Issue1493 extends TestCase {

public void test_for_issue() throws Exception {

TestBean test = new TestBean();
String stime2 = "2017-09-22 15:08:56";

LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = LocalDateTime.parse("2017-09-22 15:08:56", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime time2 = LocalDateTime.parse(stime2, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
test.setTime1(time1);
test.setTime2(time2);
String t1 = time1.toString();

String json = JSON.toJSONString(test, SerializerFeature.WriteDateUseDateFormat);
Assert.assertEquals("{\"time1\":\""+t1+"\",\"time2\":\""+stime2+"\"}",json);


JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
String stime1 = DateTimeFormatter.ofPattern(JSON.DEFFAULT_LOCAL_DATE_TIME_FORMAT).format(time1);

json = JSON.toJSONString(test, SerializerFeature.WriteDateUseDateFormat);
Assert.assertEquals("{\"time1\":\""+stime1+"\",\"time2\":\""+stime2+"\"}",json);


json = JSON.toJSONStringWithDateFormat(test, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
Assert.assertEquals("{\"time1\":\""+stime1+"\",\"time2\":\""+stime2+"\"}",json);

String json = JSON.toJSONStringWithDateFormat(test, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
System.out.println(json);
}

public static class TestBean {
private LocalDateTime time1;
private LocalDateTime time2;
LocalDateTime time1;
LocalDateTime time2;

public LocalDateTime getTime1() {
return time1;
Expand Down