Search in sources :

Example 1 with JsonInputGSONAdapter

use of org.structr.core.rest.JsonInputGSONAdapter in project structr by structr.

the class ThreadLocalGson method initialValue.

@Override
protected Gson initialValue() {
    final ResultGSONAdapter resultGsonAdapter = new ResultGSONAdapter(propertyView, outputNestingDepth);
    final JsonInputGSONAdapter jsonInputAdapter = new JsonInputGSONAdapter();
    // create GSON serializer
    final GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting().serializeNulls().registerTypeHierarchyAdapter(FrameworkException.class, new FrameworkExceptionGSONAdapter()).registerTypeAdapter(IJsonInput.class, jsonInputAdapter).registerTypeAdapter(Result.class, resultGsonAdapter);
    final boolean lenient = Settings.JsonLenient.getValue();
    if (lenient) {
        // Serializes NaN, -Infinity, Infinity, see http://code.google.com/p/google-gson/issues/detail?id=378
        gsonBuilder.serializeSpecialFloatingPointValues();
    }
    return gsonBuilder.create();
}
Also used : ResultGSONAdapter(org.structr.rest.adapter.ResultGSONAdapter) GsonBuilder(com.google.gson.GsonBuilder) JsonInputGSONAdapter(org.structr.core.rest.JsonInputGSONAdapter) FrameworkExceptionGSONAdapter(org.structr.rest.adapter.FrameworkExceptionGSONAdapter) IJsonInput(org.structr.core.IJsonInput)

Example 2 with JsonInputGSONAdapter

use of org.structr.core.rest.JsonInputGSONAdapter 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

JsonInputGSONAdapter (org.structr.core.rest.JsonInputGSONAdapter)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonNull (com.google.gson.JsonNull)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 GraphObject (org.structr.core.GraphObject)1 IJsonInput (org.structr.core.IJsonInput)1 FrameworkExceptionGSONAdapter (org.structr.rest.adapter.FrameworkExceptionGSONAdapter)1 ResultGSONAdapter (org.structr.rest.adapter.ResultGSONAdapter)1 WebSocketMessage (org.structr.websocket.message.WebSocketMessage)1