2021-01-25 LocalDateTime与fastJson,jackson

字符串转对象时:

在"yyyy-MM-dd hh:mm:ss"格式下
fastJson 可以自动解析LocaclDateTime
Jackson 不可以自动解析LocaclDateTime
需要@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")以及 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using=MyJsonDeserialize.class)


在时间戳格式下:

fastJson 不可以自动解析LocaclDateTime
jackson 不可以自动解析LocaclDateTime
需要 @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using=MyJsonDeserialize.class)


版本:

jackson 2.11.3
fastjson 1.2.40

 

Demo:

{"order_id": 2077,"car_id": 2021,"operator_id": 2031,"deal_time": "2020-12-20 00:00:00","deal_price": 600000,"timestamp": 1608106921
package mypackage;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AccessLevel;

import lombok.Data;

import lombok.experimental.Accessors;

import lombok.experimental.FieldDefaults;

import java.time.LocalDateTime;



@Data

@Accessors(chain = true)

@FieldDefaults(level = AccessLevel.PRIVATE)

public class OrderSuccess {

    /**

     * 订单id

     */

    Long orderId;

    /**

     * 车源id

     */

    Long carId;

    /**

     * 操作人id

     */

    Long operatorId;

    /**

     * 成交时间

     */

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

    @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = MyJsonDeserialize.class)

    LocalDateTime dealTime;

    /**

     * 成交价

     */

    Long dealPrice;

    /**

     * 时间

     */

    @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = MyJsonDeserialize.class)

    LocalDateTime timestamp;

}
    @Test
	void orderSuccessTest() throws JsonProcessingException {
		String string = "{\"order_id\": 2077,\"car_id\": 2021,\"operator_id\": 2031,\"deal_price\": 600000,\"deal_time\": \"2020-12-20 00:00:00\"}";

		ObjectMapper objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
		OrderSuccess success = objectMapper.readValue(string, OrderSuccess.class);
		System.out.println(success);


		OrderSuccess orderSuccess = JSON.parseObject(string, OrderSuccess.class);
		System.out.println(orderSuccess);
}

 

反序列化类

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;

public class MyJsonDeserialize extends LocalDateTimeDeserializer {

    private static final long serialVersionUID = 1L;

    public EpochDeserializer() {
        super(DateTimeFormatter.ISO_DATE_TIME);
    }

    @Override
    public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
            long value = parser.getValueAsLong();
            Instant instant = Instant.ofEpochMilli(value);
            return LocalDateTime.ofInstant(instant, OffsetDateTime.now().getOffset());
        }
        return super.deserialize(parser, context);
    }
}

 

 

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页