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.github.abilityapi.abilityapi.external.Metrics.java

License:Open Source License

/**
 * Gets the plugin specific data.//from   w  ww  . j a va  2  s  .  c  o  m
 * This method is called using Reflection.
 *
 * @return The plugin specific data.
 */
public JsonObject getPluginData() {
    JsonObject data = new JsonObject();

    String pluginName = plugin.getName();
    String pluginVersion = plugin.getVersion().orElse("unknown");

    data.addProperty("pluginName", pluginName);
    data.addProperty("pluginVersion", pluginVersion);

    JsonArray customCharts = new JsonArray();
    for (CustomChart customChart : charts) {
        // Add the data of the custom charts
        JsonObject chart = customChart.getRequestJsonObject(logger, logFailedRequests);
        if (chart == null) { // If the chart is null, we skip it
            continue;
        }
        customCharts.add(chart);
    }
    data.add("customCharts", customCharts);

    return data;
}

From source file:com.github.abilityapi.abilityapi.external.Metrics.java

License:Open Source License

/**
 * Collects the data and sends it afterwards.
 *//* ww  w .j a va  2 s  .c  o  m*/
private void submitData() {
    final JsonObject data = getServerData();

    JsonArray pluginData = new JsonArray();
    // Search for all other bStats Metrics classes to get their plugin data
    for (Object metrics : knownMetricsInstances) {
        try {
            Object plugin = metrics.getClass().getMethod("getPluginData").invoke(metrics);
            if (plugin instanceof JsonObject) {
                pluginData.add((JsonObject) plugin);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
        }
    }

    data.add("plugins", pluginData);

    // Create a new thread for the connection to the bStats server
    new Thread(() -> {
        try {
            // Send the data
            sendData(data);
        } catch (Exception e) {
            // Something went wrong! :(
            if (logFailedRequests) {
                logger.warn("Could not submit plugin stats!", e);
            }
        }
    }).start();
}

From source file:com.github.aint.uchananalyzer.Parser.java

License:Open Source License

private static void save2Json(Collection<Post> posts, String pageNumber) {
    JsonArray array = new JsonArray();
    for (Post post : posts) {
        JsonObject innerJson = new JsonObject();
        innerJson.addProperty("author", post.getAuthor());
        innerJson.addProperty("text", post.getText());
        innerJson.addProperty("date", post.getDate().toString());
        innerJson.addProperty("image", post.isHasImage());
        array.add(innerJson);/*from   w w  w.j  a  va2 s  .  c  o m*/
    }
    JsonObject json = new JsonObject();
    json.add(pageNumber, array);

    try (Writer writer = new FileWriter(pageNumber + ".json")) {
        new GsonBuilder().setPrettyPrinting().create().toJson(json, writer);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.autermann.matlab.json.MatlabRequestSerializer.java

License:Open Source License

private JsonArray serializeParameters(MatlabRequest req, JsonSerializationContext ctx) {
    JsonArray parameters = new JsonArray();
    for (MatlabValue parameter : req.getParameters()) {
        parameters.add(ctx.serialize(parameter));
    }/*from w w w  .j  a  v  a 2s.  c  om*/
    return parameters;

}

From source file:com.github.cc007.headsweeper.controller.HeadSweeperController.java

License:Open Source License

public JsonObject serialize() {
    JsonObject output = new JsonObject();
    JsonArray sweeperGamesJSON = new JsonArray();
    for (HeadSweeperGame sweeperGame : sweeperGames) {
        sweeperGamesJSON.add(sweeperGame.serialize());
    }/*from ww  w.ja  v a2s .  c  om*/
    output.add("sweeperGames", sweeperGamesJSON);
    return output;
}

From source file:com.github.consiliens.harv.gson.IRequestLogRecordSerializer.java

License:Open Source License

public static JsonArray paramsToJsonArray(final HttpParams params) {
    JsonArray paramsJson = new JsonArray();

    // BasicHttpParams has no way to access HashMap parameters so use
    // reflection.
    try {/*from  ww  w  .  ja  v a  2 s. c o  m*/
        final Field parameters = BasicHttpParams.class.getDeclaredField(R.parameters);
        parameters.setAccessible(true);
        HashMap<?, ?> targetParameters = (HashMap<?, ?>) parameters.get(params);

        if (!targetParameters.isEmpty()) {
            for (Entry<?, ?> entry : targetParameters.entrySet()) {
                JsonArray entryJson = new JsonArray();
                entryJson.add(new JsonPrimitive(entry.getKey().toString()));
                // TODO: Is there a way to easily support non-string values?
                entryJson.add(new JsonPrimitive(entry.getValue().toString()));

                paramsJson.add(entryJson);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return paramsJson;
}

From source file:com.github.consiliens.harv.gson.IRequestLogRecordSerializer.java

License:Open Source License

private static JsonArray headersToJsonArray(Header[] headers) {
    final JsonArray headersArray = new JsonArray();

    for (Header header : headers) {
        JsonArray nvPair = new JsonArray();

        nvPair.add(new JsonPrimitive(header.getName()));
        nvPair.add(new JsonPrimitive(header.getValue()));

        headersArray.add(nvPair);//from   w w w .  j a va 2 s. c o  m
    }

    return headersArray;
}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiSerializer.java

License:Apache License

/**
 * Serializer when occur an success//w w  w  . ja  v a 2 s .  co  m
 * 
 * @param cloneData
 *            the list with data cloned
 * @param jsonapi
 *            the json object
 * @param jsonContext
 *            the json context
 * @return the json api object with values created
 */
private JsonElement serializerData(List<Data> cloneData, JsonApi jsonapi,
        JsonSerializationContext jsonContext) {

    JsonObject jsonElem = new JsonObject();

    JsonArray jsonArrayData = new JsonArray();

    for (Data jsonApiData : cloneData) {

        JsonObject jsonData = new JsonObject();

        if (Assert.notEmpty(jsonApiData.getId())) {
            jsonData.addProperty("id", jsonApiData.getId());
        }

        if (Assert.notEmpty(jsonApiData.getType())) {
            jsonData.addProperty("type", jsonApiData.getType());
        }

        if (Assert.notNull(jsonApiData.getAttr())) {
            JsonElement jsonAttr = serializerDataAttr(jsonApiData.getAttr(), jsonContext);
            jsonData.add("attributes", jsonAttr);
        }

        if (Assert.notNull(jsonApiData.getRels())) {
            JsonElement jsonRels = serializerDataRels(jsonApiData.getRels(), jsonContext);

            if (Assert.notNull(jsonRels)) {
                jsonData.add("relationships", jsonRels);
            }

        }

        jsonArrayData.add(jsonData);

    }

    jsonElem.add("data", jsonArrayData);

    return jsonElem;

}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiSerializer.java

License:Apache License

/**
 * Serializer relationship//from  w ww. j  av  a2 s . co  m
 * 
 * @param rels
 *            the relationships object
 * @param jsonContext
 *            the json context
 * @return one instance of json
 */
private JsonElement serializerDataRels(Relationships rels, JsonSerializationContext jsonContext) {

    JsonObject jsonRels = new JsonObject();

    if (Assert.isNull(rels) || rels.getRelationships().isEmpty()) {
        return null;
    }

    for (Relationship jsonApiRels : rels.getRelationships()) {

        if (Assert.notEmpty(jsonApiRels.getName())) {

            JsonObject jsonRel = new JsonObject();
            JsonObject jsonRelLink = null;
            JsonArray jsonRelData = null;
            JsonObject jsonRelMeta = null;

            // Build links json object
            if (Assert.notNull(jsonApiRels.getLinks())) {

                jsonRelLink = new JsonObject();
                JsonObject jsonRelLinkRelated = null;

                if (Assert.notNull(jsonApiRels.getLinks().getLinkRelated())) {

                    jsonRelLinkRelated = new JsonObject();

                    JsonTools.insertObject(jsonRelLinkRelated, "href",
                            jsonApiRels.getLinks().getLinkRelated().getHref());

                    if (Assert.notNull(jsonApiRels.getLinks().getLinkRelated().getMeta())) {
                        JsonElement jsonRelLinkMeta = serializerObject(
                                jsonApiRels.getLinks().getLinkRelated().getMeta(),
                                EasyJsonApiTypeToken.TOKEN_META, jsonContext);

                        JsonTools.insertObject(jsonRelLinkRelated, "meta", jsonRelLinkMeta);
                    }
                }

                JsonTools.insertObject(jsonRelLink, "self", jsonApiRels.getLinks().getSelf());
                JsonTools.insertObject(jsonRelLink, "related", jsonRelLinkRelated);

            }

            // Build data json object
            if (Assert.notNull(jsonApiRels.getDataLinkage()) && !jsonApiRels.getDataLinkage().isEmpty()) {

                jsonRelData = new JsonArray();

                for (DataLinkage dataLinkage : jsonApiRels.getDataLinkage()) {
                    JsonObject jsonData = new JsonObject();
                    jsonData.addProperty("id", dataLinkage.getId());
                    jsonData.addProperty("type", dataLinkage.getType());

                    jsonRelData.add(jsonData);
                }
            }

            // Build meta json object
            if (Assert.notNull(jsonApiRels.getMeta())) {

                jsonRelMeta = serializerObject(jsonApiRels.getMeta(),
                        EasyJsonApiTypeToken.TOKEN_META_RELATIONSHIP, jsonContext).getAsJsonObject();
            }

            JsonTools.insertObject(jsonRel, "links", jsonRelLink);
            JsonTools.insertObject(jsonRel, "data", jsonRelData);
            JsonTools.insertObject(jsonRel, "meta", jsonRelMeta);

            JsonTools.insertObject(jsonRels, jsonApiRels.getName(), jsonRel);

        }

    }

    return jsonRels;

}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiSerializer.java

License:Apache License

/**
 * Serializer when occur an error//w w w.ja  va  2 s . c om
 * 
 * @param cloneError
 *            the list with errors cloned
 * @param jsonApi
 *            the json object
 * @param jsonContext
 *            the json context
 * @return the json api object with values created
 */
private JsonElement serializerError(List<Error> cloneError, JsonApi jsonApi,
        JsonSerializationContext jsonContext) {

    JsonObject jsonElem = new JsonObject();

    JsonArray jsonArrayErrors = new JsonArray();

    for (Error jsonApiError : cloneError) {

        JsonObject jsonError = new JsonObject();

        if (Assert.notEmpty(jsonApiError.getId())) {
            jsonError.addProperty("id", jsonApiError.getId());
        }

        if (Assert.notEmpty(jsonApiError.getTitle())) {
            jsonError.addProperty("title", jsonApiError.getTitle());
        }

        if (Assert.notEmpty(jsonApiError.getCode())) {
            jsonError.addProperty("code", jsonApiError.getCode());
        }

        if (Assert.notEmpty(jsonApiError.getDetail())) {
            jsonError.addProperty("detail", jsonApiError.getDetail());
        }

        if (Assert.notNull(jsonApiError.getStatus())) {
            jsonError.addProperty("status", String.valueOf(jsonApiError.getStatus().getCode()));
        }

        if (Assert.notNull(jsonApiError.getSource())) {
            // TODO: Need to do
            // jsonError.addProperty("source", requestError.getDetail());
        }

        if (Assert.notNull(jsonApiError.getMeta())) {
            // TODO: Need to do
            // jsonError.addProperty("meta", requestError.getDetail());
        }

        jsonArrayErrors.add(jsonError);
    }

    jsonElem.add("errors", jsonArrayErrors);

    return jsonElem;

}