Jackson
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "2.1.10"
|
kotlin("jvm") version "2.1.10"
|
||||||
kotlin("plugin.serialization") version "2.1.10"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "com.subman"
|
group = "com.subman"
|
||||||
@@ -13,8 +12,9 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation(kotlin("test"))
|
testImplementation(kotlin("test"))
|
||||||
implementation("com.charleskorn.kaml:kaml:0.72.0")
|
implementation("com.fasterxml.jackson.core:jackson-databind:2.18.3")
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
|
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.18.3")
|
||||||
|
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.18.3")
|
||||||
implementation("io.ktor:ktor-client-core:3.1.1")
|
implementation("io.ktor:ktor-client-core:3.1.1")
|
||||||
implementation("io.ktor:ktor-client-cio:3.1.1")
|
implementation("io.ktor:ktor-client-cio:3.1.1")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,54 +1,53 @@
|
|||||||
package com.subman
|
import com.fasterxml.jackson.annotation.JsonSubTypes
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeInfo
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature
|
||||||
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
|
||||||
|
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
||||||
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
|
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
|
||||||
|
|
||||||
import Clash
|
// 基类,使用注解指定多态类型信息
|
||||||
import com.charleskorn.kaml.PolymorphismStyle
|
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
|
||||||
import com.charleskorn.kaml.Yaml
|
@JsonSubTypes(
|
||||||
import com.charleskorn.kaml.YamlConfiguration
|
JsonSubTypes.Type(value = Dog::class, name = "dog"),
|
||||||
import com.charleskorn.kaml.YamlNamingStrategy
|
JsonSubTypes.Type(value = Cat::class, name = "cat")
|
||||||
import io.ktor.client.*
|
)
|
||||||
import io.ktor.client.call.*
|
abstract class Animal(
|
||||||
import io.ktor.client.engine.cio.*
|
open val name: String
|
||||||
import io.ktor.client.request.*
|
|
||||||
import io.ktor.client.statement.*
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import kotlinx.serialization.json.JsonNamingStrategy
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class Temp(
|
|
||||||
val bCd: List<Int>,
|
|
||||||
val abCd: String? = null,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//fun main() {
|
// 子类 Dog
|
||||||
// val ymlStr = File("/Users/rainbus/Projects/Join/Java/subconverter4j/src/main/resources/temp.json").readText();
|
data class Dog(
|
||||||
// val json = Json {
|
override val name: String,
|
||||||
// namingStrategy = JsonNamingStrategy.KebabCase
|
val barkVolume: Int,
|
||||||
// }
|
val type: String
|
||||||
// println(json.decodeFromString(Temp.serializer(), ymlStr))
|
) : Animal(name)
|
||||||
//}
|
|
||||||
|
// 子类 Cat
|
||||||
|
data class Cat(
|
||||||
|
override val name: String,
|
||||||
|
val purrFrequency: Int,
|
||||||
|
val type: String
|
||||||
|
) : Animal(name)
|
||||||
|
|
||||||
|
val yamlMapper: ObjectMapper = ObjectMapper(YAMLFactory()).registerKotlinModule()
|
||||||
|
val objectMapper: ObjectMapper = ObjectMapper().registerKotlinModule()
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val client = HttpClient(CIO);
|
val yaml = """
|
||||||
runBlocking {
|
- type: dog
|
||||||
// val resp: HttpResponse =
|
name: Buddy
|
||||||
// client.get("https://UWUyUZlo.doggygo.top:8443/api/v1/client/657ae66e99810c9ab390c63c05cc2fac?flag=clashmeta");
|
barkVolume: 80
|
||||||
// val strBody: String = resp.body();
|
- type: cat
|
||||||
// print(strBody)
|
name: Whiskers
|
||||||
val strBody =
|
purrFrequency: 25
|
||||||
File("E:\\Project\\Join\\Kotlin\\subconverter4j\\src\\main\\resources\\gougou-temp.yaml").readText();
|
""".trimIndent()
|
||||||
val yaml = Yaml(
|
val animals = yamlMapper.readValue<List<Animal>>(yaml)
|
||||||
configuration = YamlConfiguration(
|
// val animals = listOf(
|
||||||
encodeDefaults = false,
|
// Dog("cat", 80),
|
||||||
yamlNamingStrategy = YamlNamingStrategy.KebabCase,
|
// Cat("dog", 25),
|
||||||
polymorphismStyle = PolymorphismStyle.Property
|
// )
|
||||||
)
|
animals.forEach { println(it) }
|
||||||
)
|
println(yamlMapper.writeValueAsString(animals))
|
||||||
println()
|
}
|
||||||
val clash = yaml.decodeFromString(Clash.serializer(), strBody)
|
|
||||||
println(yaml.encodeToString(Clash.serializer(), clash))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,6 @@
|
|||||||
a: abc
|
tunnels:
|
||||||
b:
|
- tcp/udp,127.0.0.1:6553,8.8.8.8:53,proxy
|
||||||
- 1
|
- network: [tcp, udp]
|
||||||
- 2
|
address: 127.0.0.1:6553
|
||||||
|
target: 8.8.8.8:53
|
||||||
|
proxy: proxy
|
||||||
Reference in New Issue
Block a user