I'm creating a client that can send different requests to a server. Part of my goal is to not have any large dependencies.
A request can look as following:
{
"method": "getUser",
"methodParameters": {
"a": "b",
"c": "d",
"e": "f",
"data": {
"u": "v",
"x": "y"
}
},
"version": "1.3"
}
The content of the data object is different for every type of request method. My question is, what would be the best "Java-way" to build these JSON requests dynamically?
So far I've only come up with a couple alternatives that I don't think is the best way to do it..
Example 1
Just a normal POJO with some set/getters and a SimpleJSON object.
public class MethodOne {
JSONObject data = new JSONObject();
private void setX(String x) {
data.put("x", x);
}
private String getX() {
return (String) data.get("x");
}
private void setY(String y) {
data.put("y", y);
}
private String getY() {
return (String) data.get("y");
}
}
Example 2
public class RequestData {
public JSONObject methodOne(String x, String y) {
JSONObject data = new JSONObject();
data.put("x", x);
data.put("y", y);
return data;
}
public JSONObject methodTwo(String a, String b) {
JSONObject data = new JSONObject();
data.put("a", a);
data.put("b", b);
return data;
}
}
So what do you think. Is any of the above solutions better for the job, or is there a third solution that I have yet to come up with? Thanks.