溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法

發(fā)布時(shí)間:2021-06-29 14:36:56 來(lái)源:億速云 閱讀:826 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

有這樣一個(gè)類(lèi):

@Setter
@Getter
@JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Student {

    private String bName;

}

序列化后,希望首字母大寫(xiě),如下面的測(cè)試代碼:

@Test
    public void contextLoads() throws IOException {

        Student test = new Student();
        test.setBName("234234");

        String s = objectMapper.writeValueAsString(test);

        Assert.assertEquals("{\"BName\":\"234234\"}", s);

    }

可實(shí)際運(yùn)行后,結(jié)果與希望不一樣:

org.junit.ComparisonFailure:

Expected :{"BName":"234234"}

Actual   :{"Bname":"234234"}

jackson在序列化時(shí)把第二個(gè)大寫(xiě)字母n轉(zhuǎn)成了小寫(xiě),這是為什么呢?

以下是跟蹤源碼的過(guò)程:

直接找到:com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector#collectAll這個(gè)方法:

jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法

執(zhí)行完_addFields(props)方法后:

jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法

執(zhí)行完_addMethods(props)方法后:

jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法

一個(gè)是bName,一個(gè)是bname;

第一個(gè)bName取的是字段的名稱(chēng),

第二個(gè)bname是取的它的set方法:

public static String okNameForIsGetter(AnnotatedMethod am, String name,
            boolean stdNaming)
    {
        if (name.startsWith("is")) { // plus, must return a boolean
            Class<?> rt = am.getRawType();
            if (rt == Boolean.class || rt == Boolean.TYPE) {
                return stdNaming
                        ? stdManglePropertyName(name, 2)
                        : legacyManglePropertyName(name, 2);
            }
        }
        return null;
    }

根據(jù)stdNaming來(lái)決定這個(gè)name是以什么標(biāo)準(zhǔn)輸出,默認(rèn)的是false;

stdManglePropertyName 就是原始輸出。

legacyManglePropertyName 就是規(guī)范輸出。

下面的代碼就是規(guī)范輸出:

protected static String legacyManglePropertyName(final String basename, final int offset)
    {
        final int end = basename.length();
        if (end == offset) { // empty name, nope
            return null;
        }
        // next check: is the first character upper case? If not, return as is
        char c = basename.charAt(offset);
        char d = Character.toLowerCase(c);
        
        if (c == d) {
            return basename.substring(offset);
        }
        // otherwise, lower case initial chars. Common case first, just one char
        StringBuilder sb = new StringBuilder(end - offset);
        sb.append(d);
        int i = offset+1;
        for (; i < end; ++i) {
            c = basename.charAt(i);
            d = Character.toLowerCase(c);
            if (c == d) {
                sb.append(basename, i, end);
                break;
            }
            sb.append(d);
        }
        return sb.toString();
    }

主要邏輯在for循環(huán)中,去除set后,第一個(gè)字母小寫(xiě),

第二字母小寫(xiě)后,與第二個(gè)字母比較,如果都是小寫(xiě),則直接接上,返回,

如果第二字母大寫(xiě),就如我們的這種情況,就以小寫(xiě)的情況,接上,再去找下一個(gè)字母,直到找到小寫(xiě)字母為止。

意思就是為了滿(mǎn)足駝峰命名規(guī)則,要規(guī)范輸出。

如果我們的字段命名正如它的規(guī)范的話,props是只有一條記錄的,因?yàn)椋好Q(chēng)相同,就不插入了,由于咱們的名稱(chēng)不同,所以就有兩條記錄。

protected POJOPropertyBuilder _property(Map<String, POJOPropertyBuilder> props,
            String implName)
    {
        POJOPropertyBuilder prop = props.get(implName);
        if (prop == null) {
            prop = new POJOPropertyBuilder(_config, _annotationIntrospector, _forSerialization,
                    PropertyName.construct(implName));
            props.put(implName, prop);
        }
        return prop;
    }

可是我們輸出中只有一條,沒(méi)有bName這條,

jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法

其實(shí)在是這里把第一條刪除了。因?yàn)椋?/p>

jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法

這些屬性為空,導(dǎo)致這個(gè)字段不可見(jiàn):

protected void _removeUnwantedProperties(Map<String, POJOPropertyBuilder> props)
    {
        Iterator<POJOPropertyBuilder> it = props.values().iterator();
        while (it.hasNext()) {
            POJOPropertyBuilder prop = it.next();

            // First: if nothing visible, just remove altogether
            if (!prop.anyVisible()) {
                it.remove();
                continue;
            }
            // Otherwise, check ignorals
            if (prop.anyIgnorals()) {
                // first: if one or more ignorals, and no explicit markers, remove the whole thing
                if (!prop.isExplicitlyIncluded()) {
                    it.remove();
                    _collectIgnorals(prop.getName());
                    continue;
                }
                // otherwise just remove ones marked to be ignored
                prop.removeIgnored();
                if (!prop.couldDeserialize()) {
                    _collectIgnorals(prop.getName());
                }
            }
        }
    }

只剩第二記錄bname,再首字母大寫(xiě),所以就是Bname了。

解決方案:

第一個(gè)就是JsonProperty

@Setter
@Getter
@JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Student {

    @JsonProperty("BName")
    private String bName;

}

測(cè)試結(jié)果如下:

org.junit.ComparisonFailure:

Expected :{"BName":"234234"}

Actual   :{"Bname":"234234","BName":"234234"}

雖然生成了BName,但是Bname仍在(加了JsonProperty就visable了)。

第二個(gè)就是配置objectMapper的MapperFeature.USE_STD_BEAN_NAMIN如上文提到了,非規(guī)范化輸出。

如下代碼:

@Test
    public void contextLoads() throws IOException {

        Student test = new Student();
        test.setBName("234234");
        objectMapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true);
        String s = objectMapper.writeValueAsString(test);

        Assert.assertEquals("{\"BName\":\"234234\"}", s);

    }

第三個(gè)方案:重寫(xiě)PropertyNamingStrategy:

@Test
    public void contextLoads() throws IOException {

        Student test = new Student();
        test.setBName("234234");
        //objectMapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true);

        objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy() {
            private static final long serialVersionUID = 1L;
            // 反序列化時(shí)調(diào)用
            @Override
            public String nameForSetterMethod(MapperConfig<?> config,
                                              AnnotatedMethod method, String defaultName) {
                return method.getName().substring(3);
            }
            // 序列化時(shí)調(diào)用
            @Override
            public String nameForGetterMethod(MapperConfig<?> config,
                                              AnnotatedMethod method, String defaultName) {
                return method.getName().substring(3);
            }
        });


        String s = objectMapper.writeValueAsString(test);

        Assert.assertEquals("{\"BName\":\"2342344\"}", s);

    }

修改objectMapper的配置,要注意對(duì)其他功能的影響。

“jackson json序列化實(shí)現(xiàn)首字母大寫(xiě),第二個(gè)字母小寫(xiě)的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI