You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过父类字段值使用Jackson反序列化子类?

Absolutely! You can totally handle polymorphic deserialization of your NetworkMessage subclasses using just annotations—no TreeNode or fancy workarounds needed. Let me break this down for you using two popular serialization libraries that fit your use case perfectly:

Using Jackson (Kotlin/JVM)

Jackson has built-in annotation support for polymorphic types that aligns exactly with your setup, since you’re using a dedicated type field to identify each subclass.

Step 1: Annotate the Parent Class

First, add annotations to NetworkMessage to tell Jackson which field acts as the type discriminator, and map each NetworkMessageType value to its corresponding subclass:

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo

enum class NetworkMessageType {
    CONNECTION_ACCEPTED,
    // Add other message types here as needed
}

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    property = "type" // Points directly to your existing type field
)
@JsonSubTypes(
    JsonSubTypes.Type(value = ConnectionAcceptedResponseMessage::class, name = "CONNECTION_ACCEPTED"),
    // Add mappings for other subclasses here
)
open class NetworkMessage(var type: NetworkMessageType)

class ConnectionAcceptedResponseMessage(
    val accepted: Boolean,
    val uid: String
) : NetworkMessage(NetworkMessageType.CONNECTION_ACCEPTED)

Step 2: Deserialize normally

Once annotated, Jackson will automatically inspect the type field in your JSON and instantiate the correct subclass:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

val mapper = jacksonObjectMapper()
val json = """{"type":"CONNECTION_ACCEPTED","accepted":true,"uid":"user_123"}"""
val message: NetworkMessage = mapper.readValue(json)

// Verify the correct subclass is created
println(message is ConnectionAcceptedResponseMessage) // Prints true
Using Kotlinx Serialization (Kotlin Native)

If you prefer Kotlin’s native serialization library, it also supports polymorphic deserialization with annotations, no extra structures required.

Step 1: Annotate Classes

First, ensure your enum and classes are marked as serializable, then use @Polymorphic and @SerialName to map types:

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass

@Serializable
enum class NetworkMessageType {
    CONNECTION_ACCEPTED
}

@Serializable
@Polymorphic
open class NetworkMessage(var type: NetworkMessageType)

@Serializable
@SerialName("CONNECTION_ACCEPTED") // Matches the enum value string
class ConnectionAcceptedResponseMessage(
    val accepted: Boolean,
    val uid: String
) : NetworkMessage(NetworkMessageType.CONNECTION_ACCEPTED)

Step 2: Configure the Serializer

You just need to register the polymorphic subtypes with your Json instance:

val module = SerializersModule {
    polymorphic(NetworkMessage::class) {
        subclass(ConnectionAcceptedResponseMessage::class)
        // Add other subclasses here
    }
}

val json = Json { serializersModule = module }
val jsonString = """{"type":"CONNECTION_ACCEPTED","accepted":true,"uid":"user_123"}"""
val message: NetworkMessage = json.decodeFromString(jsonString)

// Check the subclass type
println(message is ConnectionAcceptedResponseMessage) // Prints true

Note: Kotlinx Serialization uses type as the default discriminator field, which matches your setup perfectly—no extra configuration needed here.

Key Takeaways
  • Both libraries let you handle polymorphic deserialization with only annotations (plus minimal setup for Kotlinx).
  • Your existing type field is the perfect discriminator—no need to add extra fields or use complex structures like TreeNode.
  • Just map each NetworkMessageType value to its subclass via annotations, and the library handles the rest of the deserialization logic.

内容的提问来源于stack exchange,提问作者Fairy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 17:47:51