Java Code Examples for com.google.gson.JsonElement#getAsInt()

The following examples show how to use com.google.gson.JsonElement#getAsInt() . 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 Project: graphql_java_gen   File: Error.java    License: MIT License 6 votes vote down vote up
public Error(JsonObject fields) {
    JsonElement message = fields.get("message");
    if (message != null && message.isJsonPrimitive() && message.getAsJsonPrimitive().isString()) {
        this.message = message.getAsString();
    } else {
        this.message = "Unknown error";
    }

    JsonElement line = fields.get("line");
    if (line != null && line.isJsonPrimitive() && line.getAsJsonPrimitive().isNumber()) {
        this.line = line.getAsInt();
    } else {
        this.line = 0;
    }

    JsonElement column = fields.get("column");
    if (column != null && column.isJsonPrimitive() && column.getAsJsonPrimitive().isNumber()) {
        this.column = column.getAsInt();
    } else {
        this.column = 0;
    }
}
 
Example 2
Source Project: supbot   File: Configuration.java    License: MIT License 6 votes vote down vote up
/**
 * Generalized private method to get value from the saved name value pair configuration
 * @param name name
 * @param _defautlt default value
 * @return returns value from the configuration, returns _default if not found
 */
private Object GetConfig(String name, Object _defautlt){
    try {
        JsonObject jsonObject = ReadJsonObject();

        JsonElement result = jsonObject.get(name);

        if(_defautlt.getClass() == String.class)
            return result.getAsString();

        if(_defautlt.getClass() == Integer.class)
            return result.getAsInt();

        if(_defautlt.getClass() == Float.class)
            return result.getAsFloat();

        if(_defautlt.getClass() == Boolean.class)
            return result.getAsBoolean();

    } catch (IOException | NullPointerException e) {
        Log.p(e);
        //Log.e("Could not find config file or config, returning default");
    }
    return _defautlt;
}
 
Example 3
public OriginalAudioStream(JsonObject json, String relativeFile, long unitCount) {
	super(json, relativeFile, unitCount);
	JsonElement element = null;
	// Convert msec to sec
	this.unitCount = unitCount / 1000;
	if ((element = json.get("bit_rate")) != null) {
		// convert from bit/s to kbps
		this.bitrate = element.getAsInt() / 1000;
	}
	if ((element = json.get("channels")) != null) {
		this.channels = ChannelDisposition.getDispositionFromCount(element.getAsInt());
	}
	if ((element = json.get("sample_rate")) != null) {
		this.sampleRate = element.getAsInt();
	}
}
 
Example 4
Source Project: StatsAgg   File: JsonUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Integer getIntegerFieldFromJsonObject(JsonObject jsonObject, String fieldName) {
    
    if (jsonObject == null) {
        return null;
    }
    
    Integer returnInteger = null;
    
    try {
        JsonElement jsonElement = jsonObject.get(fieldName);
        if (jsonElement != null) returnInteger = jsonElement.getAsInt();
    }
    catch (Exception e) {
        logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));    
    }

    return returnInteger;
}
 
Example 5
@Override
public UserData getUser(UUID id) throws IOException {
    if (Bukkit.isPrimaryThread() && !MCAuthenticator.isReload) throw new RuntimeException("Primary thread I/O");
    File f = getUserFile(id);
    if (!f.exists()) return null;

    try (FileReader reader = new FileReader(f)) {
        JsonObject jsonObject = gson.fromJson(reader, JsonObject.class);
        JsonElement lastIp = jsonObject.get("lastIp");
        JsonElement secret = jsonObject.get("secret");
        JsonElement authType = jsonObject.get("authtype");
        return new UpdatableFlagData(updateHook,
                UUID.fromString(jsonObject.get("id").getAsString()),
                lastIp != null ? InetAddress.getByName(lastIp.getAsString()) : null,
                secret != null ? secret.getAsString() : null,
                authType != null ? authType.getAsInt(): 0,
                jsonObject.get("locked").getAsBoolean());
    }
}
 
