Datasets:
ArXiv:
License:
File size: 1,814 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package httpcli.adapter.json;
import httpcli.RequestBody;
import httpcli.adapter.ObjectAdapter;
import httpcli.adapter.ReqBodyAdapter;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
public class ReqBodyJSON implements ReqBodyAdapter<JSON> {
private static final String MEDIA_TYPE = "application/json; charset=UTF-8";
@Override
public RequestBody parse(JSON obj) throws Exception {
String json = obj.toString();
return RequestBody.create(MEDIA_TYPE, json);
}
public static class Obj implements ReqBodyAdapter<JSONObject> {
@Override
public RequestBody parse(JSONObject obj) throws Exception {
String json = obj.toString();
return RequestBody.create(MEDIA_TYPE, json);
}
}
public static class Array implements ReqBodyAdapter<JSONArray> {
@Override
public RequestBody parse(JSONArray obj) throws Exception {
String json = obj.toString();
return RequestBody.create(MEDIA_TYPE, json);
}
}
public static class Any<T> implements ReqBodyAdapter<T> {
public final Class<T> classOf;
public Any(Class<T> classOf) {
this.classOf = classOf;
}
@Override
public RequestBody parse(T obj) throws Exception {
ObjectAdapter<T> adapter = ObjectAdapter.get(classOf);
Map<String, Object> vars = adapter.toMap(obj);
JSONObject json = new JSONObject();
for (Map.Entry<String, Object> entry : vars.entrySet()) {
json.putOpt(entry.getKey(), entry.getValue());
}
String content = json.toString();
return RequestBody.create(MEDIA_TYPE, content);
}
}
}
|