Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
fuookami.ospf.kotlin.utils.serialization.Json.kt Maven / Gradle / Ivy
package fuookami.ospf.kotlin.utils.serialization
import java.io.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import fuookami.ospf.kotlin.utils.meta_programming.*
@OptIn(ExperimentalSerializationApi::class)
class JsonNamingPolicy(
val frontend: NamingSystem,
val backend: NamingSystem
) : JsonNamingStrategy {
val transfer = NameTransfer(frontend, backend)
override fun serialNameForJson(descriptor: SerialDescriptor, elementIndex: Int, serialName: String): String {
return descriptor.serialName.ifBlank {
transfer(serialName)
}
}
}
@OptIn(InternalSerializationApi::class)
inline fun readFromJson(path: String, namingPolicy: JsonNamingPolicy? = null): T {
return readFromJson(T::class.serializer(), path, namingPolicy)
}
@OptIn(InternalSerializationApi::class)
inline fun readFromJsonList(path: String, namingPolicy: JsonNamingPolicy? = null): List {
return readFromJsonList(T::class.serializer(), path, namingPolicy)
}
@OptIn(ExperimentalSerializationApi::class)
fun readFromJson(serializer: KSerializer, path: String, namingPolicy: JsonNamingPolicy? = null): T {
val file = File(path)
val json = Json {
ignoreUnknownKeys = true
if (namingPolicy != null) {
namingStrategy = namingPolicy
}
}
return json.decodeFromStream(serializer, FileInputStream(file))
}
@OptIn(ExperimentalSerializationApi::class)
fun readFromJsonList(serializer: KSerializer, path: String, namingPolicy: JsonNamingPolicy? = null): List {
return readFromJson(ListSerializer(serializer), path, namingPolicy)
}
@OptIn(InternalSerializationApi::class)
inline fun readFromJson(stream: InputStream, namingPolicy: JsonNamingPolicy? = null): T {
return readFromJson(T::class.serializer(), stream, namingPolicy)
}
@OptIn(InternalSerializationApi::class)
inline fun readFromJsonList(stream: InputStream, namingPolicy: JsonNamingPolicy? = null): List {
return readFromJsonList(ListSerializer(T::class.serializer()), stream, namingPolicy)
}
@OptIn(ExperimentalSerializationApi::class)
fun readFromJson(serializer: KSerializer, stream: InputStream, namingPolicy: JsonNamingPolicy? = null): T {
val json = Json {
ignoreUnknownKeys = true
if (namingPolicy != null) {
namingStrategy = namingPolicy
}
}
return json.decodeFromStream(serializer, stream)
}
@OptIn(ExperimentalSerializationApi::class)
fun readFromJsonList(serializer: KSerializer>, stream: InputStream, namingPolicy: JsonNamingPolicy? = null): List {
val json = Json {
ignoreUnknownKeys = true
if (namingPolicy != null) {
namingStrategy = namingPolicy
}
}
return json.decodeFromStream(serializer, stream)
}
@OptIn(InternalSerializationApi::class)
inline fun writeJson(value: T, namingPolicy: JsonNamingPolicy? = null): String {
val stream = ByteArrayOutputStream()
writeJsonToStream(stream, T::class.serializer(), value, namingPolicy)
return stream.toString()
}
fun writeJson(serializer: KSerializer, value: T, namingPolicy: JsonNamingPolicy? = null): String {
val stream = ByteArrayOutputStream()
writeJsonToStream(stream, serializer, value, namingPolicy)
return stream.toString()
}
@OptIn(InternalSerializationApi::class)
inline fun writeJsonToFile(path: String, value: T, namingPolicy: JsonNamingPolicy? = null) {
writeJsonToStream(File(path).outputStream(), T::class.serializer(), value, namingPolicy)
}
fun writeJsonToFile(serializer: KSerializer, path: String, value: List, namingPolicy: JsonNamingPolicy? = null) {
writeJsonToStream(File(path).outputStream(), ListSerializer(serializer), value, namingPolicy)
}
fun writeJsonToFile(path: String, serializer: KSerializer, value: T, namingPolicy: JsonNamingPolicy? = null) {
writeJsonToStream(File(path).outputStream(), serializer, value, namingPolicy)
}
fun writeJsonToFile(path: String, serializer: KSerializer>, value: List, namingPolicy: JsonNamingPolicy? = null) {
writeJsonToStream(File(path).outputStream(), serializer, value, namingPolicy)
}
@OptIn(InternalSerializationApi::class)
inline fun writeJsonToStream(
stream: OutputStream,
value: T,
namingPolicy: JsonNamingPolicy? = null
) {
writeJsonToStream(stream, T::class.serializer(), value, namingPolicy)
}
@OptIn(InternalSerializationApi::class)
inline fun writeJsonToStream(
stream: OutputStream,
value: List,
namingPolicy: JsonNamingPolicy? = null
) {
writeJsonToStream(stream, ListSerializer(T::class.serializer()), value, namingPolicy)
}
@OptIn(ExperimentalSerializationApi::class)
fun writeJsonToStream(
stream: OutputStream,
serializer: KSerializer,
value: T,
namingPolicy: JsonNamingPolicy? = null
) {
val json = Json {
ignoreUnknownKeys = true
if (namingPolicy != null) {
namingStrategy = namingPolicy
}
}
json.encodeToStream(serializer, value, stream)
}
@OptIn(ExperimentalSerializationApi::class)
fun writeJsonToStream(
stream: OutputStream,
serializer: KSerializer>,
value: List,
namingPolicy: JsonNamingPolicy? = null
) {
val json = Json {
ignoreUnknownKeys = true
if (namingPolicy != null) {
namingStrategy = namingPolicy
}
}
json.encodeToStream(serializer, value, stream)
}