telaCode
telaCode

Reputation: 199

Exclude SerializedName field but only when its not set

I have an object that I'm serializing to make requests.

private static final String JSON_REQUEST_ID = "requestId";
private static final String JSON_OBJECT_ID = "objectId";
private static final String JSON_GROUP_ID = "groupID";

@SerializedName(JSON_REQUEST_ID)
public String requestId;

@SerializedName(JSON_OBJECT_ID)
public String objectId;

@SerializedName(JSON_GROUP_ID)
public String groupId;

When I send this data off to be processed the objectId and groupId need to be exclusive. So if I set the objectId I must remove the groupId, and vice-versa, from the request.

Is there an easy way to annotate that or should I create a different class for each?

Upvotes: 0

Views: 67

Answers (1)

Frito
Frito

Reputation: 446

Using different classes would make it more obvious, since different outcome (different attributes) mean different json objects.

For "simplification" on your side, you can use the same java class and provide a custom serializer for this.

Upvotes: 1

Related Questions