Skip to content

Commit

Permalink
Feat: Improve gson api (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
amadeusmz authored Jun 8, 2024
1 parent 1f95eaf commit e949baf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

allprojects {
group = "net.minevn"
version = "1.1.0"
version = "1.1.3"

apply(plugin = "java")
apply(plugin = "org.jetbrains.kotlin.jvm")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.minevn.libs.bukkit

import com.google.gson.JsonElement
import com.google.gson.JsonParser
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Location
Expand All @@ -19,10 +17,6 @@ fun runSync(action: Runnable) {
}
}

fun String.parseJson() = JsonParser.parseString(this)!!

fun JsonElement.getOrNull() = if (isJsonNull) null else this

fun Player.sendMessages(messages: List<String>) = messages.forEach { sendMessage(it) }

fun Player.sendMessages(messages: Array<String>) = messages.forEach { sendMessage(it) }
Expand Down
18 changes: 17 additions & 1 deletion minevnlib-master/src/main/java/net/minevn/libs/MineVNUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.minevn.libs

import com.google.gson.Gson
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import java.time.YearMonth
import java.time.ZoneId
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -69,4 +72,17 @@ fun minMaxEpochTimestamp(monthYear: String): Pair<Long, Long> {
.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()

return Pair(startOfMonth, endOfMonth)
}
}

private val gson: Gson = Gson()
fun Any.toJson() = gson.toJson(this)!!
fun String.parseJson() = JsonParser.parseString(this)!!
fun JsonElement?.getOrNull() = if (this == null || this.isJsonNull) null else this
fun JsonElement?.asBooleanOrNull() = this?.getOrNull()?.asBoolean
fun JsonElement?.asIntOrNull() = this?.getOrNull()?.asInt
fun JsonElement?.asLongOrNull() = this?.getOrNull()?.asLong
fun JsonElement?.asDoubleOrNull() = this?.getOrNull()?.asDouble
fun JsonElement?.asFloatOrNull() = this?.getOrNull()?.asFloat
fun JsonElement?.asShortOrNull() = this?.getOrNull()?.asShort
fun JsonElement?.asStringOrNull() = this?.getOrNull()?.asString
fun JsonElement?.asArrayOrNull() = this?.getOrNull()?.asJsonArray

0 comments on commit e949baf

Please sign in to comment.