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
* `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
......
......@@ -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 <reified T> String.toObjectOfType(): T? {
val obj = kLibInf.jsonHandler.toObject(this)
if (obj is T)
return obj
return null
}
/**
......
......@@ -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 <reified T> URL.toObjectOfType(): T? {
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