Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [SupportSmartMatch] 使用JSON.to或者JSONObject.getObject JSONObject.getObject时;全小写的字段名不生效 #3182

Open
BohrX opened this issue Nov 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@BohrX
Copy link

BohrX commented Nov 27, 2024

问题描述

使用JSON.to或者JSONObject.getObject JSONObject.getObject时;全小写的字段名不生效
涉及api:
com.alibaba.fastjson2.JSONObject#getObject(java.lang.String, java.lang.Class, com.alibaba.fastjson2.JSONReader.Feature...)
com.alibaba.fastjson2.JSON#to

环境信息

  • OS信息: Win11
  • JDK信息: 1.8
  • 版本信息:Fastjson2 2.0.53

重现步骤

import java.util.Map;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;

import lombok.Data;

public class ObjToBeanFeatureTest {

    @Before
    public void setUp() throws Exception {
        JSON.config(JSONReader.Feature.SupportSmartMatch, false);
    }


    @Test
    public void allLowerCaseFieldObjToBean() {
        // 全小写 有问题
        String text = "{\"appid\":\"com.xxx.xxx\"}";

        // 全局设置智能匹配, 反序列化没问题
        JSON.config(JSONReader.Feature.SupportSmartMatch, true);
        Bean bean2 = JSON.parseObject(text, Bean.class);
        Assertions.assertThat(bean2.appID).isEqualTo("com.xxx.xxx");

        Map<String, Object> jsonObject = JSON.parseObject(text);
        Assertions.assertThat(jsonObject.get("appid")).isEqualTo("com.xxx.xxx");
        JSONObject obj = new JSONObject();
        obj.put("v1", jsonObject);
        
        // 传 Feature,两个API均无法正确反序列化
        Bean bean3 = obj.getObject("v1", Bean.class, JSONReader.Feature.SupportSmartMatch);
        Assertions.assertThat(bean3.appID).isEqualTo(null);
        Bean bean4 = JSON.to(Bean.class, jsonObject);
        Assertions.assertThat(bean4.appID).isEqualTo(null);
    }

    @Data
    private static class Bean {
        private String appID;
    }
    
}

期待的正确结果

期望与反序列化API的处理方式保持一致

@BohrX BohrX added the bug Something isn't working label Nov 27, 2024
@BohrX BohrX changed the title [BUG] [SupportSmartMatch][ 使用JSON.to或者JSONObject.getObject JSONObject.getObject时;全小写的字段名不生效 [BUG] [SupportSmartMatch] 使用JSON.to或者JSONObject.getObject JSONObject.getObject时;全小写的字段名不生效 Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant