commit 24/09/2025

This commit is contained in:
2025-09-24 11:48:36 +07:00
parent 59dd67acc8
commit 55f6a24cce
7 changed files with 156 additions and 15 deletions

View File

@@ -1,5 +1,7 @@
package codes
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import content.ScheduleDay
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -31,6 +33,47 @@ class Somecodes {
const val MB_threshold = KB_threshold * 1024.0
const val GB_threshold = MB_threshold * 1024.0
const val TB_threshold = GB_threshold * 1024.0
val objectmapper = jacksonObjectMapper()
/**
* Convert an object to a JSON string.
* @param data The object to convert.
* @return A JSON string representation of the object, or "{}" if conversion fails.
*/
fun toJsonString(data: Any) : String {
return try {
objectmapper.writeValueAsString(data)
} catch (e: Exception){
"{}"
}
}
/**
* Convert a JSON string to an object of the specified class.
* @param json The JSON string to convert.
* @param clazz The class of the object to convert to.
* @return An object of the specified class, or null if conversion fails.
*/
fun <T> fromJsonString(json: String, clazz: Class<T>) : T? {
return try {
objectmapper.readValue(json, clazz)
} catch (e: Exception){
null
}
}
/**
* Convert a JSON string to a JsonNode.
* @param data The JSON string to convert.
* @return A JsonNode representation of the JSON string, or empty JsonNode if conversion fails.
*/
fun toJsonNode(data: String) : JsonNode {
return try {
objectmapper.readTree(data)
} catch (e: Exception){
objectmapper.createObjectNode()
}
}
/**
* List all audio files (.mp3 and .wav) in the specified directory and its subdirectories.