如何使用Jackson的嵌套有效负载序列化Flux样式的动作?

我正在尝试使用jackson为“助焊剂标准操作”设置自定义序列化。

示例JSON:

{
  type: 'ADD_TODO',payload: {
    text: 'Do something.'  
  }
}

我尝试通过声明@JsonSubTypes的接口:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.PROPERTY,property = "type")
@JsonSubTypes(
    JsonSubTypes.Type(value = AddTodoaction::class)
)
interface action

@JsonTypeName("ADD_TODO")
data class AddTodoaction(
    val text: String
) : action

并编写自定义序列化器:

class actionSerializer<T : Any>(clazz: KClass<T>) : StdSerializer<T>(clazz.java) {
    override fun serialize(value: T,gen: JsonGenerator?,provider: SerializerProvider?) {
        // ??
    }

    override fun serializeWithType(
        value: T?,serializers: SerializerProvider?,typeSer: TypeSerializer?
    ) {
        check(gen != null)
        check(serializers != null)

        if (value == null) {
            serializers.defaultSerializeNull(gen)
            return
        }

        val typeId = typeSer!!.typeId(value,JsonToken.START_OBJECT)

        typeSer.writeTypePrefix(gen,typeId)
        gen.writeFieldName("payload")
        serialize(value,gen,serializers)
        typeSer.writeTypeSuffix(gen,typeId)
    }
}

这里的问题是,我不知道如何编写serialize函数而不会引起无限递归。我什至不确定这是最好的方法。有什么建议么?我不想写些骇人听闻的东西,也不想为每个payload都有一个单独的类。

shong29 回答:如何使用Jackson的嵌套有效负载序列化Flux样式的动作?

为开箱即用的基本接口

尝试<rule name="Rewrite" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{URL}" negate="true" pattern="isABC" /> </conditions> <action type="Rewrite" url="localhost55717/product/xyz.aspx" /> </rule> <rule name="Rewrite" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{URL}" pattern="isABC" /> </conditions> <action type="Redirect" url="localhost55717/product/xyz.aspx" redirectType="Permanent" /> </rule> JsonDeserializer 而且JsonSerializer非常易于实现,而且记录也很丰富

gson
本文链接:https://www.f2er.com/3114543.html

大家都在问