如何使用Jackson JSON处理器序列化Joda DateTime?

问题

如何根据一个简单的模式(如"dd-MM-yyyy")让杰克逊序列化我的Joda DateTime对象?

我试过了:

@JsonSerialize(using=DateTimeSerializer.class)
private final DateTime date;

我也尝试过:

ObjectMapper mapper = new ObjectMapper()
    .getSerializationConfig()
    .setDateFormat(df);

谢谢!


#1 热门回答(128 赞)

使用Jackson 2.0和Joda模块,这变得非常容易。

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

Maven依赖:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.1.1</version>
</dependency>

代码和文档:https://github.com/FasterXML/jackson-datatype-joda

二进制:http://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-joda/


#2 热门回答(70 赞)

在你要映射的对象中:

@JsonSerialize(using = CustomDateSerializer.class)
public DateTime getDate() { ... }

在CustomDateSerializer中:

public class CustomDateSerializer extends JsonSerializer<DateTime> {

    private static DateTimeFormatter formatter = 
        DateTimeFormat.forPattern("dd-MM-yyyy");

    @Override
    public void serialize(DateTime value, JsonGenerator gen, 
                          SerializerProvider arg2)
        throws IOException, JsonProcessingException {

        gen.writeString(formatter.print(value));
    }
}

#3 热门回答(20 赞)

正如@Kimble所说,使用Jackson 2,使用默认格式非常容易;只需在你的ObjectMapper上注册JodaModule即可。

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

对于DateTime的自定义序列化/反序列化,你需要实现自己的StdScalarSerializerStdScalarDeserializer;它很复杂,但无论如何。

例如,这是aDateTime序列化程序,它使用带有UTC时区的ISODateFormat

public class DateTimeSerializer extends StdScalarSerializer<DateTime> {

    public DateTimeSerializer() {
        super(DateTime.class);
    }

    @Override
    public void serialize(DateTime dateTime,
                          JsonGenerator jsonGenerator,
                          SerializerProvider provider) throws IOException, JsonGenerationException {
        String dateTimeAsString = ISODateTimeFormat.withZoneUTC().print(dateTime);
        jsonGenerator.writeString(dateTimeAsString);
    }
}

和相应的反序列化器:

public class DateTimeDesrializer extends StdScalarDeserializer<DateTime> {

    public DateTimeDesrializer() {
        super(DateTime.class);
    }

    @Override
    public DateTime deserialize(JsonParser jsonParser,
                                DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        try {
            JsonToken currentToken = jsonParser.getCurrentToken();
            if (currentToken == JsonToken.VALUE_STRING) {
                String dateTimeAsString = jsonParser.getText().trim();
                return ISODateTimeFormat.withZoneUTC().parseDateTime(dateTimeAsString);
            }
        } finally {
            throw deserializationContext.mappingException(getValueClass());
        }
    }

然后将这些与模块绑在一起:

public class DateTimeModule extends SimpleModule {

    public DateTimeModule() {
        super();
        addSerializer(DateTime.class, new DateTimeSerializer());
        addDeserializer(DateTime.class, new DateTimeDeserializer());
    }
}

然后在ObjectMapper上注册模块:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new DateTimeModule());