Example usage for com.google.gson JsonObject has

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

Introduction

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

Prototype

public boolean has(String memberName) 

Source Link

Document

Convenience method to check if a member with the specified name is present in this object.

Usage

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

public static boolean fromJson(String queriesJson, List<UnitStep> queryUnits) throws ErrorCodeExp {
    Gson gson = new Gson();

    JsonElement jsonElem = gson.fromJson(queriesJson, JsonElement.class);
    JsonObject jsonObj = jsonElem.getAsJsonObject();
    Iterator<JsonElement> queriesIterator = jsonObj.get("queries").getAsJsonArray().iterator();

    while (queriesIterator.hasNext()) {
        JsonObject queryObj = (JsonObject) queriesIterator.next();

        /**//from  www.  java2s. c o  m
         * queryid
         */
        if (queryObj.has("queryid")) {
            String queryId = queryObj.get("queryid").getAsString().trim();
            addQuery(queryUnits, queryObj, queryId);
        } else if (queryObj.has("functionid")) {
            String functionId = queryObj.get("functionid").getAsString().trim();
            addFunction(queryUnits, queryObj, functionId);
        } else if (queryObj.has("spid")) {
            String spId = queryObj.get("spid").getAsString().trim();
            addSp(queryUnits, queryObj, spId);
        }
    }

    return true;
}

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

private static void addFunction(List<UnitStep> queryUnits, JsonObject queryObj, String funcId)
        throws ErrorCodeExp {

    List<UnitStep> queryObject = FunctionDefinationFactory.functions.get(funcId);

    try {/*  w ww . j  a va  2 s .c  om*/
        if (queryObject == null) {
            FunctionDefinationFactory.refreshFunctions();
            queryObject = FunctionDefinationFactory.functions.get(funcId);
        }
    } catch (Exception e) {
        LOG.warn(e.getMessage());
    }

    if (queryObject == null) {
        throw new ErrorCodeExp(funcId, ErrorCodes.FUNCTION_NOT_FOUND, "Function not found :" + funcId,
                ErrorCodes.FUNCTION_KEY);
    }

    Boolean isRecursive = (queryObj.has("isRecursive")) ? queryObj.get("isRecursive").getAsBoolean() : false;

    /**
     * Single Expression
     */
    UnitExpr expr = extractExpr(queryObj);

    /**
     * Boolean Expression
     */
    List<UnitExpr> andExprs = booleanExprs(queryObj, true);
    List<UnitExpr> orExprs = booleanExprs(queryObj, false);

    String variables = (queryObj.has("variables"))
            ? "{variables:" + queryObj.get("variables").getAsJsonArray().toString() + "}"
            : null;

    UnitFunction uf = new UnitFunction(funcId, variables, expr, andExprs, orExprs, isRecursive);
    uf.stepId = funcId;
    uf.isFunc = true;

    queryUnits.add(uf);
}

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

private static void addSp(List<UnitStep> queryUnits, JsonObject queryObj, String spId) throws ErrorCodeExp {

    StoredProcConfig spObject = StoredProcDefinationFactory.storedProcM.get(spId);

    try {/*from   www . j a va  2  s  .  c  om*/
        if (spObject == null) {
            StoredProcDefinationFactory.refreshQueries();
        }
    } catch (Exception e) {
        LOG.warn(e.getMessage());
    }

    if (spObject == null) {
        throw new ErrorCodeExp(spId, ErrorCodes.STORED_PROC_NOT_FOUND, "Sp not found :" + spId,
                ErrorCodes.SP_KEY);
    }

    String variables = (queryObj.has("variables"))
            ? "{variables:" + queryObj.get("variables").getAsJsonArray().toString() + "}"
            : null;

    List<Object> inParams = new ArrayList<Object>();
    List<StoredProcOutParam> queryOutVars = spObject.getOutVars();
    List<String> inputVars = spObject.getVars();

    UnitSp up = new UnitSp(spObject, variables, inParams, queryOutVars, inputVars);
    up.stepId = spId;
    up.isFunc = false;
    queryUnits.add(up);
}

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

