OSPL20 will be replaced with MPL2.0 after 1st February 2021! Read more here
OSPL18 and OSPL19 will be replaced with MIT after 1st February2021! Read more here

Verified Commit 5448fbed authored by AtjonTV's avatar AtjonTV

extensions: Use generics for toObjectFromType functions on String and URL

parent 0a68f1a9
Pipeline #2048 passed with stages
in 1 minute and 24 seconds
...@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `toObjectOfType` extension for JsonObject * `toObjectOfType` extension for JsonObject
### Changed ### Changed
* Moved and Renamed a lot of packages (Major Breaking Change) * Moved and Renamed a lot of packages (Major Breaking Change)
* `toObjectFromType` extension on String uses Generics
* `toObjectFromType` extension on String uses Generics
### Deprecated ### Deprecated
* kSock (`klib.net.socket.kSock`) * kSock (`klib.net.socket.kSock`)
### Removed ### Removed
......
...@@ -13,7 +13,6 @@ import java.io.File ...@@ -13,7 +13,6 @@ import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.FileOutputStream import java.io.FileOutputStream
import java.lang.reflect.Type
import java.util.Base64 import java.util.Base64
/** /**
...@@ -299,16 +298,21 @@ fun String.loadAsLibraryWithFunction(className: String, functionName: String): L ...@@ -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.json.Json
* @see klib.interfaces.Json
* *
* @since 3.1.0 * @since 3.1.0
* @since 4.0.0 (Generic)
* @author Thomas Obernosterer * @author Thomas Obernosterer
*/ */
fun String.toObjectOfType(type: Type): Any? { inline fun <reified T> String.toObjectOfType(): T? {
return kLibInf.jsonHandler.toObject(this, type) val obj = kLibInf.jsonHandler.toObject(this)
if (obj is T)
return obj
return null
} }
/** /**
......
...@@ -2,7 +2,6 @@ package klib.extensions ...@@ -2,7 +2,6 @@ package klib.extensions
import klib.kLibInf import klib.kLibInf
import java.io.File import java.io.File
import java.lang.reflect.Type
import java.net.URL import java.net.URL
/** /**
...@@ -18,15 +17,19 @@ fun URL.toFile(file: File) { ...@@ -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 kLibInf.jsonHandler
* @see klib.interfaces.Json * @see klib.json.Json
* *
* @since 3.1.0 * @since 3.1.0
* @since 4.0.0 (Generic)
* @author Thomas Obernosterer * @author Thomas Obernosterer
*/ */
fun URL.toObjectOfType(type: Type): Any? { inline fun <reified T> URL.toObjectOfType(): T? {
return kLibInf.jsonHandler.toObject(this.readText(), type) val obj = kLibInf.jsonHandler.toObject(this.readText())
if (obj is T)
return obj
return null
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment