Fastjson支持Java8中的LocalDateTime吗?

乐山ing 发布于 2016/05/05 17:11
阅读 2K+
收藏 0

@wenshao 你好,想跟你请教个问题:Fastjson支持Java8中的LocalDateTime吗?或者说要自己写些什么类型处理器才能支持?比如我一个User对象,里面有一个LocalDateTime的属性lastVisit,我试了一下,似乎自动忽略掉该属性了

加载中
0
wenshao
wenshao

fastjson-1.2.11是支持的

0
乐山ing
乐山ing

引用来自“wenshao”的评论

fastjson-1.2.11是支持的

我试了一下

JSON.toJSONStringWithDateFormat(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss.SSS")



这样子调用没法问题。

但是,如果User对象有一个LocalDateTime属性的话,似乎就显示不出来。
比如下面这段代码
 
User user = new User("name","123456","admin","i");
user.setLastVisit(LocalDateTime.now());
System.out.println(JSON.toJSON(user));




返回的信息为
{"password":"123456","userType":"admin","userName":"name","userId":0,"status":"i"}
似乎是把LocalDateTime类型忽略掉了。

0
wenshao
wenshao
有getLastVisit的方法么?
0
乐山ing
乐山ing

引用来自“wenshao”的评论

有getLastVisit的方法么?

有这个方法

User user = new User("name","123456","admin","i");
user.setLastVisit(LocalDateTime.now());
System.out.println(user.getLastVisit());
System.out.println(JSON.toJSON(user));
显示结果是

2016-05-05T17:52:03.386
{"password":"123456","userType":"admin","userName":"name","userId":0,"status":"i"}
User的源代码是

package iue.sitemanagement.domain;

import java.time.LocalDateTime;
import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class User {
	private int userId;
	private String userName;
	private String password;
	private String userType;
	private String status;
	@JSONField (format="yyyy-MM-dd HH:mm:ss")
	private LocalDateTime lastVisit;
	private String lastIp;
	/*@JSONField (format="yyyy-MM-dd HH:mm:ss")
	private Date now;*/
	
	public User() {
	}

	public User(String userName, String password, String userType, String status) {
		this.userName = userName;
		this.password = password;
		this.userType = userType;
		this.status = status;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public LocalDateTime getLastVisit() {
		return lastVisit;
	}

	public void setLastVisit(LocalDateTime lastVisit) {
		this.lastVisit = lastVisit;
	}

	public String getLastIp() {
		return lastIp;
	}

	public void setLastIp(String lastIp) {
		this.lastIp = lastIp;
	}
	
	

	

	@Override
	public String toString() {
		return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", userType=" + userType
				+ ", status=" + status + ", lastVisit=" + lastVisit + ", lastIp=" + lastIp + "]";
	}
	
	
	
	
	
}



0
乐山ing
乐山ing

引用来自“wenshao”的评论

有getLastVisit的方法么?

另外一个奇怪的现象是

List<User> userList = new ArrayList<>();
for (int i = 0; i < 2; i++) {
	User user = new User("name"+i,"123456","admin","i");
	user.setLastVisit(LocalDateTime.now());
	//user.setNow(new java.util.Date());
	userList.add(user);
}
String json = JSON.toJSONString(userList);
System.out.println(json);
输出结果

[{"lastVisit":"2016-05-05T17:55:41.791","password":"123456","status":"i","userId":0,"userName":"name0","userType":"admin"},{"lastVisit":"2016-05-05T17:55:41.791","password":"123456","status":"i","userId":0,"userName":"name1","userType":"admin"}]

也就是确实处理了lastVisit

0
乐山ing
乐山ing

引用来自“wenshao”的评论

有getLastVisit的方法么?

我知道我哪里错了,把toJSONString写成toJSON了

不过另外一个问题是

@JSONField (format="yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastVisit;
这句在序列化的时候似乎不起作用,显示的是

"lastVisit":"2016-05-05T17:58:25.353"

0
tianshuang_me
tianshuang_me
今下午我也遇到这个问题,看样子是调用了LocalDateTime类的toString方法
/**
     * Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30}.
     * <p>
     * The output will be one of the following ISO-8601 formats:
     * <ul>
     * <li>{@code uuuu-MM-dd'T'HH:mm}</li>
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss}</li>
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSS}</li>
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSS}</li>
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li>
     * </ul>
     * The format used will be the shortest that outputs the full value of
     * the time where the omitted parts are implied to be zero.
     *
     * @return a string representation of this date-time, not null
     */
    @Override
    public String toString() {
        return date.toString() + 'T' + time.toString();
    }

不知如何在fastjson中指定json后LocalDateTime的输出格式?



0
wenshao
wenshao
很好的反馈,我将会在下个版本中补上支持
乐山ing
乐山ing
非常感谢
返回顶部
顶部