jackson json序列化 首字母大寫 第二個字母需小寫


有這樣一個類:

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

    private String bName;

}

序列化后,希望首字母大寫,如下面的測試代碼:

@Test
    public void contextLoads() throws IOException {

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

        String s = objectMapper.writeValueAsString(test);

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

    }

可實際運行后,結果與希望不一樣:

org.junit.ComparisonFailure: 
Expected :{"BName":"234234"}
Actual   :{"Bname":"234234"}

jackson在序列化時把第二個大寫字母n轉成了小寫,這是為什么呢?

以下是跟蹤源碼的過程:

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

 

 執行完_addFields(props)方法后:

執行完_addMethods(props)方法后:

 

 一個是bName,一個是bname;

第一個bName取的是字段的名稱,

第二個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;
    }

根據stdNaming來決定這個name是以什么標准輸出,默認的是false;

stdManglePropertyName 就是原始輸出。
legacyManglePropertyName 就是規范輸出。

下面的代碼就是規范輸出:

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循環中,去除set后,第一個字母小寫,

第二字母小寫后,與第二個字母比較,如果都是小寫,則直接接上,返回,

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

意思就是為了滿足駝峰命名規則,要規范輸出。

 

如果我們的字段命名正如它的規范的話,props是只有一條記錄的,因為:名稱相同,就不插入了,由於咱們的名稱不同,所以就有兩條記錄。

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;
    }

可是我們輸出中只有一條,沒有bName這條,

 

 其實在是這里把第一條刪除了。因為:

 

 這些屬性為空,導致這個字段不可見:

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,再首字母大寫,所以就是Bname了。

解決方案:

第一個就是JsonProperty

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

    @JsonProperty("BName")
    private String bName;

}

測試結果如下:

org.junit.ComparisonFailure: 
Expected :{"BName":"234234"}
Actual   :{"Bname":"234234","BName":"234234"}

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

 

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

如下代碼:

@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);

    }

 

 第三個方案:重寫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;
            // 反序列化時調用
            @Override
            public String nameForSetterMethod(MapperConfig<?> config,
                                              AnnotatedMethod method, String defaultName) {
                return method.getName().substring(3);
            }
            // 序列化時調用
            @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的配置,要注意對其他功能的影響。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM