Java Code Examples for com.eclipsesource.json.JsonArray#values()

The following examples show how to use com.eclipsesource.json.JsonArray#values() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelfUpdater.java    From Recaf with MIT License 5 votes vote down vote up
/**
 * Fetch the {@link #latestVersion latest version}
 * and {@link #latestArtifact latest artifact url}.
 *
 * @throws IOException
 * 		When opening a connection to the API url fails.
 */
private static void fetchLatestInfo() throws IOException {
	URL updateURL = new URL(API);
	String content = IOUtils.toString(updateURL.openStream(), StandardCharsets.UTF_8);
	JsonObject updateJson = Json.parse(content).asObject();
	// compare versions
	latestVersion = updateJson.getString("tag_name", "2.0.0");
	latestPatchnotes = updateJson.getString("body", "#Error\nCould not fetch update notes.");
	if (isOutdated()) {
		Log.info(LangUtil.translate("update.outdated"));
		JsonArray assets = updateJson.get("assets").asArray();
		for(JsonValue assetValue : assets.values()) {
			JsonObject assetObj = assetValue.asObject();
			String file = assetObj.getString("name", "invalid");
			// Skip non-jars
			if (!file.endsWith(".jar")) {
				continue;
			}
			// Find the largest jar
			int size = assetObj.getInt("size", 0);
			if (size > latestArtifactSize) {
				latestArtifactSize = size;
				String fileURL = assetObj.getString("browser_download_url", null);
				if (fileURL != null)
					latestArtifact = fileURL;
			}
		}
		try {
			String date = updateJson.getString("published_at", null);
			if (date != null)
				latestVersionDate = Instant.parse(date);
		} catch(DateTimeParseException ex) {
			Log.warn("Failed to parse timestamp for latest release");
		}
		if (latestArtifact == null)
			Log.warn(LangUtil.translate("update.fail.nodownload"));
	}
}
 
Example 2
Source File: PlayerAction.java    From microrts with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a PlayerAction from a JSON object
 * @param JSON
 * @param gs
 * @param utt
 * @return
 */
public static PlayerAction fromJSON(String JSON, GameState gs, UnitTypeTable utt) {
    PlayerAction pa = new PlayerAction();
    JsonArray a = Json.parse(JSON).asArray();
    for(JsonValue v:a.values()) {
        JsonObject o = v.asObject();
        int id = o.getInt("unitID", -1);
        Unit u = gs.getUnit(id);
        UnitAction ua = UnitAction.fromJSON(o.get("unitAction").asObject(), utt);
        pa.addUnitAction(u, ua);
    }
    return pa;
}
 
Example 3
Source File: BoxUser.java    From box-java-sdk with Apache License 2.0 5 votes vote down vote up
private Map<String, String> parseTrackingCodes(JsonArray jsonArray) {
    Map<String, String> result = new HashMap<String, String>();
    if (jsonArray == null) {
        return null;
    }
    List<JsonValue> valuesList = jsonArray.values();
    for (JsonValue jsonValue : valuesList) {
        JsonObject object = jsonValue.asObject();
        result.put(object.get("name").asString().toString(), object.get("value").asString().toString());
    }
    return result;
}
 
Example 4
Source File: VariableArrayJsonRule.java    From dungeon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void validate(JsonValue value) {
  super.validate(value);
  JsonArray array = value.asArray();
  for (JsonValue arrayValue : array.values()) {
    rule.validate(arrayValue);
  }
}
 
Example 5
Source File: JsonItemPresetFactory.java    From dungeon with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Collection<ItemPreset> getItemPresets() {
  JsonObject objects = JsonObjectFactory.makeJsonObject(filename);
  Collection<ItemPreset> itemPresets = new ArrayList<>();
  for (JsonValue value : objects.get("items").asArray()) {
    JsonObject itemObject = value.asObject();
    ItemPreset preset = new ItemPreset();
    Id id = new Id(itemObject.get("id").asString());
    preset.setId(id);
    preset.setType(itemObject.get("type").asString());
    preset.setName(NameFactory.fromJsonObject(itemObject.get("name").asObject()));
    preset.setRarity(Rarity.valueOf(itemObject.get("rarity").asString()));
    preset.setTagSet(new TagSetParser<>(Item.Tag.class, itemObject.get("tags")).parse());
    if (itemObject.get("enchantments") != null) {
      for (JsonValue enchantment : itemObject.get("enchantments").asArray()) {
        Id enchantmentId = new Id(enchantment.asObject().get("id").asString());
        double probability = enchantment.asObject().get("probability").asDouble();
        preset.getEnchantmentRules().add(enchantmentId, probability);
      }
    }
    preset.setUnique(itemObject.getBoolean("unique", false));
    if (itemObject.get("decompositionPeriod") != null) {
      long seconds = DungeonTimeParser.parseDuration(itemObject.get("decompositionPeriod").asString()).getSeconds();
      preset.setPutrefactionPeriod(seconds);
    }
    JsonObject integrity = itemObject.get("integrity").asObject();
    preset.setIntegrity(new Integrity(integrity.get("current").asInt(), integrity.get("maximum").asInt()));
    preset.setVisibility(Percentage.fromString(itemObject.get("visibility").asString()));
    if (itemObject.get("luminosity") != null) {
      preset.setLuminosity(new Luminosity(Percentage.fromString(itemObject.get("luminosity").asString())));
    }
    preset.setWeight(Weight.newInstance(itemObject.get("weight").asDouble()));
    preset.setDamage(itemObject.get("damage").asInt());
    preset.setHitRate(Percentage.fromString(itemObject.get("hitRate").asString()));
    preset.setIntegrityDecrementOnHit(itemObject.get("integrityDecrementOnHit").asInt());
    if (itemObject.get("nutrition") != null) {
      preset.setNutrition(itemObject.get("nutrition").asInt());
    }
    if (itemObject.get("integrityDecrementOnEat") != null) {
      preset.setIntegrityDecrementOnEat(itemObject.get("integrityDecrementOnEat").asInt());
    }
    if (preset.getTagSet().hasTag(Tag.BOOK)) {
      preset.setText(itemObject.get("text").asString());
    }
    if (preset.getTagSet().hasTag(Tag.DRINKABLE)) {
      preset.setDrinkableDoses(itemObject.get("drinkableDoses").asInt());
      for (JsonValue effectValue : itemObject.get("drinkableEffects").asArray()) {
        JsonArray effectArray = effectValue.asArray();
        List<JsonValue> values = effectArray.values();
        Id effectId = new Id(values.get(0).asString());
        List<String> effectParameters = new ArrayList<>();
        for (int i = 1; i < values.size(); i++) {
          if (values.get(i).isString()) {
            // Calling toString() makes a JSON String, which includes the quotation marks.
            effectParameters.add(values.get(i).asString());
          } else {
            effectParameters.add(values.get(i).toString());
          }
        }
        preset.addDrinkableEffect(effectId, effectParameters);
      }
      preset.setIntegrityDecrementPerDose(itemObject.get("integrityDecrementPerDose").asInt());
    }
    if (itemObject.get("spell") != null) {
      preset.setSpellId(itemObject.get("spell").asString());
    }
    itemPresets.add(preset);
  }
  DungeonLogger.info("Loaded " + itemPresets.size() + " item presets.");
  return itemPresets;
}