UrlParams.Builder paramsBuilder = new UrlParams.Builder(spec);
if (spec.hasParamsObject()) {
Type paramType = spec.getType();
FieldNavigator navigator = new FieldNavigator(paramType);
StringWriter json = new StringWriter();
final JsonWriter jsonWriter = new JsonWriter(json);
jsonWriter.beginObject();
ValueReceiver receiver = new ValueReceiver() {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void put(String name, Type type, Object value) throws IOException {
jsonWriter.name(name);
if (type instanceof TypeVariable || type == Object.class) {
// We can not use Gson to extract the value, so just use the specified value.
jsonWriter.value((String)value);
} else {
TypeAdapter adapter = gson.getAdapter(TypeToken.get(type));
adapter.write(jsonWriter, value);
}
}
};
for (Field f : navigator.getFields()) {
extractUrlParam(f.getName(), f.getGenericType(), requestParams, receiver);
}
jsonWriter.endObject();
jsonWriter.close();
Object obj = gson.fromJson(json.toString(), paramType);
paramsBuilder = new UrlParams.Builder(spec, obj);
} else {
paramsBuilder = new UrlParams.Builder(spec);
}