Example 6
Source Project: DanDanPlayForAndroid   File: GsonFactory.java    License: MIT License 6 votes vote down vote up
@Override
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    try {
        if (json.getAsString().equals("") || json.getAsInt() == 0) {
            return false;
        } else if (json.getAsInt() == 1) {
            return true;
        }
    } catch (Exception ignore) {
    }
    try {
        return json.getAsBoolean();
    } catch (Exception e) {
        throw new JsonSyntaxException(e);
    }
}
 
Example 7
private static Integer getAsInt(JsonElement element) {
	if (element == null) {
		return null;
	}
	try {
		return element.getAsInt();
	} catch (Exception e) {
		return null;
	}
}
 
Example 8
Source Project: star-zone-android   File: GsonUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Integer deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    try {
        //定义为int类型,如果后台返回""或者null,则返回0
        if (json.getAsString().equals("") || json.getAsString().equals("null")) {
            return 0;
        }
    } catch (Exception ignore) {
    }
    try {
        return json.getAsInt();
    } catch (NumberFormatException e) {
        throw new JsonSyntaxException(e);
    }
}
 
Example 9
void _on_part(JsonObject obj) {
    JsonElement id = obj.get("user_id");
    if (id == null){
        return;
    }
    Integer userId = id.getAsInt();
    FlooUser user = state.users.get(userId);
    if (user == null) {
        return;
    }
    state.removeUser(user.user_id);
    context.removeUser(user);
}
 
Example 10
Source Project: moql   File: CommonSupplementReader.java    License: Apache License 2.0 5 votes vote down vote up
protected void readHits(JsonObject hits) {
  JsonElement jValue = hits.getAsJsonPrimitive("total");
  totalHits = jValue.getAsInt();
  jValue = hits.get("max_score");
  if (!jValue.isJsonNull())
    maxScore = jValue.getAsDouble();
  JsonArray hitArray = hits.getAsJsonArray("hits");
  for (int i = 0; i < hitArray.size(); i++) {
    JsonObject jo = (JsonObject) hitArray.get(i);
    this.hitSupplements.add(readHit(jo));
  }
}
 
Example 11
/**
 * Returns the index of the child that continues the path if any.
 */
public int getChildIndex() {
  final JsonElement childIndex = json.get("childIndex");
  if (childIndex.isJsonNull()) {
    return -1;
  }
  return childIndex.getAsInt();
}
 
Example 12
@NonNull
@Override
public Boolean deserialize(@NonNull JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    return arg0.getAsInt() == 1;
}
 
Example 13
Source Project: mq-http-java-sdk   File: BooleanSerializer.java    License: MIT License 4 votes vote down vote up
@Override
public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    return arg0.getAsInt() == 1;
}
 
Example 14
Source Project: The-5zig-Mod   File: JsonUtil.java    License: MIT License 4 votes vote down vote up
public static int getInt(JsonObject object, String name) {
	JsonElement element = object.get(name);
	if (element == null || element.isJsonNull())
		return 0;
	return element.getAsInt();
}
 
Example 15
/**
 * A utility method to handle null values and JsonNull values.
 */
int getAsInt(String name) {
  final JsonElement element = json.get(name);
  return (element == null || element == JsonNull.INSTANCE) ? -1 : element.getAsInt();
}
 
Example 16
@NonNull
@Override
public Boolean deserialize(@NonNull JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    return arg0.getAsInt() == 1;
}
 
Example 17
Source Project: mq-http-java-sdk   File: BooleanSerializer.java    License: MIT License 4 votes vote down vote up
@Override
public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    return arg0.getAsInt() == 1;
}
 
Example 18
public static int getAsInt(JsonElement jsonElement, String s) {
	if (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber()) {
		return jsonElement.getAsInt();
	}
	throw new JsonSyntaxException("Expected " + s + " to be a Int, was " + toString(jsonElement));
}
 
Example 19
public static int getIntMember(@NotNull JsonObject json, @NotNull String memberName) {
  if (!json.has(memberName)) return -1;

  final JsonElement value = json.get(memberName);
  return value instanceof JsonNull ? -1 : value.getAsInt();
}
 
Example 20
Source Project: JavaSteam   File: EResultDeserializer.java    License: MIT License 4 votes vote down vote up
@Override
public EResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    int code = json.getAsInt();
    return EResult.from(code);
}