private static void addQuery(List<UnitStep> queryUnits, JsonObject queryObj, String queryId)
        throws ErrorCodeExp {

    AppConfig queryObject = QueryDefinationFactory.queryM.get(queryId);

    if (queryObject == null) {
        QueryDefinationFactory.refreshQueries();
        queryObject = QueryDefinationFactory.queryM.get(queryId);
    }//from ww  w  . j  av  a 2 s . c  om

    if (queryObject == null) {
        throw new ErrorCodeExp(queryId, ErrorCodes.QUERY_NOT_FOUND,
                "Query Id " + queryId + " is not configured.", ErrorCodes.QUERY_KEY);

    }

    queryObject = queryObject.clone();

    /**
     * Parameters
     */
    List<Object> paramL = new ArrayList<Object>();
    if (queryObj.has("params")) {
        JsonArray paramsArray = queryObj.get("params").getAsJsonArray();
        int totalParam = paramsArray.size();
        for (int i = 0; i < totalParam; i++) {
            String paramVal = paramsArray.get(i).getAsString();
            if (paramVal.length() > 0) {
                if (paramVal.equals("__null") || paramVal.equals("null"))
                    paramVal = null;
            }
            paramL.add(paramVal);
        }
    }

    /**
     * where, sort, offset, limit
     */
    String where = (queryObj.has("where")) ? queryObj.get("where").getAsString() : null;
    String sort = (queryObj.has("sort")) ? queryObj.get("sort").getAsString() : null;
    int offset = (queryObj.has("offset")) ? queryObj.get("offset").getAsInt() : -1;
    int limit = (queryObj.has("limit")) ? queryObj.get("limit").getAsInt() : -1;

    /**
     * If there are any sequence ids to be generated then generate the ids 
     * and add the generated sequenceids to the variables string.
     */
    String variables = (queryObj.has("variables"))
            ? "{variables:" + queryObj.get("variables").getAsJsonArray().toString() + "}"
            : null;

    if (DEBUG_ENABLED && variables != null)
        LOG.debug("String variables are : " + variables);

    Boolean isRecursive = (queryObj.has("isRecursive")) ? queryObj.get("isRecursive").getAsBoolean() : false;

    /**
     * Single Expression
     */
    UnitExpr expr = extractExpr(queryObj);

    /**
     * Boolean Expression
     */
    List<UnitExpr> andExprs = booleanExprs(queryObj, true);
    List<UnitExpr> orExprs = booleanExprs(queryObj, false);

    JsonElement sequenceElem = null;
    if (queryObj.has("sequences"))
        sequenceElem = queryObj.get("sequences");

    UnitQuery uq = new UnitQuery(queryObject, paramL, expr, andExprs, orExprs, where, sort, offset, limit,
            variables, isRecursive, sequenceElem);
    uq.isFunc = false;
    uq.stepId = queryId;
    queryUnits.add(uq);
}

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

private static List<UnitExpr> booleanExprs(JsonObject queryObj, boolean flipAndOr) {

    String tag = (flipAndOr) ? "andexprs" : "orexprs";

    if (!queryObj.has(tag))
        return null;

    List<UnitExpr> andExprs = new ArrayList<>();
    Iterator expression = queryObj.get(tag).getAsJsonArray().iterator();

    while (expression.hasNext()) {
        JsonObject exprObj = (JsonObject) expression.next();
        UnitExpr cluse = extractExpr(exprObj);
        if (null != cluse) {
            andExprs.add(cluse);/*  w  ww .  jav a  2 s .  c  o  m*/
        }
    }
    return andExprs;
}

From source file:com.bizosys.dataservice.sql.SqlSensorInputParser.java

License:Apache License

private static UnitExpr extractExpr(JsonObject queryObj) {
    UnitExpr expr = null;// w  ww. j  a  va 2 s  .  co  m
    if (queryObj.has("expr")) {

        if (queryObj.get("expr").isJsonNull())
            return expr;
        if (!queryObj.get("expr").isJsonObject())
            return expr;

        JsonObject exprObj = queryObj.get("expr").getAsJsonObject();

        expr = new UnitExpr(exprObj.get("lhs").getAsString(), exprObj.get("rhs").getAsString(),
                exprObj.get("opr").getAsString());

        if (expr.rhs.length() > 0) {
            if (expr.rhs.charAt(0) == '_') {
                if (expr.rhs.equals("__null"))
                    expr.rhs = null;
            }
        }
    }
    return expr;
}

From source file:com.blackducksoftware.integration.hub.teamcity.server.global.ServerHubConfigPersistenceManager.java

License:Apache License

