카테고리 없음

JsonObject 객체에서 안전하게 값 꺼내오기

그란. 2021. 9. 2. 15:37
private fun JSONObject.getLongOrNull(name: String): Long?{
	return this.getOrNull(name){ this.getLong(name) }
}

private fun JSONObject.getIntOrNull(name: String): Int?{
	return this.getOrNull(name){ this.getInt(name) }
}

private inline fun<T: Any> JSONObject.getOrNull(name: String, block: () -> T): T?{
	return if(this.has(name)) block.invoke() else null
}