@@ -21,18 +21,6 @@ import java.sql.Connection
2121import java.util.concurrent.ThreadLocalRandom
2222import kotlin.random.Random
2323import kotlinx.serialization.Contextual
24- import kotlinx.serialization.modules.SerializersModule
25- import kotlinx.serialization.modules.contextual
26- import kotlinx.serialization.modules.polymorphic
27- import kotlinx.serialization.modules.subclass
28- import kotlinx.serialization.KSerializer
29- import kotlinx.serialization.descriptors.SerialDescriptor
30- import kotlinx.serialization.descriptors.buildClassSerialDescriptor
31- import kotlinx.serialization.descriptors.element
32- import kotlinx.serialization.encoding.Decoder
33- import kotlinx.serialization.encoding.Encoder
34- import kotlinx.serialization.encoding.encodeStructure
35- import kotlinx.serialization.encoding.decodeStructure
3624
3725@Serializable
3826data class Message (val message : String )
@@ -49,59 +37,6 @@ data class DynamicValue(
4937 val value : Any
5038)
5139
52- class AnySerializer : KSerializer <Any > {
53- override val descriptor: SerialDescriptor = buildClassSerialDescriptor(" Any" ) {
54- element<String >(" type" )
55- element<String >(" value" )
56- }
57-
58- override fun serialize (encoder : Encoder , value : Any ) {
59- encoder.encodeStructure(descriptor) {
60- when (value) {
61- is String -> {
62- encodeStringElement(descriptor, 0 , " string" )
63- encodeStringElement(descriptor, 1 , value)
64- }
65- is Number -> {
66- encodeStringElement(descriptor, 0 , " number" )
67- encodeStringElement(descriptor, 1 , value.toString())
68- }
69- is Boolean -> {
70- encodeStringElement(descriptor, 0 , " boolean" )
71- encodeStringElement(descriptor, 1 , value.toString())
72- }
73- is List <* > -> {
74- encodeStringElement(descriptor, 0 , " list" )
75- encodeStringElement(descriptor, 1 , json.encodeToString(value))
76- }
77- is Map <* , * > -> {
78- encodeStringElement(descriptor, 0 , " map" )
79- encodeStringElement(descriptor, 1 , json.encodeToString(value))
80- }
81- else -> {
82- encodeStringElement(descriptor, 0 , " string" )
83- encodeStringElement(descriptor, 1 , value.toString())
84- }
85- }
86- }
87- }
88-
89- override fun deserialize (decoder : Decoder ): Any {
90- return decoder.decodeStructure(descriptor) {
91- val type = decodeStringElement(descriptor, 0 )
92- val value = decodeStringElement(descriptor, 1 )
93- when (type) {
94- " string" -> value
95- " number" -> value.toDoubleOrNull() ? : value
96- " boolean" -> value.toBoolean()
97- " list" -> json.decodeFromString<List <Any >>(value)
98- " map" -> json.decodeFromString<Map <String , Any >>(value)
99- else -> value
100- }
101- }
102- }
103- }
104-
10540// Optimized JSON instance with better performance settings
10641private val json = Json {
10742 prettyPrint = false
@@ -110,17 +45,6 @@ private val json = Json {
11045 coerceInputValues = true
11146}
11247
113- // Create a custom JSON instance with the AnySerializer
114- private val jsonWithAny = Json {
115- serializersModule = SerializersModule {
116- contextual(AnySerializer ())
117- }
118- prettyPrint = false
119- isLenient = true
120- ignoreUnknownKeys = true
121- coerceInputValues = true
122- }
123-
12448fun Application.main () {
12549 val dbRows = 10000
12650 val poolSize = Runtime .getRuntime().availableProcessors() * 2
@@ -246,7 +170,7 @@ fun Application.main() {
246170
247171 post(" /dynamic-map" ) {
248172 val dynamicMap = call.receive<Map <String , DynamicValue >>()
249- call.respondText(jsonWithAny .encodeToString(dynamicMap), ContentType .Application .Json )
173+ call.respondText(json .encodeToString(dynamicMap), ContentType .Application .Json )
250174 }
251175 }
252176}
0 commit comments