Search in sources :

Example 6 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)

Example 7 with JsonNull

use of com.google.gson.JsonNull in project structr by structr.

the class WebSocketDataGSONAdapter method deserialize.

@Override
public WebSocketMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    WebSocketMessage webSocketData = new WebSocketMessage();
    if (json instanceof JsonObject) {
        JsonObject root = json.getAsJsonObject();
        JsonObject nodeData = root.getAsJsonObject("data");
        JsonObject relData = root.getAsJsonObject("relData");
        if (root.has("command")) {
            webSocketData.setCommand(root.getAsJsonPrimitive("command").getAsString());
        }
        if (root.has("id")) {
            webSocketData.setId(root.getAsJsonPrimitive("id").getAsString());
        }
        if (root.has("pageId")) {
            webSocketData.setPageId(root.getAsJsonPrimitive("pageId").getAsString());
        }
        if (root.has("sessionId")) {
            JsonPrimitive sessionId = root.getAsJsonPrimitive("sessionId");
            if (sessionId != null) {
                webSocketData.setSessionId(sessionId.getAsString());
            }
        }
        if (root.has("callback")) {
            webSocketData.setCallback(root.getAsJsonPrimitive("callback").getAsString());
        }
        if (root.has("button")) {
            webSocketData.setButton(root.getAsJsonPrimitive("button").getAsString());
        }
        if (root.has("parent")) {
            webSocketData.setParent(root.getAsJsonPrimitive("parent").getAsString());
        }
        if (root.has("view")) {
            webSocketData.setView(root.getAsJsonPrimitive("view").getAsString());
        }
        if (root.has("sort")) {
            webSocketData.setSortKey(root.getAsJsonPrimitive("sort").getAsString());
        }
        if (root.has("order")) {
            webSocketData.setSortOrder(root.getAsJsonPrimitive("order").getAsString());
        }
        if (root.has("pageSize")) {
            webSocketData.setPageSize(root.getAsJsonPrimitive("pageSize").getAsInt());
        }
        if (root.has("page")) {
            webSocketData.setPage(root.getAsJsonPrimitive("page").getAsInt());
        }
        if (nodeData != null) {
            JsonInputGSONAdapter adapter = new JsonInputGSONAdapter();
            for (Entry<String, JsonElement> entry : nodeData.entrySet()) {
                final JsonElement obj = entry.getValue();
                Object value = null;
                if (obj instanceof JsonPrimitive) {
                    value = adapter.fromPrimitive(obj.getAsJsonPrimitive());
                } else if (obj instanceof JsonObject) {
                    value = adapter.deserialize(obj, typeOfT, context);
                } else if (obj instanceof JsonArray) {
                    final JsonArray array = obj.getAsJsonArray();
                    final List list = new LinkedList();
                    for (JsonElement element : array) {
                        if (element.isJsonPrimitive()) {
                            list.add(fromPrimitive((element.getAsJsonPrimitive())));
                        } else if (element.isJsonObject()) {
                            // create map of values
                            list.add(JsonInputGSONAdapter.deserialize(element, context));
                        }
                    }
                    value = list;
                } else if (obj instanceof JsonNull) {
                    value = null;
                } else if (obj != null) {
                    value = obj.getAsString();
                }
                webSocketData.setNodeData(entry.getKey(), value);
            }
        }
        if (relData != null) {
            for (Entry<String, JsonElement> entry : relData.entrySet()) {
                JsonElement obj = entry.getValue();
                if (obj instanceof JsonNull || obj.isJsonNull()) {
                    webSocketData.setRelData(entry.getKey(), null);
                } else {
                    try {
                        webSocketData.setRelData(entry.getKey(), obj.getAsString());
                    } catch (Throwable t) {
                        webSocketData.setRelData(entry.getKey(), null);
                    }
                }
            }
        }
    }
    return webSocketData;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) LinkedList(java.util.LinkedList) JsonNull(com.google.gson.JsonNull) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonInputGSONAdapter(org.structr.core.rest.JsonInputGSONAdapter) JsonObject(com.google.gson.JsonObject) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) List(java.util.List) WebSocketMessage(org.structr.websocket.message.WebSocketMessage)

Aggregations

JsonArray (com.google.gson.JsonArray)7 JsonNull (com.google.gson.JsonNull)7 JsonObject (com.google.gson.JsonObject)4 JsonPrimitive (com.google.gson.JsonPrimitive)4 JsonElement (com.google.gson.JsonElement)3 List (java.util.List)2 Base64File (com.developmentontheedge.be5.api.impl.model.Base64File)1 JsonParser (com.google.gson.JsonParser)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 DataHandler (javax.activation.DataHandler)1 SBase (org.bimserver.shared.meta.SBase)1 SClass (org.bimserver.shared.meta.SClass)1 SField (org.bimserver.shared.meta.SField)1