mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Factor out getArray() for easier JSON parsing
This commit is contained in:
@@ -3,6 +3,7 @@ package org.briarproject.bramble.mailbox;
|
|||||||
import com.fasterxml.jackson.core.JacksonException;
|
import com.fasterxml.jackson.core.JacksonException;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||||
@@ -144,10 +145,7 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
if (body == null) throw new ApiException();
|
if (body == null) throw new ApiException();
|
||||||
try {
|
try {
|
||||||
JsonNode node = mapper.readTree(body.string());
|
JsonNode node = mapper.readTree(body.string());
|
||||||
JsonNode contactsNode = node.get("contacts");
|
ArrayNode contactsNode = getArray(node, "contacts");
|
||||||
if (contactsNode == null || !contactsNode.isArray()) {
|
|
||||||
throw new ApiException();
|
|
||||||
}
|
|
||||||
List<ContactId> list = new ArrayList<>();
|
List<ContactId> list = new ArrayList<>();
|
||||||
for (JsonNode contactNode : contactsNode) {
|
for (JsonNode contactNode : contactsNode) {
|
||||||
if (!contactNode.isNumber()) throw new ApiException();
|
if (!contactNode.isNumber()) throw new ApiException();
|
||||||
@@ -183,10 +181,7 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
if (body == null) throw new ApiException();
|
if (body == null) throw new ApiException();
|
||||||
try {
|
try {
|
||||||
JsonNode node = mapper.readTree(body.string());
|
JsonNode node = mapper.readTree(body.string());
|
||||||
JsonNode filesNode = node.get("files");
|
ArrayNode filesNode = getArray(node, "files");
|
||||||
if (filesNode == null || !filesNode.isArray()) {
|
|
||||||
throw new ApiException();
|
|
||||||
}
|
|
||||||
List<MailboxFile> list = new ArrayList<>();
|
List<MailboxFile> list = new ArrayList<>();
|
||||||
for (JsonNode fileNode : filesNode) {
|
for (JsonNode fileNode : filesNode) {
|
||||||
if (!fileNode.isObject()) throw new ApiException();
|
if (!fileNode.isObject()) throw new ApiException();
|
||||||
@@ -250,10 +245,7 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
if (body == null) throw new ApiException();
|
if (body == null) throw new ApiException();
|
||||||
try {
|
try {
|
||||||
JsonNode node = mapper.readTree(body.string());
|
JsonNode node = mapper.readTree(body.string());
|
||||||
JsonNode filesNode = node.get("folders");
|
ArrayNode filesNode = getArray(node, "folders");
|
||||||
if (filesNode == null || !filesNode.isArray()) {
|
|
||||||
throw new ApiException();
|
|
||||||
}
|
|
||||||
List<MailboxId> list = new ArrayList<>();
|
List<MailboxId> list = new ArrayList<>();
|
||||||
for (JsonNode fileNode : filesNode) {
|
for (JsonNode fileNode : filesNode) {
|
||||||
if (!fileNode.isObject()) throw new ApiException();
|
if (!fileNode.isObject()) throw new ApiException();
|
||||||
@@ -298,4 +290,14 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
.addHeader("Authorization", "Bearer " + token);
|
.addHeader("Authorization", "Bearer " + token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* JSON helpers */
|
||||||
|
|
||||||
|
private ArrayNode getArray(JsonNode node, String name) throws ApiException {
|
||||||
|
JsonNode arrayNode = node.get(name);
|
||||||
|
if (arrayNode == null || !arrayNode.isArray()) {
|
||||||
|
throw new ApiException();
|
||||||
|
}
|
||||||
|
return (ArrayNode) arrayNode;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user