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.jsdossier.Config.java

License:Apache License

/**
 * Returns this configuration object as a JSON object.
 *//*from   w w  w.j  a v a  2 s . co  m*/
JsonObject toJson() {
    JsonObject json = new JsonObject();
    json.add("output", new JsonPrimitive(output.toString()));
    json.add("sources", toJsonArray(srcs));
    json.add("modules", toJsonArray(modules));
    json.add("externs", toJsonArray(externs));
    json.add("excludes", toJsonArray(excludes));
    json.add("typeFilters", toJsonArray(typeFilters));
    json.add("stripModulePrefix", new JsonPrimitive(modulePrefix.toString()));
    json.add("readme", readme.isPresent() ? new JsonPrimitive(readme.get().toString()) : JsonNull.INSTANCE);
    json.addProperty("strict", strict);
    json.addProperty("useMarkdown", useMarkdown);
    json.addProperty("language", language.name());

    JsonArray pages = new JsonArray();
    for (Page page : customPages) {
        pages.add(page.toJson());
    }
    json.add("customPages", pages);

    return json;
}

From source file:com.github.jsdossier.Config.java

License:Apache License

private JsonArray toJsonArray(ImmutableSet<?> items) {
    JsonArray array = new JsonArray();
    for (Object i : items) {
        array.add(new JsonPrimitive(i.toString()));
    }/*from   w w w. ja  v a 2 s  .c  o  m*/
    return array;
}

From source file:com.github.jsdossier.DocWriter.java

License:Apache License

private static JsonArray getJsonArray(JsonObject object, String name) {
    if (!object.has(name)) {
        object.add(name, new JsonArray());
    }//from   w ww. j ava  2  s.com
    return object.get(name).getAsJsonArray();
}

From source file:com.github.jsdossier.Flags.java

License:Apache License

private void addToArray(String key, JsonElement element) {
    if (!jsonConfig.has(key)) {
        jsonConfig.add(key, new JsonArray());
    }// www  . j a  v a2 s .c  o m
    jsonConfig.get(key).getAsJsonArray().add(element);
}

From source file:com.github.jsdossier.TypeIndex.java

License:Apache License

private synchronized static JsonArray getJsonArray(JsonObject object, String name) {
    if (!object.has(name)) {
        object.add(name, new JsonArray());
    }//ww  w. j  av  a 2s  .  com
    return object.get(name).getAsJsonArray();
}

From source file:com.github.tarsys.android.orm.dataobjects.JSONDataSource.java

/**
 * Obtiene el objeto JsonArray asociado al origen de dataRows
 * @return//w  ww.  j  ava2  s  .c o  m
 */
public JsonArray getData() {
    JsonArray datos = new JsonArray();
    try {
        if (!this.jsonData.isEmpty())
            datos = new JsonParser().parse(this.jsonData).getAsJsonArray();
    } catch (JsonSyntaxException ex) {

    }
    return datos;
}

From source file:com.github.zhizheng.json.JsonSchemaGeneratorImpl.java

License:Apache License

/**
 * ? JsonElement? Json Schema /*from   ww  w .j a v a 2s .  c  om*/
 * 
 * @param jsonElement
 * @param elementName
 * @param isFirstLevel
 * @param required
 * @return
 */
