Java Code Examples for com.eclipsesource.json.JsonValue#asInt()

The following examples show how to use com.eclipsesource.json.JsonValue#asInt() . 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: FractionListParser.java    From thorntail with Apache License 2.0 6 votes vote down vote up
private FractionDescriptor getFractionDescriptor(JsonObject fraction) {
    String groupId = toString(fraction.get("groupId"));
    String artifactId = toString(fraction.get("artifactId"));
    String key = groupId + ":" + artifactId;
    FractionDescriptor descriptor = descriptors.get(key);
    if (descriptor == null) {
        String version = toString(fraction.get("version"));
        String name = toString(fraction.get("name"));
        String description = toString(fraction.get("description"));
        String tags = toString(fraction.get("tags"));
        boolean internal = toBoolean(fraction.get("internal"));

        JsonValue stabilityIndexJson = fraction.get("stabilityIndex");
        int stabilityIndex = stabilityIndexJson == null || stabilityIndexJson.isNull() ? FractionStability.UNSTABLE.ordinal() : stabilityIndexJson.asInt();
        FractionStability stability;
        if (stabilityIndex < 0 || stabilityIndex >= FractionStability.values().length) {
            stability = FractionStability.UNSTABLE;
        } else {
            stability = FractionStability.values()[stabilityIndex];
        }
        descriptor = new FractionDescriptor(groupId, artifactId, version, name, description, tags, internal, stability);
        descriptors.put(key, descriptor);
    }
    return descriptor;
}
 
Example 2
Source File: WebServer.java    From r2cloud with Apache License 2.0 5 votes vote down vote up
public static Integer getInteger(JsonValue value, String name) {
	JsonValue result = ((JsonObject) value).get(name);
	if (result == null || result.isNull()) {
		return null;
	}
	if (result.isString()) {
		return Integer.parseInt(result.asString());
	}
	if (!result.isNumber()) {
		throw new NumberFormatException();
	}
	return result.asInt();
}
 
Example 3
Source File: SdkUtils.java    From box-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Parse a given JsonValue to an int regardless of whether that value is a String or an int.
 *
 * @param value a JsonValue to parse to an int.
 * @return an int representation of the given value. Can throw a runtime exception (ParseException, UnsupportedOperationException, or NumberFormatException).
 */
public static long parseJsonValueToInteger(JsonValue value) {
    try {
        return value.asInt();
    } catch (UnsupportedOperationException e) {
        String s = value.asString().replace("\"", "");
        return Integer.parseInt(s);
    }
}
 
Example 4
Source File: BoxJsonObject.java    From box-android-sdk with Apache License 2.0 5 votes vote down vote up
public Integer getAsInt(final String field){
    JsonValue value = getAsJsonValue(field);
    if (value == null || value.isNull()) {
        return null;
    }
    return value.asInt();
}
 
Example 5
Source File: JsonConfig.java    From ServerSync with GNU General Public License v3.0 5 votes vote down vote up
private static int getInt(JsonObject root, String name) throws IOException {
    JsonValue jsv = root.get(name);
    if (jsv.isNull()) {
        throw new IOException(String.format("No %s value present in configuration file", name));
    }
    if (!jsv.isNumber()) {
        throw new IOException(String.format("Invalid value for %s, expected integer", name));
    }
    return jsv.asInt();
}
 
Example 6
Source File: IntegerJsonRule.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);
  try {
    value.asInt();
  } catch (NumberFormatException exception) {
    throw new IllegalArgumentException(value + " is not an integer.");
  }
}
 
Example 7
Source File: BoundIntegerJsonRule.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);
  int intValue = value.asInt();
  if (intValue < minValue) {
    throw new IllegalArgumentException(value + " is below the allowed minimum of " + minValue);
  }
  if (intValue > maxValue) {
    throw new IllegalArgumentException(value + " is above the allowed maximum of " + maxValue);
  }
}
 
Example 8
Source File: JsonHelper.java    From typescript.java with MIT License 4 votes vote down vote up
public static Integer getInteger(JsonObject obj, String name) {
	JsonValue value = obj.get(name);
	return value != null ? value.asInt() : null;
}
 
Example 9
Source File: BlockJson.java    From Cubes with MIT License 4 votes vote down vote up
@Override
public Integer parse(JsonValue prop) {
  return prop.asInt();
}
 
Example 10
Source File: IntegerSetting.java    From Cubes with MIT License 4 votes vote down vote up
@Override
public void readJson(JsonValue json) {
  this.i = json.asInt();
  clamp();
  onChange();
}
 
Example 11
Source File: KeybindSetting.java    From Cubes with MIT License 4 votes vote down vote up
@Override
public void readJson(JsonValue json) {
  key = json.asInt();
}