第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

杰克遜:向地圖序列化添加動(dòng)態(tài)字段

杰克遜:向地圖序列化添加動(dòng)態(tài)字段

慕容3067478 2022-09-22 16:34:00
類(lèi)似于簡(jiǎn)化版本的@JsonAppendpublic class Bean {    @JsonAppend(key = [...], value = [...])    public Map<?, ?> map = new HashMap<>();}那就太好了 - 任何簡(jiǎn)單的方法來(lái)實(shí)現(xiàn)這一目標(biāo)?但發(fā)現(xiàn)沒(méi)有任何東西符合我的需求。我提出請(qǐng)求的原因是,無(wú)法區(qū)分某些給定的JSON是否源自Map或POJO序列化。如果這是必要的(在極少數(shù)情況下),在地圖上添加一個(gè)神奇的額外字段將是實(shí)現(xiàn)這一目標(biāo)的簡(jiǎn)單方法。
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

好問(wèn)題!是的,這是(以某種方式)可能的。以下公開(kāi)的方法維護(hù)標(biāo)準(zhǔn)序列化行為,同時(shí)在其上添加注釋定義的鍵值對(duì)。


創(chuàng)建自定義批注。我來(lái)稱(chēng)呼它MapAppender


@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface MapAppender {

    String[] keys();

    String[] values();

}

如您所見(jiàn),我們定義了鍵值數(shù)組,這些數(shù)組將按索引匹配。

我們被迫使用字段而不是更通用的 ,但這是每個(gè)注釋設(shè)計(jì)。StringObject


創(chuàng)建自定義 .我來(lái)稱(chēng)呼它JsonSerializer<Map>MapAppenderSerializer


public class MapAppenderSerializer

        extends StdSerializer<Map>

        implements ContextualSerializer {

    private static final long serialVersionUID = 1L;


    private final String[] keys;

    private final String[] values;


    // No-arg constructor required for Jackson

    MapAppenderSerializer() {

        super(Map.class);

        keys = new String[0];

        values = new String[0];

    }


    MapAppenderSerializer(

            final String[] keys,

            final String[] values) {

        super(Map.class);

        this.keys = keys;

        this.values = values;

    }


    @Override

    public void serialize(

            final Map value,

            final JsonGenerator jsonGenerator,

            final SerializerProvider serializerProvider) throws IOException {

        // Create a copy Map to avoid touching the original one

        final Map hashMap = new HashMap<>(value);


        // Add the annotation-specified key-value pairs

        for (int i = 0; i < keys.length; i++) {

            hashMap.put(keys[i], values[i]);

        }


        // Serialize the new Map

        serializerProvider.defaultSerializeValue(hashMap, jsonGenerator);

    }


    @Override

    public JsonSerializer<?> createContextual(

            final SerializerProvider serializerProvider,

            final BeanProperty property) {

        MapAppender annotation = null;


        if (property != null) {

            annotation = property.getAnnotation(MapAppender.class);

        }


        if (annotation != null) {

            return new MapAppenderSerializer(annotation.keys(), annotation.values());

        }


        throw new UnsupportedOperationException("...");

    }

}

現(xiàn)在,使用您的類(lèi)示例,使用注釋字段并定義自定義序列化程序BeanMap@MapAppender@JsonSerialize


public class Bean {

    public String simpleField;


    @MapAppender(keys = {"test1", "test2"}, values = {"value1", "value2"})

    @JsonSerialize(using = MapAppenderSerializer.class)

    public Map<Object, Object> simpleMap = new HashMap<>();

}

就是這樣。序列化 的實(shí)例Bean


final ObjectMapper objectMapper = new ObjectMapper();

final String string = objectMapper.writeValueAsString(new Bean());

結(jié)果


{"simpleField":null,"simpleMap":{"test2":"value2","test1":"value1"}}

另一個(gè)示例,在序列化之前用值填充Map


final ObjectMapper objectMapper = new ObjectMapper();

final Bean value = new Bean();

value.simpleMap.put("myKey", "myValue");


final String string = objectMapper.writeValueAsString(value);

結(jié)果


{"simpleField":null,"simpleMap":{"test1":"value1","test2":"value2","myKey":"myValue"}}


查看完整回答
反對(duì) 回復(fù) 2022-09-22
  • 1 回答
  • 0 關(guān)注
  • 101 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)