使用默认的jsonserializer,但使用Snake Case字段名称

发布于 2025-02-12 18:56:46 字数 1387 浏览 0 评论 0原文

我有一个应该序列化到snake_case json的课程。

据我所知,解决此问题的一般方法是注释这样的类别:

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class MyClass

对于具有原始字段名称的简单类,这足以足够。但是,就我而言,myClass包含类型的字段。因此,在输出上,这些类中的字段被序列化为camelcase(因为我无法注释这些字段,因为它们在系统的其他部分中用作骆驼箱 - jsons)。

目前,我正在尝试创建一个自定义序列化器,例如:类:

与snake_case序列化的类:

@JsonSerialize(using = SnakeCaseSerializer.class)
class MyClass { /* ... */ }

自定义序列化器:

public class SnakeCaseSerializer extends JsonSerializer<MyClass> {

    private static final ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
    }

    @Override
    public void serialize(final MyClass value,
                          final JsonGenerator gen,
                          final SerializerProvider serializers) throws IOException {
       // Put serialization logic here...
    }

}

我尝试使用stackoverflow上的其他答案所建议,使用自定义objectmapper>使用propertyNamingStrategy = snake_case,但是我尚未找到一种获取默认实例的方法Snake_case-formatted JSON文件。

假设不能以任何方式对myClass中的字段进行的类别进行注释或修改,什么是我问题的正确解决方案?

附带说明,项目的其余部分使用默认的camelcase序列化,因此在项目级别设置上设置snake_case不合适。

I have a class which should be serialized to snake_case JSON.

General approach to this problem as far as I know, is to annotate the given class like this:

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class MyClass

This would be sufficient for a simple class with primitive field names. However in my case, MyClass contains fields which are class-types. Therefore, on the output, fields within those classes are serialized as camelCase (because I cannot annotate those, since they are used in other parts of the system as camelCase-JSONs).

At the moment, I am attempting to create a custom serializer, like this:

Class to be serialized with snake_case:

@JsonSerialize(using = SnakeCaseSerializer.class)
class MyClass { /* ... */ }

Custom serializer:

public class SnakeCaseSerializer extends JsonSerializer<MyClass> {

    private static final ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
    }

    @Override
    public void serialize(final MyClass value,
                          final JsonGenerator gen,
                          final SerializerProvider serializers) throws IOException {
       // Put serialization logic here...
    }

}

I have attempted, as suggested by other answers on StackOverflow, to use a custom ObjectMapper with propertyNamingStrategy=SNAKE_CASE, however I haven't found a way to get an instance of the default JsonSerializer which would output a snake_case-formatted JSON file.

Under the assumption that classes which are used as fields in MyClass cannot be annotated or modified in any way, what would be the correct solution to my problem?

As a side note, the remaining parts of the project use default camelCase serialization, so setting snake_case on project-level settings is not suitable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文