use of com.developmentontheedge.be5.api.impl.model.Base64File in project be5 by DevelopmentOnTheEdge.
the class ParseRequestUtils method getValuesFromJson.
public static Map<String, Object> getValuesFromJson(String valuesString) throws Be5Exception {
if (Strings.isNullOrEmpty(valuesString)) {
return Collections.emptyMap();
}
Map<String, Object> fieldValues = new HashMap<>();
// todo gson -> json-b
// InputStream stream = new ByteArrayInputStream(valuesString.getBytes(StandardCharsets.UTF_8.name()));
// javax.json.stream.JsonParser parser = Json.createParser(stream);
//
// javax.json.JsonObject object = parser.getObject();
// Set<Map.Entry<String, JsonValue>> entries = object.entrySet();
JsonObject values = (JsonObject) new JsonParser().parse(valuesString);
for (Map.Entry entry : values.entrySet()) {
String name = entry.getKey().toString();
if (entry.getValue() instanceof JsonNull) {
fieldValues.put(name, null);
} else if (entry.getValue() instanceof JsonArray) {
JsonArray value = (JsonArray) entry.getValue();
String[] arrValues = new String[value.size()];
for (int i = 0; i < value.size(); i++) {
arrValues[i] = value.get(i).getAsString();
}
fieldValues.put(name, arrValues);
} else if (entry.getValue() instanceof JsonObject) {
JsonObject jsonObject = ((JsonObject) entry.getValue());
String type = jsonObject.get("type").getAsString();
if ("Base64File".equals(type)) {
try {
String data = jsonObject.get("data").getAsString();
String base64 = ";base64,";
int base64Pos = data.indexOf(base64);
String mimeTypes = data.substring("data:".length(), base64Pos);
byte[] bytes = data.substring(base64Pos + base64.length(), data.length()).getBytes("UTF-8");
byte[] decoded = Base64.getDecoder().decode(bytes);
fieldValues.put(name, new Base64File(jsonObject.get("name").getAsString(), decoded, mimeTypes));
} catch (UnsupportedEncodingException e) {
throw Be5Exception.internal(e);
}
} else {
fieldValues.put(name, jsonObject.toString());
}
} else if (entry.getValue() instanceof JsonPrimitive) {
fieldValues.put(name, ((JsonPrimitive) entry.getValue()).getAsString());
}
}
return replaceEmptyStringToNull(fieldValues);
}
Aggregations