I am creating a library of lots of different NetworkMessages
that are normally in a JSON format, and now a corresponding Java model is needed.
The thing about it is that those messages can easily have about 100 fields in a JSON. Some of them are mandatory (30%), and some of them are optional (70%).
So the big concern that I have, is how to minimise the boiler plate code in the corresponding model. Because, like I said, those POJOs could easily have about 100 fields, and that many getters, and that many fields in a constructor.
I will give an example of one small Message
, but keep in mind the messages are normally significantly bigger (more fields).
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;
}
}
Now, I tried to solve some of that by using Google's AutoValue
library, and the code then looked a little better, but still there's the invocation of a constructor with many, many fields.
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);
}
}
Now this is better, but there's a problem that I can't have Optional return types, so I could have null values floating through my code, and lots of null-checks should be performed in other places.
What would be your advice, which way to go with this?