Search in sources :

Example 1 with Base64File

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);
}
Also used : HashMap(java.util.HashMap) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonNull(com.google.gson.JsonNull) JsonArray(com.google.gson.JsonArray) Base64File(com.developmentontheedge.be5.api.impl.model.Base64File) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Aggregations

Base64File (com.developmentontheedge.be5.api.impl.model.Base64File)1 JsonArray (com.google.gson.JsonArray)1 JsonNull (com.google.gson.JsonNull)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1