diff --git a/CHANGELOG.md b/CHANGELOG.md index c6325e2e30979df40ba3d23f6bda9493ca16a995..0c27b4533a0a4c72c6c46e68bd4c1da04e65862b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `toObjectOfType` extension for JsonObject ### Changed * Moved and Renamed a lot of packages (Major Breaking Change) +* `toObjectFromType` extension on String uses Generics +* `toObjectFromType` extension on String uses Generics ### Deprecated * kSock (`klib.net.socket.kSock`) ### Removed diff --git a/src/main/kotlin/klib/extensions/String.kt b/src/main/kotlin/klib/extensions/String.kt index ea3d99e48d1ef9c8caba605a60778fa01c61db5a..7eb3a5687d97e5e566505447930c45bf8b5bb48c 100644 --- a/src/main/kotlin/klib/extensions/String.kt +++ b/src/main/kotlin/klib/extensions/String.kt @@ -13,7 +13,6 @@ import java.io.File import java.io.FileInputStream import java.io.FileNotFoundException import java.io.FileOutputStream -import java.lang.reflect.Type import java.util.Base64 /** @@ -299,16 +298,21 @@ fun String.loadAsLibraryWithFunction(className: String, functionName: String): L } /** - * Parse to Type + * Parse to Type T * - * @param type The type to parse - * @see klib.interfaces.Json + * @see klib.json.Json * * @since 3.1.0 + * @since 4.0.0 (Generic) * @author Thomas Obernosterer */ -fun String.toObjectOfType(type: Type): Any? { - return kLibInf.jsonHandler.toObject(this, type) +inline fun String.toObjectOfType(): T? { + val obj = kLibInf.jsonHandler.toObject(this) + + if (obj is T) + return obj + + return null } /** diff --git a/src/main/kotlin/klib/extensions/URL.kt b/src/main/kotlin/klib/extensions/URL.kt index 681c1ab8883ce4f668af083a33b5cf24c47f18f5..370da5ede4857f92fcdf0f596aa066fed1af45c1 100644 --- a/src/main/kotlin/klib/extensions/URL.kt +++ b/src/main/kotlin/klib/extensions/URL.kt @@ -2,7 +2,6 @@ package klib.extensions import klib.kLibInf import java.io.File -import java.lang.reflect.Type import java.net.URL /** @@ -18,15 +17,19 @@ fun URL.toFile(file: File) { } /** - * Download json and parse to Type + * Download json and parse to Type T * - * @param type The type to parse to * @see kLibInf.jsonHandler - * @see klib.interfaces.Json + * @see klib.json.Json * * @since 3.1.0 + * @since 4.0.0 (Generic) * @author Thomas Obernosterer */ -fun URL.toObjectOfType(type: Type): Any? { - return kLibInf.jsonHandler.toObject(this.readText(), type) +inline fun URL.toObjectOfType(): T? { + val obj = kLibInf.jsonHandler.toObject(this.readText()) + if (obj is T) + return obj + + return null } \ No newline at end of file