Search in sources :

Example 1 with JsonNull

use of com.google.gson.JsonNull in project st-js by st-js.

the class JSArrayAdapter method serialize.

/**
 * {@inheritDoc}
 */
@Override
public JsonElement serialize(Array<?> array, Type type, JsonSerializationContext ctx) {
    if (array == null) {
        return new JsonNull();
    }
    JsonArray js = new JsonArray();
    // validate that we have a packed array (no unset elements) and that we do not
    // have any non-array indices. JSON supports none of these features, and toList()
    // detects them and rejects them too.
    List<?> list = array.toList();
    return ctx.serialize(list);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonNull(com.google.gson.JsonNull)

Example 2 with JsonNull

use of com.google.gson.JsonNull in project RxReddit by damien5314.

the class ModReportDeserializer method deserialize.

@Override
public ModReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonArray array = json.getAsJsonArray();
    // Extract text element
    JsonElement textElement = array.get(0);
    String text = textElement instanceof JsonNull ? null : textElement.getAsString();
    // Extract username element
    String username = array.get(1).getAsString();
    return new ModReport(text, username);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ModReport(rxreddit.model.ModReport) JsonNull(com.google.gson.JsonNull)

Example 3 with JsonNull

use of com.google.gson.JsonNull in project RxReddit by damien5314.

the class UserReportDeserializer method deserialize.

@Override
public UserReport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonArray array = json.getAsJsonArray();
    // Extract text element
    JsonElement textElement = array.get(0);
    String text = textElement instanceof JsonNull ? null : textElement.getAsString();
    // Extract quantity element
    int quantity = array.get(1).getAsInt();
    return new UserReport(text, quantity);
}
Also used : JsonArray(com.google.gson.JsonArray) UserReport(rxreddit.model.UserReport) JsonElement(com.google.gson.JsonElement) JsonNull(com.google.gson.JsonNull)

Example 4 with JsonNull

use of com.google.gson.JsonNull 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)

Example 5 with JsonNull

use of com.google.gson.JsonNull in project maple-ir by LLVM-but-worse.

the class JsonTreeReader method peek.

@Override
public JsonToken peek() throws IOException {
    if (stack.isEmpty()) {
        return JsonToken.END_DOCUMENT;
    }
    Object o = peekStack();
    if (o instanceof Iterator) {
        boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
        Iterator<?> iterator = (Iterator<?>) o;
        if (iterator.hasNext()) {
            if (isObject) {
                return JsonToken.NAME;
            } else {
                stack.add(iterator.next());
                return peek();
            }
        } else {
            return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
        }
    } else if (o instanceof JsonObject) {
        return JsonToken.BEGIN_OBJECT;
    } else if (o instanceof JsonArray) {
        return JsonToken.BEGIN_ARRAY;
    } else if (o instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) o;
        if (primitive.isString()) {
            return JsonToken.STRING;
        } else if (primitive.isBoolean()) {
            return JsonToken.BOOLEAN;
        } else if (primitive.isNumber()) {
            return JsonToken.NUMBER;
        } else {
            throw new AssertionError();
        }
    } else if (o instanceof JsonNull) {
        return JsonToken.NULL;
    } else if (o == SENTINEL_CLOSED) {
        throw new IllegalStateException("JsonReader is closed");
    } else {
        throw new AssertionError();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) Iterator(java.util.Iterator) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JsonNull(com.google.gson.JsonNull)

Aggregations

JsonArray (com.google.gson.JsonArray)6 JsonNull (com.google.gson.JsonNull)6 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 Base64File (com.developmentontheedge.be5.api.impl.model.Base64File)1 JsonParser (com.google.gson.JsonParser)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 GraphObject (org.structr.core.GraphObject)1 JsonInputGSONAdapter (org.structr.core.rest.JsonInputGSONAdapter)1 WebSocketMessage (org.structr.websocket.message.WebSocketMessage)1 ModReport (rxreddit.model.ModReport)1 UserReport (rxreddit.model.UserReport)1