use of com.dtflys.forest.http.body.ByteArrayRequestBody in project forest by dromara.
the class ForestJsonConverter method encodeRequestBody.
@Override
default byte[] encodeRequestBody(ForestBody body, Charset charset) {
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
List<ForestRequestBody> bodyList = new LinkedList(body);
if (!bodyList.isEmpty()) {
Object toJsonObj = bodyList;
if (bodyList.size() == 1) {
toJsonObj = bodyList.get(0);
} else {
Map<String, Object> jsonMap = null;
List jsonArray = null;
for (ForestRequestBody bodyItem : bodyList) {
if (bodyItem instanceof NameValueRequestBody) {
if (jsonMap == null) {
jsonMap = new LinkedHashMap<>(bodyList.size());
}
jsonMap.put(((NameValueRequestBody) bodyItem).getName(), ((NameValueRequestBody) bodyItem).getValue());
} else if (bodyItem instanceof StringRequestBody) {
String content = bodyItem.toString();
Map subMap = this.convertObjectToMap(content);
if (subMap != null) {
if (jsonMap == null) {
jsonMap = new LinkedHashMap<>(bodyList.size());
}
jsonMap.putAll(subMap);
} else {
if (jsonArray == null) {
jsonArray = new LinkedList<>();
}
jsonArray.add(content);
}
} else if (bodyItem instanceof ObjectRequestBody) {
Object obj = ((ObjectRequestBody) bodyItem).getObject();
if (obj == null) {
continue;
}
if (obj instanceof List) {
if (jsonArray == null) {
jsonArray = new LinkedList();
}
jsonArray.addAll((List) obj);
} else {
Map subMap = this.convertObjectToMap(obj);
if (subMap == null) {
continue;
}
if (jsonMap == null) {
jsonMap = new LinkedHashMap<>(bodyList.size());
}
jsonMap.putAll(subMap);
}
}
}
if (jsonMap != null) {
toJsonObj = jsonMap;
} else if (jsonArray != null) {
toJsonObj = jsonArray;
}
}
String text = null;
if (toJsonObj instanceof CharSequence || toJsonObj instanceof StringRequestBody) {
text = toJsonObj.toString();
return text.getBytes(charset);
} else if (toJsonObj instanceof ObjectRequestBody) {
text = this.encodeToString(((ObjectRequestBody) toJsonObj).getObject());
return text.getBytes(charset);
} else if (toJsonObj instanceof NameValueRequestBody) {
Map<String, Object> subMap = new HashMap<>(1);
subMap.put(((NameValueRequestBody) toJsonObj).getName(), ((NameValueRequestBody) toJsonObj).getValue());
text = this.encodeToString(subMap);
return text.getBytes(charset);
} else if (toJsonObj instanceof ByteArrayRequestBody) {
byte[] bytes = ((ByteArrayRequestBody) toJsonObj).getByteArray();
return bytes;
} else {
text = this.encodeToString(toJsonObj);
return text.getBytes(charset);
}
}
return new byte[0];
}
Aggregations