add more extensions for Jankson

This commit is contained in:
nikky 2018-06-28 07:21:05 +02:00
parent 19e4136d88
commit 0ebb89cfa8
3 changed files with 19 additions and 15 deletions

View File

@ -1,5 +1,6 @@
package matterlink
import blue.endless.jankson.Jankson
import blue.endless.jankson.JsonObject
import matterlink.config.cfg
import java.io.PrintWriter
@ -60,3 +61,7 @@ fun <T : Any> JsonObject.getOrDefault(key: String, default: T, comment: String?
// instance.info("type: ${default.javaClass.name} key: $key json: >>>${this.getObject(key)?.toJson()}<<< default: $default")
return putDefault(key, default, comment)!!
}
inline fun <reified T: Any> Jankson.Builder.registerTypeAdapter(noinline adapter: (JsonObject) -> T) = this.registerTypeAdapter(T::class.java, adapter)
inline fun <reified T: Any> Jankson.Builder.registerPrimitiveTypeAdapter(noinline adapter: (Any) -> T) = this.registerPrimitiveTypeAdapter(T::class.java, adapter)

View File

@ -7,6 +7,7 @@ import blue.endless.jankson.impl.SyntaxError
import matterlink.bridge.MessageHandlerInst
import matterlink.getOrDefault
import matterlink.instance
import matterlink.registerTypeAdapter
import matterlink.stackTraceString
import java.io.File
import java.io.FileNotFoundException
@ -119,7 +120,7 @@ data class BaseConfig(val rootDir: File) {
companion object {
val jankson = Jankson
.builder()
.registerTypeAdapter(MatterLinkConfig::class.java) {
.registerTypeAdapter {
MatterLinkConfig(
command = it.getOrDefault(
"command",
@ -161,7 +162,7 @@ data class BaseConfig(val rootDir: File) {
)
)
}
.registerTypeAdapter(CommandOptions::class.java) {
.registerTypeAdapter {
with(CommandOptions()) {
CommandOptions(
enable = it.getOrDefault(
@ -182,7 +183,7 @@ data class BaseConfig(val rootDir: File) {
)
}
}
.registerTypeAdapter(ConnectOptions::class.java) {
.registerTypeAdapter {
with(ConnectOptions()) {
ConnectOptions(
url = it.getOrDefault(
@ -208,14 +209,14 @@ data class BaseConfig(val rootDir: File) {
)
}
}
.registerTypeAdapter(DebugOptions::class.java) {
.registerTypeAdapter {
with(DebugOptions()) {
DebugOptions(
logLevel = it.getOrDefault("loglevel", logLevel, "MatterLink log level")
)
}
}
.registerTypeAdapter(IncomingOptions::class.java) {
.registerTypeAdapter {
with(IncomingOptions()) {
IncomingOptions(
chat = it.getOrDefault(
@ -241,7 +242,7 @@ data class BaseConfig(val rootDir: File) {
)
}
}
.registerTypeAdapter(OutgoingOptions::class.java) {
.registerTypeAdapter {
with(OutgoingOptions()) {
OutgoingOptions(
systemUser = it.getOrDefault(
@ -282,7 +283,7 @@ data class BaseConfig(val rootDir: File) {
)
}
}
.registerTypeAdapter(DeathOptions::class.java) { jsonObj ->
.registerTypeAdapter { jsonObj ->
with(DeathOptions()) {
DeathOptions(
enable = jsonObj.getOrDefault(
@ -312,9 +313,7 @@ data class BaseConfig(val rootDir: File) {
)
}
}
.registerTypeAdapter(JoinPartOptions::
class.java)
{
.registerTypeAdapter {
with(JoinPartOptions()) {
JoinPartOptions(
enable = it.getOrDefault(
@ -337,9 +336,7 @@ data class BaseConfig(val rootDir: File) {
)
}
}
.registerTypeAdapter(UpdateOptions::
class.java)
{
.registerTypeAdapter {
with(UpdateOptions()) {
UpdateOptions(
enable = it.getOrDefault(

View File

@ -8,6 +8,8 @@ import matterlink.bridge.command.CommandType
import matterlink.bridge.command.CustomCommand
import matterlink.getOrDefault
import matterlink.instance
import matterlink.registerPrimitiveTypeAdapter
import matterlink.registerTypeAdapter
import java.io.File
import java.io.FileNotFoundException
@ -74,7 +76,7 @@ object CommandConfig {
fun readConfig(): Boolean {
val jankson = Jankson
.builder()
.registerTypeAdapter(CustomCommand::class.java) { jsonObj ->
.registerTypeAdapter { jsonObj ->
with(CustomCommand.DEFAULT) {
CustomCommand(
type = jsonObj.get(CommandType::class.java, "type") ?: type,
@ -89,7 +91,7 @@ object CommandConfig {
)
}
}
.registerPrimitiveTypeAdapter(Regex::class.java) {
.registerPrimitiveTypeAdapter {
it.toString().toRegex()
}
.build()