public void loadSettings() {
    if (configFile.exists()) {
        try (BufferedReader reader = new BufferedReader(new FileReader(configFile))) {
            final JsonObject globalConfigJson = jsonParser.parse(reader).getAsJsonObject();
            try {
                if (globalConfigJson.has("hubServerConfig")) {
                    setHubServerConfig(//ww  w . j av  a  2s .  c om
                            gson.fromJson(globalConfigJson.get("hubServerConfig"), HubServerConfig.class));
                    setHubWorkspaceCheck(globalConfigJson.get("hubWorkspaceCheck").getAsBoolean());
                } else {
                    throw new JsonParseException(
                            "The Hub Teamcity configuration must be from a previous version.");
                }
            } catch (final JsonParseException e) {
                // try to load the old config
                setHubServerConfig(gson.fromJson(globalConfigJson, HubServerConfig.class));
                setHubWorkspaceCheck(true);
            }
        } catch (final IOException e) {
            Loggers.SERVER.error("Failed to load Hub config file: " + configFile, e);
        }
    }
}

From source file:com.blackypaw.mc.i18n.chat.ChatComponentDeserializer.java

License:Open Source License

/**
 * Creates a text component given its JSON representation. Both the shorthand notation as
 * a raw string as well as the notation as a full-blown JSON object are supported.
 *
 * @param json The JSON element to be deserialized into a text component
 *
 * @return The deserialized TextComponent
 */// w ww.j ava 2 s  .c o  m
private TextComponent deserializeTextComponent(JsonElement json) {
    final TextComponent component = new TextComponent("");

    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            component.setText(primitive.getAsString());
        }
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();

        if (object.has("text")) {
            JsonElement textElement = object.get("text");
            if (textElement.isJsonPrimitive()) {
                JsonPrimitive textPrimitive = textElement.getAsJsonPrimitive();
                if (textPrimitive.isString()) {
                    component.setText(textPrimitive.getAsString());
                }
            }
        }

        if (object.has("extra")) {
            JsonElement extraElement = object.get("extra");
            if (extraElement.isJsonArray()) {
                JsonArray extraArray = extraElement.getAsJsonArray();
                for (int i = 0; i < extraArray.size(); ++i) {
                    JsonElement fieldElement = extraArray.get(i);
                    component.addChild(this.deserializeComponent(fieldElement));
                }
            }
        }
    }

    return component;
}

From source file:com.broadlink.control.api.BroadlinkAPI.java

/**
 * Retrieve a list of devices within same WiFi
 *///from  w w  w.j av a  2  s .c  o m
public ArrayList<DeviceInfo> getProbeList() {
    if (mDevices == null || mDevices.isEmpty()) {
        JsonObject out = broadlinkExecuteCommand(BroadlinkConstants.CMD_PROBE_LIST_ID,
                BroadlinkConstants.CMD_PROBE_LIST);
        if (out == null || !out.has("list"))
            return null;
        JsonArray listJsonArray = out.get("list").getAsJsonArray();

        Gson gson = new Gson();
        Type listType = new TypeToken<ArrayList<DeviceInfo>>() {
        }.getType();
        ArrayList<DeviceInfo> deviceArrayList = (ArrayList<DeviceInfo>) gson.fromJson(listJsonArray, listType);
        if (deviceArrayList.size() <= 0)
            return null;
        mDevices = deviceArrayList;
    }

    return mDevices;
}

From source file:com.buddycloud.friendfinder.provider.Facebook.java

License:Apache License

@Override
public ContactProfile getProfile(String accessToken) throws Exception {

    StringBuilder meURLBuilder = new StringBuilder(GRAPH_URL);
    meURLBuilder.append("/me?fields=id&access_token=").append(accessToken);
    String myId = HttpUtils.consumeJSON(meURLBuilder.toString()).getAsJsonObject().get("id").getAsString();

    ContactProfile contactProfile = new ContactProfile(HashUtils.encodeSHA256(PROVIDER_NAME, myId));

    StringBuilder friendsURLBuilder = new StringBuilder(GRAPH_URL);
    friendsURLBuilder.append("/me/friends?fields=id&access_token=").append(accessToken);

    while (true) {
        JsonObject friendsJson = HttpUtils.consumeJSON(friendsURLBuilder.toString()).getAsJsonObject();

        JsonArray friendsArray = friendsJson.get("data").getAsJsonArray();
        for (JsonElement friendJson : friendsArray) {
            JsonObject friendObject = friendJson.getAsJsonObject();
            String friendId = friendObject.get("id").getAsString();
            contactProfile.addFriendHash(HashUtils.encodeSHA256(PROVIDER_NAME, friendId));
        }/*from  w ww.  j a  v a 2s .c  o m*/
        JsonObject paging = friendsJson.get("paging").getAsJsonObject();
        if (!paging.has("next")) {
            break;
        }
        friendsURLBuilder = new StringBuilder(paging.get("next").getAsString());
    }

    return contactProfile;
}