序列化Joda DateTime对象根据上下文创建不同的输出 [英] Serializing Joda DateTime object is creating different output depending on context

查看:56
本文介绍了序列化Joda DateTime对象根据上下文创建不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套了Joda DateTime对象的对象,但是我很难对其进行序列化和反序列化.

I have an object in which is nested a Joda DateTime object, and I'm having trouble serializing it and deserializing it.

当我序列化嵌套DateTime的完整对象时,输出与直接序列化DateTime对象时的输出看起来有很大不同,我的结果在下面

When I serialize the full object that DateTime is nested in, the output looks vastly different than when I serialize the DateTime object directly, my results are below

新的ObjectMapper.writeValueAsString(fullObjectWithDateTime)为DateTime对象生成以下blob:

new ObjectMapper.writeValueAsString(fullObjectWithDateTime) produces the following blob for the DateTime object:

{
    "dateTime": {
        "weekOfWeekyear": 34,
        "weekyear": 2019,
        "yearOfEra": 2019,
        "yearOfCentury": 19,
        "centuryOfEra": 20,
        "secondOfDay": 4530,
        "minuteOfDay": 75,
        "millisOfDay": 4530777,
        "monthOfYear": 8,
        "hourOfDay": 1,
        "minuteOfHour": 15,
        "secondOfMinute": 30,
        "millisOfSecond": 777,
        "year": 2019,
        "dayOfMonth": 21,
        "dayOfWeek": 3,
        "era": 1,
        "dayOfYear": 233,
        "millis": 1566350130777,
        "chronology": {
            "zone": {
                "fixed": true,
                "id": "UTC"
            }
        },
        "zone": {
            "fixed": true,
            "id": "UTC"
        },
        "afterNow": false,
        "beforeNow": true,
        "equalNow": false
    }
}

ObjectMapper.writeValueAsString(fullObjectWithDateTime.getDateTime())

ObjectMapper.writeValueAsString(fullObjectWithDateTime.getDateTime())

产生:

"2019-08-21T01:15:30.777Z"

当我尝试反序列化由ObjectMapper.writeValueAsString(fullObjectWithDateTime)生成的对象时,错误就会出现

The error comes when I try to deserialize the object produced by ObjectMapper.writeValueAsString(fullObjectWithDateTime), where it says

'com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.joda.time.DateTime out of START_OBJECT token'

推荐答案

您需要注册 JodaModule .

示例:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import org.joda.time.DateTime;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.registerModule(new JodaModule());

        String json = mapper.writeValueAsString(new MyModel());
        System.out.println(json);
        MyModel model = mapper.readValue(json, MyModel.class);
        System.out.println(model);
    }
}

class MyModel {
    private DateTime now = DateTime.now();

    public DateTime getNow() {
        return now;
    }

    public void setNow(DateTime now) {
        this.now = now;
    }

    @Override
    public String toString() {
        return "MyModel{" +
                "now=" + now +
                '}';
    }
}

上面的代码显示:

{"now":"2019-09-06T21:58:34.917Z"}
MyModel{now=2019-09-06T21:58:34.917Z}

这篇关于序列化Joda DateTime对象根据上下文创建不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