java - 避免使用代表 JSON 的 POJO 的样板代码

标签 java json oop auto-value

我正在创建一个包含许多不同 NetworkMessages 的库,这些消息通常采用 JSON 格式,现在需要一个相应的 Java 模型。

问题在于,这些消息在 JSON 中可以轻松包含大约 100 个字段。其中一些是强制 (30%),一些是可选 (70%)

所以我最关心的是如何最小化相应模型中的样板代码。因为,正如我所说,这些 POJO 可以轻松拥有大约 100 个字段、许多 getter,以及构造函数中的许多字段。

我将给出一个小消息的示例,但请记住,消息通常要大得多(更多字段)。

MessageA.java

@JsonInclude(Include.NON_EMPTY)
public class MessageA extends NetworkMessage {

    private final String a;

    private final String b;

    private final String c;

    private final String d;

    private final String e;

    private final String f;

    private final String g;

    private final String h;

    private final String i;

    private final String j;

    private final String k;

    @JsonCreator
    private MessageA(
            // required fields
            @JsonProperty(value = "a", required = true) String a, 
            @JsonProperty(value = "b", required = true) String b,
            @JsonProperty(value = "c", required = true) String c,
            @JsonProperty(value = "d", required = true) String d, 
            @JsonProperty(value = "e", required = true) String e, 
            @JsonProperty(value = "f", required = true) String f,

            // optional fields
            @JsonProperty(value = "g") String g, 
            @JsonProperty(value = "h") String h, 
            @JsonProperty(value = "i") String i, 
            @JsonProperty(value = "j") String j,
            @JsonProperty(value = "k") String k) {

        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f = f;
        this.g = g;
        this.h = h;
        this.i = i;
        this.j = j;
        this.k = k;
    }

    public Optional<String> getG() {
        return Optional.ofNullable(g);
    }

    public Optional<String> getH() {
        return Optional.ofNullable(h);
    }

    public Optional<String> getI() {
        return Optional.ofNullable(i);
    }

    public Optional<MessageType> getJ() {
        return Optional.ofNullable(j);
    }

    public Optional<String> getK() {
        return Optional.ofNullable(k);
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public String getC() {
        return c;
    }

    public String getD() {
        return d;
    }

    public String getE() {
        return e;
    }

    public String getF() {
        return f;
    }
}

现在,我尝试使用 Google 的 AutoValue 库来解决其中的一些问题,代码看起来好一些,但仍然调用了具有许多字段的构造函数。

MessageA.java

@AutoValue
@JsonInclude(Include.NON_EMPTY)
public abstract class MessageA extends NetworkMessage {

    // required fields
    @Nonnull public abstract String getFieldA();

    @Nonnull public abstract String getFieldB();

    @Nonnull public abstract String getFieldC();

    @Nonnull public abstract String getFieldD();

    @Nonnull public abstract String getFieldE();

    @Nonnull public abstract String getFieldF();

    // optional fields
    @Nullable public abstract String getFieldG();

    @Nullable public abstract String getFieldH();

    @Nullable public abstract String getFieldI();

    @Nullable public abstract String getFieldJ();

    @Nullable public abstract String getFieldK();

    @JsonCreator
    private static MessageA create(
              // required fields
              @JsonProperty(value = "a", required = true) String a,
              @JsonProperty(value = "b", required = true) String b,
            @JsonProperty(value = "c", required = true) String c,
            @JsonProperty(value = "d", required = true) String d,
            @JsonProperty(value = "e", required = true) String e,
            @JsonProperty(value = "f", required = true) String f,

            // optional fields
            @JsonProperty(value = "g") String g,
            @JsonProperty(value = "h") String h,
            @JsonProperty(value = "i") String i,
            @JsonProperty(value = "j") String j,
            @JsonProperty(value = "k") String k) {

        return new AutoValue_MessageA(
                a, b, c, d, e, f, g, h, I, j, k);
    }
}

现在好多了,但是有一个问题,我不能有可选返回类型,所以我的代码中可能会 float 空值,并且应该在其他地方执行大量空检查。

您的建议是什么?采取哪种方式?

最佳答案

如果您确实想要/需要将这些 Json 消息表示为 POJO,那么您至少可以通过仅定义所需的字段并使用 Lombok 来绕过所有样板 getter/setter/Ctor 噪音。用于生成 getter/setter/Ctor。 通过使用 @Data @Data 注释该类您将免费获得 getters/setter/ctor,而无需看到涉及的样板文件。

@Data
public class MessageA extends NetworkMessage {

private final String a;

将导致一个看起来像这样(以及更多)的类

@Data
public class MessageA extends NetworkMessage {

private final String a;
public MessageA(String a){
  this.a=a;
}
public String getA(){
  return this.a;
}

/*
*No setter since the field is private
*public void setA(String a){
*  this.a=a;
*}

*public boolean equals(Object o){...}
*public String toString(){...}
*/

我一直使用 lombok 来避免一些必要的困惑。

但也许您最好完全利用这些数据类。如此巨大的消息看起来并不代表一个概念,而是代表几个嵌套的概念。否则,只需将 JSON 存储为 JSONObject 并提供直接从 JSON 获取值的 getter(这样您将可以更轻松地配置/验证复杂条件)

关于java - 避免使用代表 JSON 的 POJO 的样板代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47790414/

相关文章:

python - 什么可以从元类实例化

java - 如何访问Android设备上的剪贴板

Java在codechef问题中包含字符串搜索

python - jsonify flask-sqlalchemy flask中的多对一关系

javascript - 下划线 JavaScript 查找复杂对象中键存在的值

c# - 如何在C#中使用Either类型?

java - Rational Appln Deb IBM 7.5.4

java - Android Volley 错误 header 值中的意外字符 0x0a

php - 如何在 symfony 中返回 json 编码格式错误

java - 有条件地实例化不同的类