Example usage for com.google.gson JsonArray JsonArray

List of usage examples for com.google.gson JsonArray JsonArray

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:com.autoclavestudios.jbower.config.internal.JsonRegistryTranslator.java

License:Apache License

private JsonArray ConvertToJsonArray(List<URL> UrlList) {

    JsonArray jsonArray = new JsonArray();
    for (URL url : UrlList) {
        jsonArray.add(new JsonPrimitive(url.toString()));
    }/*from  w ww.ja  va 2  s .co  m*/
    return jsonArray;
}

From source file:com.awadm.CountBuilderServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String action = request.getParameter("action"); //action comes with URL
    String uuid = request.getParameter("uuid"); //read param from url for hashtags

    if (uuid != null) {
        if (action.equals("count")) {
            //query
            String selectSQL = "SELECT TIMESTAMP, Count(ID) AS tblTimeCount FROM TBL_COUNT WHERE UUID = ? GROUP BY TIMESTAMP";
            //String selectSQL = "SELECT CONVERT(VARCHAR(16), TIMESTAMP) AS timekey, Count(ID) AS tblTimeCount FROM TBL_COUNT WHERE UUID = ? GROUP BY CONVERT(VARCHAR(16), TIMESTAMP)";
            try {
                PreparedStatement updateemp = con.prepareStatement(selectSQL);
                updateemp.setString(1, uuid);
                ResultSet rSet = updateemp.executeQuery();
                Gson gson = new Gson();

                JsonObject jsonResponse = new JsonObject();
                JsonArray data = new JsonArray();

                while (rSet.next()) {
                    JsonArray row = new JsonArray();
                    row.add(new JsonPrimitive(rSet.getString("TIMESTAMP")));
                    row.add(new JsonPrimitive(rSet.getString("TBLTIMECOUNT")));
                    data.add(row);/*from  ww w .  j  a  va  2s  . c  o m*/
                }

                jsonResponse.add("rows", data);
                response.getWriter().write(gson.toJson(jsonResponse));

            } catch (SQLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else { //no uuid came with request
        response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
    }
}

From source file:com.baidu.cc.configuration.service.impl.VersionServiceImpl.java

License:Apache License

/**
 * ???.//from  w w  w .  j  av  a 2 s . c  o  m
 * 
 * @param os
 *            ?
 * @param versionId
 *            ?id
 * @throws IOException
 *             ??
 */
@Override
public void exportToFile(OutputStream os, Long versionId) throws IOException {

    Version version = findById(versionId);
    if (version == null) {
        LOGGER.warn("export to file failed due to version id not exist. id=" + versionId);
        return;
    }

    JsonArray jsonArray = new JsonArray();

    // get all group
    List<ConfigGroup> groups = configGroupService.findByVersionId(versionId);
    if (CollectionUtils.isEmpty(groups)) {
        LOGGER.warn("No config group under version id:" + versionId);
        return;
    }

    for (ConfigGroup configGroup : groups) {
        JsonObject jo = configGroup.copyToJson();
        jsonArray.add(jo);
        Long groupId = configGroup.getId();
        // add sub
        List<ConfigItem> items = configItemService.findByGroupId(groupId, true);
        if (CollectionUtils.isEmpty(items)) {
            LOGGER.warn("No config items under version id:" + versionId + " and group id:" + groupId);
        }

        JsonArray subItemsJson = new JsonArray();
        for (ConfigItem configItem : items) {
            JsonObject itemJson = configItem.copyToJson();
            subItemsJson.add(itemJson);
        }
        jo.add(CONFIG_ITEMS_ELE, subItemsJson);
    }

    writeJson(jsonArray, os);

}

From source file:com.balajeetm.mystique.core.ArrayToMapMystTurn.java

License:Open Source License

@Override
protected JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    JsonObject mapJson = new JsonObject();
    JsonElement elementSource = mystiqueLever.getFirst(source);

    if (null != elementSource) {
        turn = mystiqueLever.asJsonObject(turn, new JsonObject());
        JsonElement granularSource = getGranularSource(elementSource, turn, deps, aces);
        JsonArray inputArray = mystiqueLever.asJsonArray(granularSource, new JsonArray());
        JsonArray keyArray = mystiqueLever.getJpath(turn.get(MystiqueConstants.KEY));
        if (mystiqueLever.isNotNull(keyArray)) {
            JsonElement valueElement = turn.get(MystiqueConstants.VALUE);
            valueElement = mystiqueLever.isNull(valueElement) ? new JsonArray() : valueElement;

            for (JsonElement jsonElement : inputArray) {
                JsonElement keyField = mystiqueLever.getField(jsonElement, keyArray, deps, aces);
                String key = mystiqueLever.asString(keyField, MystiqueConstants.EMPTY);

                JsonElement finalValue = mystiqueLever.subset(jsonElement, deps, aces, valueElement);
                mapJson.add(key, finalValue);
            }/*from w  w w .  ja v  a2  s  .  c  o m*/
        }
    }

    return mapJson;
}

From source file:com.balajeetm.mystique.core.ChainMystTurn.java

License:Open Source License

@Override
public JsonElement transform(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn,
        JsonObject resultWrapper) {//ww  w .  j a  va 2 s . c  o  m

    JsonElement transform = JsonNull.INSTANCE;
    turn = jsonLever.asJsonObject(turn, new JsonObject());
    JsonArray turnArray = jsonLever.asJsonArray(turn.get(MystiqueConstants.TURNS), new JsonArray());

    for (JsonElement turnObject : turnArray) {
        if (jsonLever.isObject(turnObject)) {
            JsonObject asJsonObject = turnObject.getAsJsonObject();
            MystTurn mystique = factory.getMystTurn(asJsonObject);
            if (null != mystique) {
                mystique.transform(source, deps, aces, asJsonObject, resultWrapper);
            }
        }
    }
    return transform;
}

From source file:com.balajeetm.mystique.core.CopyMystTurn.java

License:Open Source License

@Override
public JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {
    JsonElement transform = null;/* w w w .j a v a  2  s  .  co m*/
    if (CollectionUtils.isNotEmpty(source)) {
        if (source.size() > 1) {
            transform = new JsonArray();
            for (JsonElement jsonElement : source) {
                JsonElement granularSource = getGranularSource(jsonElement, turn, deps, aces);
                granularSource = getSubset(granularSource, deps, aces, turn);
                transform.getAsJsonArray().add(granularSource);
            }
        } else {
            JsonElement granularSource = getGranularSource(source.get(0), turn, deps, aces);
            transform = getSubset(granularSource, deps, aces, turn);
        }
    }
    return transform;
}

From source file:com.balajeetm.mystique.core.GetFromDepsMystTurn.java

License:Open Source License

@Override
protected JsonElement transmute(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn) {

    JsonElement transform = JsonNull.INSTANCE;
    JsonElement elementSource = mystiqueLever.getFirst(source);
    if (null != elementSource) {
        // element source is key

        turn = mystiqueLever.asJsonObject(turn, new JsonObject());
        JsonElement granularSource = getGranularSource(elementSource, turn, deps, aces);
        JsonArray reference = mystiqueLever.getJpath(granularSource);

        /* Default is needed since the key can turn out to be null */
        JsonArray keyPath = mystiqueLever.asJsonArray(mystiqueLever.getJpath(turn.get(MystiqueConstants.KEY)),
                new JsonArray());
        JsonElement value = turn.get(MystiqueConstants.VALUE);
        value = mystiqueLever.isNull(value) ? new JsonArray() : value;

        // keymap - The source is deps
        JsonElement keyMap = mystiqueLever.getField(deps, keyPath, deps, aces);

        JsonElement actualElement = mystiqueLever.get(keyMap, reference);

        transform = mystiqueLever.subset(actualElement, deps, aces, value);
    }/*w  ww.j ava  2 s .com*/
    return transform;
}

From source file:com.balajeetm.mystique.core.LoopySpell.java

License:Open Source License

@Override
public JsonElement cast(MystTurn mystique) {
    JsonElement transform = JsonNull.INSTANCE;
    if (CollectionUtils.isNotEmpty(source)) {
        transform = new JsonArray();
        List<CompletableFuture<JsonElement>> cfs = new ArrayList<>();
        JsonArray jsonArray = source.get(0).getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {

            CompletableFuture<JsonElement> transformElement = CompletableFuture.supplyAsync(() -> mystique
                    .transform(Lists.newArrayList(jsonElement), dependencies, aces, turn, resultWrapper))
                    .exceptionally(e -> {
                        String msg = String.format(
                                "Error transforming one of the elements in the array for %s - %s", turn,
                                e.getMessage());
                        logger.info(msg, e);
                        return JsonNull.INSTANCE;
                    });//from w w w.j  a  v  a  2s .  c o m
            cfs.add(transformElement);
        }

        for (CompletableFuture<JsonElement> completableFuture : cfs) {
            JsonElement element = null;
            try {
                element = completableFuture.get();
            } catch (InterruptedException | ExecutionException e) {
                String msg = String.format(
                        "Error getting transformed element for one of the elements in the array for %s - %s",
                        turn, e.getMessage());
                logger.info(msg, e);
                element = JsonNull.INSTANCE;
            }
            Boolean optional = jsonLever.asBoolean(turn.get(MystiqueConstants.OPTIONAL), Boolean.FALSE);
            if (jsonLever.isNotNull(element) || !optional) {
                transform.getAsJsonArray().add(element);
            }
        }
    }
    return transform;
}

From source file:com.balajeetm.mystique.core.module.GsonDeserialiser.java

License:Open Source License

/**
 * Deserialize./*from  w ww . j a  v a  2  s . co  m*/
 *
 * @param node the node
 * @return the json element
 */
@SuppressWarnings("unchecked")
private T deserialize(JsonNode node) {
    JsonElement result = JsonNull.INSTANCE;
    if (null != node && !node.isNull()) {
        if (node.isObject()) {
            ObjectNode onode = (ObjectNode) node;
            JsonObject jsonObject = new JsonObject();
            Iterator<Entry<String, JsonNode>> fields = onode.fields();
            while (fields.hasNext()) {
                Entry<String, JsonNode> next = fields.next();
                jsonObject.add(next.getKey(), deserialize(next.getValue()));
            }
            result = jsonObject;
        } else if (node.isArray()) {
            ArrayNode anode = (ArrayNode) node;
            JsonArray jsonArray = new JsonArray();
            Iterator<JsonNode> elements = anode.elements();
            while (elements.hasNext()) {
                jsonArray.add(deserialize(elements.next()));
            }
            result = jsonArray;
        } else if (node.isBoolean()) {
            result = new JsonPrimitive(node.asBoolean());
        } else if (node.isNumber()) {
            NumericNode nnode = (NumericNode) node;
            result = new JsonPrimitive(nnode.numberValue());
        } else if (node.isTextual()) {
            TextNode tnode = (TextNode) node;
            result = new JsonPrimitive(tnode.textValue());
        }
    }

    return (T) result;
}

From source file:com.balajeetm.mystique.core.ToggleMystTurn.java

License:Open Source License

@Override
public JsonElement transform(List<JsonElement> source, JsonObject deps, JsonObject aces, JsonObject turn,
        JsonObject resultWrapper) {/*from  ww w . j  av  a  2s .  c  om*/

    JsonElement transform = JsonNull.INSTANCE;
    turn = jsonLever.asJsonObject(turn, new JsonObject());
    JsonArray turnArray = jsonLever.asJsonArray(turn.get(MystiqueConstants.TURNS), new JsonArray());
    for (JsonElement turnObject : turnArray) {
        if (jsonLever.isObject(turnObject)) {
            JsonObject asJsonObject = turnObject.getAsJsonObject();
            MystTurn mystique = factory.getMystTurn(asJsonObject);
            if (null != mystique) {
                transform = mystique.transform(source, deps, aces, asJsonObject, resultWrapper);
            }
            if (jsonLever.isNotNull(transform)) {
                break;
            }
        }
    }
    return transform;
}