改造:2.0.0-beta2,JacksonConverter与Joda DateTime

时间:2015-12-29 07:28:23

标签: android jackson jodatime retrofit2

我试图将Retrofit2与Jackson转换器和Joda DateTime一起使用。我的代码如下:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
retrofit = new Retrofit.Builder()
        .baseUrl(RestServiceApi.BASE_URL)
        .addConverterFactory(JacksonConverterFactory.create(mapper))
        .client(okHttpClient)
        .build();

然而,未使用Joda序列化器/解串器导致

{
    ...........
    "DateTime": {
         "centuryOfEra": 20,
         "dayOfMonth": 29,
         "dayOfWeek": 2,
         "dayOfYear": 363,
         "era": 1,
         "hourOfDay": 8,
         .....
    }
 }

使用没有ObjectMapper的JacksonConverterFactory.create()时可以获得相同的结果

   .addConverterFactory(JacksonConverterFactory.create())

如果直接使用相同的ObjectMapper将值写入String,则所有内容都按预期工作。

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    String payload = mapper.writeValueAsString(message);

任何帮助将不胜感激。

修改 依赖关系:

+--- com.squareup.okhttp:okhttp:2.7.0
|    \--- com.squareup.okio:okio:1.6.0
+--- com.squareup.okhttp:logging-interceptor:2.7.0
|    \--- com.squareup.okhttp:okhttp:2.7.0 (*)
+--- com.squareup.retrofit:retrofit:2.0.0-beta2
|    \--- com.squareup.okhttp:okhttp:2.5.0 -> 2.7.0 (*)
+--- com.squareup.retrofit:converter-jackson:2.0.0-beta2
|    +--- com.squareup.retrofit:retrofit:2.0.0-beta2 (*)
|    \--- com.fasterxml.jackson.core:jackson-databind:2.4.3
|         +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0
|         \--- com.fasterxml.jackson.core:jackson-core:2.4.3
+--- com.fasterxml.jackson.datatype:jackson-datatype-joda:2.4.3
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0
|    +--- com.fasterxml.jackson.core:jackson-core:2.4.3
|    +--- com.fasterxml.jackson.core:jackson-databind:2.4.3 (*)
|    \--- joda-time:joda-time:2.2 -> 2.3
+--- com.box:json-schema-validator:2.2.10
|    +--- joda-time:joda-time:2.3

Edit2:我删除了joda-time:2.3依赖,没有结果。

1 个答案:

答案 0 :(得分:0)

即使使用Retrofit2.0-beta3,我也无法使上面的代码正常工作。

我最终在DateTime类型的每个字段上使用显式序列化器/反序列化器:

@JsonProperty("DateTime")
@NotNull
@JsonDeserialize(using = CustomDateTimeDeserializer.class)
@JsonSerialize(using = CustomDateTimeSerializer.class)
private Date DateTime;

串行器代码:

public class CustomDateTimeSerializer extends StdScalarSerializer<DateTime> {

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

@Override
public void serialize(DateTime dateTime,
                      JsonGenerator jsonGenerator,
                      SerializerProvider provider) throws IOException, JsonGenerationException {
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(DateTimeZone.forID("Europe/London"));
    String dateTimeAsString = fmt.print(dateTime);
    jsonGenerator.writeString(dateTimeAsString);
}

}

反序列化代码:

public class CustomDateTimeDeserializer extends StdScalarDeserializer<DateTime> {

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

@Override
public DateTime deserialize(JsonParser jsonParser,
                            DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonToken currentToken = jsonParser.getCurrentToken();
    if (currentToken == JsonToken.VALUE_STRING) {
        String dateTimeAsString = jsonParser.getText().trim();
        DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(DateTimeZone.forID("Europe/London"));
        DateTime result = fmt.parseDateTime(dateTimeAsString);
        return result;
    }
    throw deserializationContext.mappingException(getValueClass());
}

}