Example usage for com.google.gson JsonObject isJsonNull

List of usage examples for com.google.gson JsonObject isJsonNull

Introduction

In this page you can find the example usage for com.google.gson JsonObject isJsonNull.

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ActionTypeAdapterFactory.java

License:Apache License

@Override
protected void beforeWrite(Action source, JsonElement toSerialize) {
    if (toSerialize == null || toSerialize.isJsonNull()) {
        return;// w  ww . ja v  a  2s  .c  om
    }

    JsonObject actionObj = toSerialize.getAsJsonObject();
    if (actionObj == null || actionObj.isJsonNull()) {
        return;
    }

    // rewrite parameters from a nested object (map) to properties on the action
    JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS);
    if (paramsElem != null && !paramsElem.isJsonNull()) {
        JsonObject paramsObj = paramsElem.getAsJsonObject();
        actionObj.remove(PROPERTY_PARAMETERS);

        for (Entry<String, JsonElement> entry : paramsObj.entrySet()) {
            actionObj.add(entry.getKey(), entry.getValue());
        }
    }
}

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ActionTypeAdapterFactory.java

License:Apache License

@Override
protected void afterRead(JsonElement deserialized) {
    if (deserialized == null || deserialized.isJsonNull()) {
        return;//from w  ww . ja  v  a 2 s  .  c om
    }

    JsonObject actionObj = deserialized.getAsJsonObject();
    if (actionObj == null || actionObj.isJsonNull()) {
        return;
    }

    if (actionObj.has(PROPERTY_PARAMETERS)) {
        throw new IllegalArgumentException("Attempting to deserialize Action that contains a colliding "
                + PROPERTY_PARAMETERS + " property");
    }

    // temporary list of field names
    List<String> fields = new ArrayList<>();

    // rewrite parameters to a nested object (map)
    JsonObject paramsObj = new JsonObject();
    for (Entry<String, JsonElement> entry : actionObj.entrySet()) {
        // skip ID field
        if (PROPERTY_ID.equals(entry.getKey())) {
            continue;
        }

        String paramId = entry.getKey();
        fields.add(paramId); // to be removed later

        paramsObj.add(paramId, entry.getValue());
    }
    actionObj.add(PROPERTY_PARAMETERS, paramsObj);

    // remove all fields other than the list
    fields.forEach(field -> actionObj.remove(field));
}

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ApplicationTypeAdapterFactory.java

License:Apache License

@Override
protected void beforeWrite(Application source, JsonElement toSerialize) {
    if (toSerialize == null || toSerialize.isJsonNull()) {
        return;/*w ww.j  a v a  2  s  .  c o m*/
    }

    JsonObject appObj = toSerialize.getAsJsonObject();
    if (appObj == null || appObj.isJsonNull()) {
        return;
    }

    // remove widget IDs
    JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
    if (widgetsElem != null && !widgetsElem.isJsonNull()) {
        for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) {
            JsonElement widgetElem = entry.getValue();
            if (widgetElem == null || widgetElem.isJsonNull()) {
                continue;
            }
            widgetElem.getAsJsonObject().remove(PROPERTY_ID);
        }
    }

    // remove action IDs
    JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
    if (actionsElem != null && !actionsElem.isJsonNull()) {
        for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) {
            JsonElement actionElem = entry.getValue();
            if (actionElem == null || actionElem.isJsonNull()) {
                continue;
            }
            actionElem.getAsJsonObject().remove(PROPERTY_ID);
        }
    }
}

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.ApplicationTypeAdapterFactory.java

License:Apache License

@Override
protected void afterRead(JsonElement deserialized) {
    if (deserialized == null || deserialized.isJsonNull()) {
        return;/*  w w w . jav a2 s.  c  om*/
    }

    JsonObject appObj = deserialized.getAsJsonObject();
    if (appObj == null || appObj.isJsonNull()) {
        return;
    }

    // inject widget IDs
    JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
    if (widgetsElem != null && !widgetsElem.isJsonNull()) {
        for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) {
            JsonElement widgetElem = entry.getValue();
            if (widgetElem == null || widgetElem.isJsonNull()) {
                continue;
            }
            widgetElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
        }
    }

    // inject action IDs
    JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
    if (actionsElem != null && !actionsElem.isJsonNull()) {
        for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) {
            JsonElement actionElem = entry.getValue();
            if (actionElem == null || actionElem.isJsonNull()) {
                continue;
            }
            actionElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
        }
    }
}

From source file:org.syphr.lametrictime.api.common.impl.typeadapters.UpdateActionTypeAdapterFactory.java

License:Apache License

@Override
protected void beforeWrite(UpdateAction source, JsonElement toSerialize) {
    if (toSerialize == null || toSerialize.isJsonNull()) {
        return;/*ww  w .ja v  a2  s  .  c o m*/
    }

    JsonObject actionObj = toSerialize.getAsJsonObject();
    if (actionObj == null || actionObj.isJsonNull()) {
        return;
    }

    // rewrite parameters map from {name => Parameter} to {name => value}
    JsonElement paramsElem = actionObj.get(PROPERTY_PARAMETERS);
    if (paramsElem != null && !paramsElem.isJsonNull()) {
        JsonObject paramsObj = paramsElem.getAsJsonObject();
        actionObj.remove(PROPERTY_PARAMETERS);

        JsonObject newParamsObj = new JsonObject();
        for (Entry<String, JsonElement> entry : paramsObj.entrySet()) {
            newParamsObj.add(entry.getKey(),
                    entry.getValue().getAsJsonObject().getAsJsonPrimitive(PROPERTY_VALUE));
        }
        actionObj.add(PROPERTY_PARAMETERS, newParamsObj);
    }
}

From source file:org.wikimedia.analytics.kraken.funnel.Funnel.java

License:Open Source License

private UserActionNode addUserActionNodeToGraph(DirectedGraph<Node, DefaultEdge> graph, JsonObject json) {
    UserActionNode node = null;//from  w  w w.  j  a  va 2s  . com
    if (json != null && !json.isJsonNull()) {
        node = new UserActionNode(json);
        if (!graph.containsVertex(node)) {
            graph.addVertex(node);
        }
    }
    return node;

}