This commit is contained in:
2025-03-11 15:09:26 +08:00
parent 184b4d495d
commit cbff8ad6fc
4 changed files with 302 additions and 388 deletions

View File

@@ -1,6 +1,5 @@
plugins {
kotlin("jvm") version "2.1.10"
kotlin("plugin.serialization") version "2.1.10"
}
group = "com.subman"
@@ -13,8 +12,9 @@ repositories {
dependencies {
testImplementation(kotlin("test"))
implementation("com.charleskorn.kaml:kaml:0.72.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
implementation("com.fasterxml.jackson.core:jackson-databind:2.18.3")
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-cio:3.1.1")
}

View File

@@ -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
import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import com.charleskorn.kaml.YamlNamingStrategy
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
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,
// 基类,使用注解指定多态类型信息
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes(
JsonSubTypes.Type(value = Dog::class, name = "dog"),
JsonSubTypes.Type(value = Cat::class, name = "cat")
)
abstract class Animal(
open val name: String
)
//fun main() {
// val ymlStr = File("/Users/rainbus/Projects/Join/Java/subconverter4j/src/main/resources/temp.json").readText();
// val json = Json {
// namingStrategy = JsonNamingStrategy.KebabCase
// }
// println(json.decodeFromString(Temp.serializer(), ymlStr))
//}
// 子类 Dog
data class Dog(
override val name: String,
val barkVolume: Int,
val type: String
) : 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() {
val client = HttpClient(CIO);
runBlocking {
// val resp: HttpResponse =
// client.get("https://UWUyUZlo.doggygo.top:8443/api/v1/client/657ae66e99810c9ab390c63c05cc2fac?flag=clashmeta");
// val strBody: String = resp.body();
// print(strBody)
val strBody =
File("E:\\Project\\Join\\Kotlin\\subconverter4j\\src\\main\\resources\\gougou-temp.yaml").readText();
val yaml = Yaml(
configuration = YamlConfiguration(
encodeDefaults = false,
yamlNamingStrategy = YamlNamingStrategy.KebabCase,
polymorphismStyle = PolymorphismStyle.Property
)
)
println()
val clash = yaml.decodeFromString(Clash.serializer(), strBody)
println(yaml.encodeToString(Clash.serializer(), clash))
}
val yaml = """
- type: dog
name: Buddy
barkVolume: 80
- type: cat
name: Whiskers
purrFrequency: 25
""".trimIndent()
val animals = yamlMapper.readValue<List<Animal>>(yaml)
// val animals = listOf(
// Dog("cat", 80),
// Cat("dog", 25),
// )
animals.forEach { println(it) }
println(yamlMapper.writeValueAsString(animals))
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,6 @@
a: abc
b:
- 1
- 2
tunnels:
- tcp/udp,127.0.0.1:6553,8.8.8.8:53,proxy
- network: [tcp, udp]
address: 127.0.0.1:6553
target: 8.8.8.8:53
proxy: proxy