gpt4 book ai didi

java - ObjectMapper 截断 Map 中的日期

转载 作者:行者123 更新时间:2023-11-30 05:43:25 26 4
gpt4 key购买 nike

我有一个 ObjectMapper,它配置了以下 DateTimeFormatter:

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withZone(ZoneOffset.UTC);

我想将其中包含 OffsetDateTime 的 map 转换为 json。我的问题是,显然 OffsetDateTime 当它是值时会正确转换

Map<Integer, OffsetDateTime> map = ImmutableMap.of(1, offsetDateTime);
System.out.println(mapper.writeValueAsString(map));
// {"1":"2019-03-20T16:46:00.000+0000"}

但不是当它是关键

Map<OffsetDateTime, Integer> map = ImmutableMap.of(offsetDateTime, 1);
System.out.println(mapper.writeValueAsString(map).toString());
// {"2019-03-20T16:46Z":1}

请注意,秒和毫秒被截断。我已经使用其他映射(例如 HashMapTreeMap)对此进行了测试,结果相同。

这是ObjectMapper的定义:

ObjectMapper mapper = new ObjectMapper();

mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(DateConstants.SIMPLE_DATE_FORMATTER);
mapper.registerModule(new JavaTimeModule());

SimpleModule instantModule = new SimpleModule();
instantModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
instantModule.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());

mapper.registerModule(instantModule);
mapper.registerModule(offsetDateTimeModule);

还有这个序列化器:

public class OffsetDateTimeSerializer extends JsonSerializer<OffsetDateTime> {
@Override
public void serialize(OffsetDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
String str = DateConstants.FORMATTER.format(value);
gen.writeString(str);
}
}

在 assylias 回答后编辑:

我现在已将我的 SimpleModule 更改为

SimpleModule instantModule = new SimpleModule();
instantModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
instantModule.addKeyDeserializer(OffsetDateTime.class, new OffsetDateTimeKeyDeserializer());
instantModule.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
instantModule.addKeySerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());
mapper.registerModule(instantModule);

OffsetDateTimeKeyDeserialzer

public class OffsetDateTimeKeyDeserializer extends KeyDeserializer {
@Override
public OffsetDateTime deserializeKey(String key, DeserializationContext ctxt) {
return OffsetDateTime.from(DateConstants.FORMATTER.parse(key));
}
}

OffsetDateTime 为值时,事情会继续正常工作。但是,如果这是关键,我现在明白了

Exception in thread "main" com.fasterxml.jackson.core.JsonGenerationException: Can not write a string, expecting field name (context: Object)
at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1961)
at com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:244)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator._verifyValueWrite(WriterBasedJsonGenerator.java:866)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeString(WriterBasedJsonGenerator.java:368)
at com.brandwatch.signals.commons.util.jackson.OffsetDateTimeSerializer.serialize(OffsetDateTimeSerializer.java:16)
at com.brandwatch.signals.commons.util.jackson.OffsetDateTimeSerializer.serialize(OffsetDateTimeSerializer.java:12)
at com.fasterxml.jackson.databind.ser.std.StdKeySerializers$Dynamic.serialize(StdKeySerializers.java:225)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:707)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:639)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:33)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3905)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3219)

编辑2:

这个KeySerializer似乎对我来说已经成功了:

public class OffsetDateTimeKeySerializer extends StdSerializer<Object> {

public OffsetDateTimeKeySerializer() {
super(Object.class);
}

@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (value instanceof OffsetDateTime) {
String str = DateConstants.FORMATTER.format((OffsetDateTime) value);
gen.writeFieldName(str);
} else {
gen.writeFieldName(value.toString());
}
}
}

最佳答案

正如您所发现的,这仅将(反)序列化器应用于值。要将相同的序列化规则应用于 key ,您需要一个 key (反)序列化器:

instantModule.addKeyDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
instantModule.addKeySerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());

请注意, key 反序列化器需要一个 KeyDeserializer 对象,而不是 JsonDeserializer

更多信息请参见 the javadoc .

关于java - ObjectMapper 截断 Map 中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com