private JsonObject makeSchemaElement(JsonElement jsonElement, String elementName, boolean isFirstLevel,
        JsonArray required) {
    JsonObject jsonSchemaObject = new JsonObject();

    // id, $schema
    if (isFirstLevel) {
        if (jsonSchemaConfig.isPrintId()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.ID.toString(), jsonSchemaConfig.getId());
        }
        jsonSchemaObject.addProperty(JsonSchemaKeywords.SCHEMA.toString(), jsonSchemaConfig.getVersion());
    } else {
        if (jsonSchemaConfig.isPrintId()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.ID.toString(), "/" + elementName);
        }
    }

    // title
    if (jsonSchemaConfig.isPrintTitle()) {
        jsonSchemaObject.addProperty(JsonSchemaKeywords.TITLE.toString(), elementName);// jsonSchemaConfig.getTitle()
    }

    // description
    if (jsonSchemaConfig.isPrintDescription()) {
        jsonSchemaObject.addProperty(JsonSchemaKeywords.DESCRIPTION.toString(),
                jsonSchemaConfig.getDescription());
    }

    // type
    String jsonElementType = JsonValueTypes.getJsonValueType(jsonElement);
    jsonSchemaObject.addProperty(JsonSchemaKeywords.TYPE.toString(), jsonElementType);
    if (jsonElementType.equals(JsonValueTypes.STRING.toString())) {// string
        if (jsonSchemaConfig.isPrintMinLength()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.MINLENGTH.toString(),
                    jsonSchemaConfig.getMinLength());
        }
        if (jsonSchemaConfig.isPrintMaxLength()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXLENGTH.toString(),
                    jsonSchemaConfig.getMaxLength());
        }
        if (jsonSchemaConfig.isPrintDefault()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.DEFAULT.toString(),
                    jsonSchemaConfig.isDefaultFromJson() ? jsonElement.getAsString()
                            : jsonSchemaConfig.getDefaultString());
        }
    }
    if (jsonElementType.equals(JsonValueTypes.NUMBER.toString())) {// number
        if (jsonSchemaConfig.isPrintMinimum()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.MINIMUM.toString(), jsonSchemaConfig.getMinimum());
        }
        if (jsonSchemaConfig.isPrintMaximum()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXIMUM.toString(), jsonSchemaConfig.getMaximum());
        }
        if (jsonSchemaConfig.isPrintExclusiveMinimum()) {
            if (!jsonSchemaConfig.isPrintMinimum()) {
                jsonSchemaObject.addProperty(JsonSchemaKeywords.MINIMUM.toString(),
                        jsonSchemaConfig.getMinimum());
            }
            jsonSchemaObject.addProperty(JsonSchemaKeywords.EXCLUSIVEMINIMUM.toString(),
                    jsonSchemaConfig.isExclusiveMinimum());
        }
        if (jsonSchemaConfig.isPrintExclusiveMaximum()) {
            if (!jsonSchemaConfig.isPrintMaximum()) {
                jsonSchemaObject.addProperty(JsonSchemaKeywords.MAXIMUM.toString(),
                        jsonSchemaConfig.getMaximum());
            }
            jsonSchemaObject.addProperty(JsonSchemaKeywords.EXCLUSIVEMAXIMUM.toString(),
                    jsonSchemaConfig.isExclusiveMaximum());
        }
        if (jsonSchemaConfig.isPrintDefault()) {
            jsonSchemaObject.addProperty(JsonSchemaKeywords.DEFAULT.toString(),
                    jsonSchemaConfig.isDefaultFromJson() ? jsonElement.getAsNumber()
                            : jsonSchemaConfig.getDefaultNumber());
        }
    }

    // required && V3
    if (jsonSchemaConfig.isPrintRequired()
            && JsonSchemaVersions.V3.toString().equals(jsonSchemaConfig.getVersion())) {// V3 required  boolean ???
        jsonSchemaObject.addProperty(JsonSchemaKeywords.REQUIRED.toString(), jsonSchemaConfig.isRequired());
    }
    // required && V4
    if (jsonSchemaConfig.isPrintRequired()
            && JsonSchemaVersions.V4.toString().equals(jsonSchemaConfig.getVersion())
            && (jsonElementType.equals(JsonValueTypes.STRING.toString())
                    || jsonElementType.equals(JsonValueTypes.NUMBER.toString())
                    || jsonElementType.equals(JsonValueTypes.INTEGER.toString())
                    || jsonElementType.equals(JsonValueTypes.BOOLEAN.toString()))) {// V4 required  array ? object 
        required.add(elementName);
    }

    // properties, items
    JsonArray newRequired = new JsonArray();
    if (jsonElementType.equals(JsonValueTypes.OBJECT.toString())
            && !jsonElement.getAsJsonObject().entrySet().isEmpty()) {// object.properties
        JsonObject propertiesObject = new JsonObject();
        for (Map.Entry<String, JsonElement> propertyElemement : jsonElement.getAsJsonObject().entrySet()) {
            propertiesObject.add(propertyElemement.getKey(), makeSchemaElement(propertyElemement.getValue(),
                    propertyElemement.getKey(), false, newRequired));
        }
        jsonSchemaObject.add(JsonSchemaKeywords.PROPERTIES.toString(), propertiesObject);
    } else if (jsonElementType.equals(JsonValueTypes.ARRAY.toString())
            && jsonElement.getAsJsonArray().size() > 0) {// array.items
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        jsonSchemaObject.add(JsonSchemaKeywords.ITEMS.toString(),
                makeSchemaElement(jsonArray.get(0), "0", false, new JsonArray()));

    }

    // required && V4
    if (jsonElementType.equals(JsonValueTypes.OBJECT.toString())
            && JsonSchemaVersions.V4.toString().equals(jsonSchemaConfig.getVersion())) {// object.required
        jsonSchemaObject.add(JsonSchemaKeywords.REQUIRED.toString(), newRequired);
    }

    // minitems , uniqueitems
    if (jsonElementType.equals(JsonValueTypes.ARRAY.toString())) {// array
        if (jsonSchemaConfig.isPrintMinItems()) {// array.minitems
            jsonSchemaObject.addProperty(JsonSchemaKeywords.MINITEMS.toString(),
                    jsonSchemaConfig.getMinItems());
        }
        if (jsonSchemaConfig.isPrintUniqueItems()) {// array.uniqueitems
            jsonSchemaObject.addProperty(JsonSchemaKeywords.UNIQUEITEMS.toString(),
                    jsonSchemaConfig.isUniqueItems());
        }
    }

    return jsonSchemaObject;
}

