package com.alibaba.fastjson2.v1issues.issue_2200;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import java.util.Calendar;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2241 {
    @Test
    public void test_for_issue() {
        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);
    }

    @Test
    public void test_for_issue2() {
        String text = "{\"createTime\":1548166745}";

        Order2 o = JSON.parseObject(text, Order2.class);
        assertEquals(1548166745000L, o.createTime.getTimeInMillis());

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

    @Test
    public void test_for_issue3() {
        String text = "{\"createTime\":\"20180714224948\"}";

        Order3 o = JSON.parseObject(text, Order3.class);
        assertEquals(1531579788000L, o.createTime.getTimeInMillis());

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

    public static class Order {
        @JSONField(format = "unixtime")
        public Date createTime;
    }

    public static class Order2 {
        @JSONField(format = "unixtime")
        public Calendar createTime;
    }

    public static class Order3 {
        @JSONField(format = "yyyyMMddHHmmss")
        public Calendar createTime;
    }
}
