使用JacksonJsonMap器将LocalDateTime序列化为旧格式

vbopmzt1  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(234)

在更新Jackson库之后,响应发生了变化,以前使用的是Json.toJSON(LocalDateTime.now()),它调用ObjectMapper().valueToTree(data);
我希望视图与以前相同:

"date": {
                "year": 2022,
                "month": "SEPTEMBER",
                "dayOfMonth": 8,
                "dayOfWeek": "THURSDAY",
                "dayOfYear": 251,
                "monthValue": 9,
                "hour": 16,
                "minute": 4,
                "second": 30,
                "nano": 0,
                "chronology": {
                    "calendarType": "iso8601",
                    "id": "ISO"
                }
            }

现在答案看起来像这样:

[2022,9,20,11,28,9,598000000]

在更新库之前,序列化JsonNode中的LocalDateTime对象就足以得到正确的答案,现在我尝试了不同的方法-

final String jsonNode = new ObjectMapper().findAndRegisterModules().writeValueAsString(LocalDateTime.now());
    return ok(Json.toJson(jsonNode));

或者没有Json.toJson(jsonNode),只是:

final String jsonNode = new ObjectMapper().findAndRegisterModules().writeValueAsString(LocalDateTime.now());
    return ok(jsonNode);

或:

final JsonNode jsonNode = new ObjectMapper().registerModule(new JSR310Module()).valueToTree(LocalDateTime.now());
    return ok(Json.toJson(jsonNode));

和各种其他选项,不幸的是,即使不使用Json.toJSON(),这些选项也不能给予预期的效果;
Json.到Json()(代码):

public static JsonNode toJson(final Object data) {
    try {
        return mapper().valueToTree(data);
    } catch(Exception e) {
        throw new RuntimeException(e);
    }
}

我使用Play和sbt进行编译,对我来说,不要仅仅因为更新库而改变整个项目的代码是很重要的,但应该注意的是,我需要更新它们,因为需要添加的一些新模块与旧版本的Jackson不兼容。依赖关系如下所示

libraryDependencies += "com.fasterxml.jackson.dataformat" % "jackson-dataformat-cbor" % "2.12.6"
    libraryDependencies += "com.fasterxml.jackson.datatype" % "jackson-datatype-jdk8" % "2.12.6"
    libraryDependencies += "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.12.6"
    libraryDependencies += "com.fasterxml.jackson.core" % "jackson-annotations" % "2.12.6"
    libraryDependencies += "com.fasterxml.jackson.core" % "jackson-core" % "2.12.6"here

我将非常感谢你的答复,如果有可能做到这一点的话

UPD

我这样解决了这个问题,也许会有用:首先,我创建了自定义序列化程序:

public class LocalDateTimeKeySerializer extends StdSerializer<LocalDateTime> {

public static final LocalDateTimeKeySerializer INSTANCE = new LocalDateTimeKeySerializer();

public LocalDateTimeKeySerializer() {
    super(LocalDateTime.class);
}

@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    gen.writeStartObject();
    gen.writeNumberField("year", value.getYear());
    gen.writeNumberField("monthValue", value.getMonthValue());
    gen.writeStringField("month", value.getMonth().name());
    gen.writeNumberField("dayOfMonth", value.getDayOfMonth());
    gen.writeNumberField("hour", value.getHour());
    gen.writeNumberField("minute", value.getMinute());
    gen.writeNumberField("second", value.getSecond());
    gen.writeNumberField("nano", value.getNano());
    gen.writeStringField("dayOfWeek", value.getDayOfWeek().name());
    gen.writeNumberField("dayOfYear", value.getDayOfYear());
    gen.writeEndObject();
}}

之后,在自定义模块中添加了此序列化程序:

public class JsonCustomModule extends SimpleModule {

public JsonCustomModule() {
    super(PackageVersion.VERSION);
    addSerializer(LocalDateTime.class, LocalDateTimeKeySerializer.INSTANCE);
}}

我希望能对你有所帮助

bnlyeluc

bnlyeluc1#

您需要创建一个自定义的JsonSerializer,将LocalDateTime转换为Calendar,然后序列化它。

public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    Calendar calendar = GregorianCalendar.from(value.atZone(ZoneId.systemDefault()));
    JsonSerializer<Object> calendarSerializer = serializers.findValueSerializer(calendar.getClass());
    calendarSerializer.serialize(calendar, gen, serializers);
}

相关问题