From source file:com.gmail.tracebachi.DeltaSkins.Bungee.DeltaSkins.java

License:Open Source License

public void onEnable() {
    Configuration config = loadConfig();
    if (config == null) {
        return;/*from   w w  w. j  a  v  a2 s  .c  om*/
    }

    debugLevel = config.getInt("DebugLevel", 2);
    long batchRequestIntervalSeconds = config.getLong("BatchRequestIntervalSeconds", 90);
    long skinCacheDurationMinutes = config.getLong("SkinCacheDurationMinutes", 180);
    long fileSaveInterval = config.getLong("FileSaveInterval", 1800);
    loadFormats(config);

    mojangApi = new MojangApi(this);
    skinApplier = new SkinApplier(this);
    playerProfileStorage = new PlayerProfileStorage();
    skinDataStorage = new SkinDataStorage(skinCacheDurationMinutes);

    // Read player profiles
    File profileFile = new File(getDataFolder(), PROFILES_FILENAME);
    JsonArray playerProfilesArray = readJsonElementFromFile(profileFile, new JsonArray()).getAsJsonArray();
    playerProfileStorage.deserialize(playerProfilesArray);

    // Read skin data
    File skinDataFile = new File(getDataFolder(), SKIN_DATA_FILENAME);
    JsonArray skinDataArray = readJsonElementFromFile(skinDataFile, new JsonArray()).getAsJsonArray();
    skinDataStorage.deserialize(skinDataArray);

    // Schedule repeating tasks
    TaskScheduler scheduler = this.getProxy().getScheduler();
    BatchUuidRunnable uuidRunnable = new BatchUuidRunnable(batchRequestIntervalSeconds, this);
    SaveFilesRunnable fileSaveRunnable = new SaveFilesRunnable(profileFile, skinDataFile, this);
    scheduler.schedule(this, uuidRunnable, 60, 15, TimeUnit.SECONDS);
    scheduler.schedule(this, fileSaveRunnable, 600, fileSaveInterval, TimeUnit.SECONDS);

    // Register login listener
    loginListener = new LoginListener(skinApplier, this);
    getProxy().getPluginManager().registerListener(this, loginListener);

    // Register commands
    commands = new MainCommands(skinApplier, this);
    getProxy().getPluginManager().registerCommand(this, commands);
}

From source file:com.gmail.tracebachi.DeltaSkins.Shared.MojangApi.java

License:Open Source License

public JsonArray getUuids() throws IOException, RateLimitedException {
    synchronized (uuidRequestLock) {
        // If requests are empty, do not make a request
        if (uuidRequests.size() <= 0) {
            return new JsonArray();
        }/*from   ww  w . j  a v a  2s . c om*/
    }

    String stringPayload = convertUuidRequestsToStringPayload();

    HttpURLConnection httpConn = openNameToIdConnection();
    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Content-Type", "application/json");
    OutputStream outputStream = httpConn.getOutputStream();

    byte[] payload = stringPayload.getBytes(StandardCharsets.UTF_8);
    outputStream.write(payload);
    outputStream.flush();
    outputStream.close();
    plugin.debugApi("[Payload] " + stringPayload);

    if (httpConn.getResponseCode() == 429) {
        throw new RateLimitedException();
    }

    StringWriter writer = new StringWriter();
    InputStream inputStream = httpConn.getInputStream();
    IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8);
    inputStream.close();
    String response = writer.toString();
    writer.close();
    plugin.debugApi("[Response] " + response);

    synchronized (parserLock) {
        return parser.parse(response).getAsJsonArray();
    }
}

From source file:com.gmail.tracebachi.DeltaSkins.Shared.MojangApi.java

License:Open Source License

private String convertUuidRequestsToStringPayload() {
    synchronized (uuidRequestLock) {
        Iterator<String> iterator = uuidRequests.iterator();
        JsonArray array = new JsonArray();

        for (int i = 0; i < 100 && iterator.hasNext(); ++i) {
            String next = iterator.next();

            if (next != null && !next.isEmpty()) {
                array.add(new JsonPrimitive(next));
            }//  w ww.  j  ava 2 s. com

            iterator.remove();
        }

        return gson.toJson(array);
    }
}