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(Class<T> clazz)方法不支持日期格式的unixtime #2300

Closed
QinRoc opened this issue Feb 25, 2019 · 1 comment
Closed

Comments

@QinRoc
Copy link

QinRoc commented Feb 25, 2019

Fastjson版本:1.2.56

@wenshao 你好,1.2.56版本中针对issue #2241 中的问题的改动,在使用JSON.parseObject(String text, Class clazz)方法时是有效的,但是将JSON字符串先转换成JSONObject对象,再调用toJavaObject方法,获取的对象的Date成员变量就是错误的了。

  public void test_for_issue() throws Exception {
    String text = "{\"createTime\":1548166745}";

    Order o = JSON.parseObject(text, Order.class);
    assertEquals(1548166745000L, o.createTime.getTime());

    String json = JSON.toJSONString(o);
    assertEquals("{\"createTime\":1548166745}", json);

    //新增校验1
    JSONObject jsonObject = JSONObject.parseObject(text);
    Order order1 = JSONObject.toJavaObject(jsonObject, Order.class);
    //校验不通过
    assertEquals(1548166745000L, order1.createTime.getTime());

    //新增校验2
    Order order2 = jsonObject.toJavaObject(Order.class);
    //校验不通过
    assertEquals(1548166745000L, order2.createTime.getTime());

  }

  public static class Order {
    @JSONField(format = "unixtime")
    public Date createTime;
  }
@Omega-Ariston
Copy link
Collaborator

TypeUtil类中的

       if(value instanceof Number){
            longValue = ((Number) value).longValue();
            return new Date(longValue); // 此处需要针对unixtime的format进行特殊处理
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants