java Jackson JSON 未正确序列化 Joda DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5694678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:21:20  来源:igfitidea点击:

Hymanson JSON not serializing Joda DateTime correctly

javajsonjodatimeHymanson

提问by Naresh

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.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" : "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 Hymanson-core-asl-1.7.6.jar and Hymanson-mapper-asl-1.7.6.jar in my classpath in both cases.

我错过了什么?在这两种情况下,我都在我的类路径中包含 Hymanson-core-asl-1.7.6.jar 和 Hymanson-mapper-asl-1.7.6.jar 。

In some online examples I saw an annotation on DateTime. I don't know if this is needed, but I tried it nevertheness. See below:

在一些在线示例中,我看到了 DateTime 上的注释。我不知道这是否需要,但我从未尝试过。见下文:

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

This seems to make no difference.

这似乎没什么区别。

Thanks.

谢谢。

P.S. Does anyone know if the Hymanson mailing list is working? 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.

PS 有谁知道Hyman逊邮件列表是否有效?我在用户邮件列表上发布了这个问题,但它没有显示在档案中。档案中的最后一篇文章日期为 2010 年 6 月 24 日。

回答by Air Mason

This seemed to do the trick for me: How to serialize Joda DateTime with Hymanson JSON processer?

这似乎对我有用How to serialize Joda DateTime with Hymanson JSON processor?

Basically, the idea was to create a class that extends org.codehaus.Hymanson.map.JsonSerializerwith a overried serialize method:

基本上,这个想法是创建一个类,它扩展org.codehaus.Hymanson.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.