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 File: JsonUtils.java    From StatsAgg with 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 2
Source File: GsonFactory.java    From DanDanPlayForAndroid with 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 3
Source File: DirectoryUserDataSource.java    From MCAuthenticator with GNU General Public License v3.0 6 votes vote down vote up
@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 4
Source File: Error.java    From graphql_java_gen with 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 5
Source File: OriginalAudioStream.java    From lancoder with GNU General Public License v3.0 6 votes vote down vote up
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 6
Source File: Configuration.java    From supbot with 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 7
Source File: InboundRequestHandler.java    From floobits-intellij with Apache License 2.0 5 votes vote down vote up
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 8
Source File: GsonUtil.java    From star-zone-android with 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
Source File: CommonSupplementReader.java    From moql with 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 10
Source File: DiagnosticsPathNode.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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 11
Source File: LanguageConfiguration.java    From tm4e with Eclipse Public License 1.0 5 votes vote down vote up
private static Integer getAsInt(JsonElement element) {
	if (element == null) {
		return null;
	}
	try {
		return element.getAsInt();
	} catch (Exception e) {
		return null;
	}
}
 
Example 12
Source File: Element.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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 13
Source File: JsonUtil.java    From The-5zig-Mod with 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 14
Source File: BooleanSerializer.java    From mq-http-java-sdk with 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 15
Source File: BooleanSerializer.java    From prayer-times-android with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public Boolean deserialize(@NonNull JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    return arg0.getAsInt() == 1;
}
 
Example 16
Source File: BooleanSerializer.java    From prayer-times-android with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public Boolean deserialize(@NonNull JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
    return arg0.getAsInt() == 1;
}
 
Example 17
Source File: BooleanSerializer.java    From mq-http-java-sdk with 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
Source File: JsonUtils.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 4 votes vote down vote up
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
Source File: JsonUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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 File: EResultDeserializer.java    From JavaSteam with 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);
}