简体   繁体   English

Jackson JSON没有正确地序列化Joda DateTime

[英]Jackson JSON not serializing Joda DateTime correctly

I have a Joda DateTime in an Order class: 我在Order类中有一个Joda DateTime:

public class Order {
    private DateTime creationTime;
    ...
}

I have initialized my mapper as follows: 我已经初始化了我的mapper,如下所示:

mapper.configure(
        SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

When I serialize this class, I expect to see creationTime serialized in the ISO-8601 format as follows 当我序列化这个类时,我希望看到以ISO-8601格式序列化的creationTime如下

{
  "creationTime" : "2011-01-01T09:00:00.000-04:00"
}

This is working perfectly fine in my unit test. 这在我的单元测试中工作得非常好。 However, in my web application, the exact same code is serializing all the DateTime fields: 但是,在我的Web应用程序中,完全相同的代码序列化所有DateTime字段:

{
  "creationTime" : {
    "year" : 2011,
    "dayOfMonth" : 17,
    "dayOfWeek" : 7,
    "era" : 1,
    "dayOfYear" : 107,
    "weekOfWeekyear" : 15,
    "weekyear" : 2011,
    "monthOfYear" : 4,
    "yearOfEra" : 2011,
    "yearOfCentury" : 11,
    "centuryOfEra" : 20,
    "millisOfSecond" : 590,
    "millisOfDay" : 40311590,
    "secondOfMinute" : 51,
    "secondOfDay" : 40311,
    "minuteOfHour" : 11,
    "minuteOfDay" : 671,
    "hourOfDay" : 11,
    "millis" : 1303053111590,
    "zone" : {
      "fixed" : false,
      "uncachedZone" : {
        "cachable" : true,
        "fixed" : false,
        "id" : "America/New_York"
      },
      "id" : "America/New_York"
    },
    "chronology" : {
      "zone" : {
        "fixed" : false,
        "uncachedZone" : {
          "cachable" : true,
          "fixed" : false,
          "id" : "America/New_York"
        },
        "id" : "America/New_York"
      }
    }
}

What am I missing? 我错过了什么? I am including jackson-core-asl-1.7.6.jar and jackson-mapper-asl-1.7.6.jar in my classpath in both cases. 在这两种情况下,我都在我的类路径中包含jackson-core-asl-1.7.6.jar和jackson-mapper-asl-1.7.6.jar。

In some online examples I saw an annotation on DateTime. 在一些在线示例中,我在DateTime上看到了一个注释。 I don't know if this is needed, but I tried it nevertheness. 我不知道是否需要这个,但我尝试了它的nevertheness。 See below: 见下文:

public class Order {
    @JsonSerialize(using=DateTimeSerializer.class)
    private DateTime creationTime;
    ...
}

This seems to make no difference. 这似乎没什么区别。

Thanks. 谢谢。

PS Does anyone know if the Jackson mailing list is working? PS有谁知道Jackson邮件列表是否有效? I posted this question on the user mailing list, but it doesn't show in the archives. 我在用户邮件列表中发布了此问题,但它未显示在存档中。 The last post in the archives is dated 24 June 2010. 档案中的最后一篇文章日期为2010年6月24日。

This seemed to do the trick for me: How to serialize Joda DateTime with Jackson JSON processer? 这似乎对我有用如何使用Jackson JSON处理器序列化Joda DateTime?

Basically, the idea was to create a class that extends org.codehaus.jackson.map.JsonSerializer with a overried serialize method: 基本上,我们的想法是创建一个使用overried serialize方法扩展org.codehaus.jackson.map.JsonSerializer的类:

public void serialize(DateTime value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {
    gen.writeString(formatter.print(value));
}

Then just use that custom serializer in place of DateTimeSerializer . 然后只使用该自定义序列化程序代替DateTimeSerializer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM