All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.serialization.Json.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy