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

JSONObject.toJavaObject对于指定日期format解析报错 #3309

Closed
jaemybai opened this issue Jun 29, 2020 · 1 comment
Closed

JSONObject.toJavaObject对于指定日期format解析报错 #3309

jaemybai opened this issue Jun 29, 2020 · 1 comment
Labels
Milestone

Comments

@jaemybai
Copy link

public class Test {
public static void main(String[] args) {

   JSONObject jsonObj = new JSONObject();
    jsonObj.put("formatDate","20200623 15:20:01");
    DateFormatTest dateFormatTest = jsonObj.toJavaObject(DateFormatTest.class);
    System.out.println(JSON.toJSONString(dateFormatTest));
}

}

class DateFormatTest {
@JSONField(format = "yyyyMMdd HH:mm:ss")
private Date formatDate;

    public Date getFormatDate() {
        return formatDate;
    }

    public void setFormatDate(Date formatDate) {
        this.formatDate = formatDate;
    }
}

运行程序报错
Exception in thread "main" com.alibaba.fastjson.JSONException: For input string: "20200623 15:20:01"
at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1505)
at com.alibaba.fastjson.JSONObject.toJavaObject(JSONObject.java:603)
at Test .main(StartProvider.java:30)
Caused by: java.lang.NumberFormatException: For input string: "20200623 15:20:01"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.alibaba.fastjson.util.TypeUtils.castToDate(TypeUtils.java:482)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:1435)
at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1503)

但是这种写法是可以正常运行的,JSON.parseObject(jsonObj.toJSONString(), DateFormatTest.class);
之前升级到1.2.67是支持的,后来升级到1.2.72又不行了

wenshao added a commit that referenced this issue Jun 30, 2020
Fix castToDate with format error #3309
@wenshao wenshao added this to the 1.2.73 milestone Jul 19, 2020
@wenshao wenshao added the bug label Jul 19, 2020
@wenshao
Copy link
Member

wenshao commented Jul 26, 2020

https://github.com/alibaba/fastjson/releases/tag/1.2.73
问题修复,请用新版本

@wenshao wenshao closed this as completed Jul 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants