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]CamelCase has some problems #546

Closed
SuperEdison opened this issue Jul 12, 2022 · 7 comments
Closed

[BUG]CamelCase has some problems #546

SuperEdison opened this issue Jul 12, 2022 · 7 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@SuperEdison
Copy link

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

  • OS信息: Mac M1
  • JDK信息: zulu8
  • 版本信息:[e.g.:Fastjson2 2.x.x]

重现步骤

如何操作可以重现该问题:

  1. 使用 BeanUtils.getterName 方法
  2. 输入 UId 数据
  3. 出现 UId 错误
    public static void main(String[] args) {
        A vo = new A();
        vo.setCId("2");
        System.out.println(JSON.toJSONString(vo));
        //{"CId":"2"}
    }

    @Data
    static public  class A {
        private String uId;
        private String cId;
    }

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
//{"cId":"2"}

相关日志输出

请复制并粘贴任何相关的日志输出。

{"CId":"2"}

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。
image

@SuperEdison SuperEdison added the bug Something isn't working label Jul 12, 2022
@SuperEdison
Copy link
Author

and than i found the error code that is here

image

CamelCase's definition is the first word is lowercase. but here why do you add char[1] && chars[1] <= 'Z' judge

@SuperEdison SuperEdison mentioned this issue Jul 12, 2022
3 tasks
@SuperEdison
Copy link
Author

@SuperEdison
Copy link
Author

fastjson v1 is use reflect to solve fields name, but fastjson2 is use gettername to solve fields name. so i think need to use CamelCase definition to make obvious distinction.

@wenshao wenshao added this to the 2.0.10 milestone Jul 12, 2022
@SuperEdison SuperEdison mentioned this issue Jul 12, 2022
3 tasks
wenshao added a commit that referenced this issue Jul 15, 2022
wenshao added a commit that referenced this issue Jul 15, 2022
@wenshao
Copy link
Member

wenshao commented Jul 15, 2022

   @Test
    public void test1() {
        B vo = new B();
        vo.cId = "2";
        assertEquals("{\"cId\":\"2\"}", JSON.toJSONString(vo));
    }

    public static class B {
        public String uId;
        public String cId;
    }

你可以通过把field声明为public就可以解决这个问题

@wenshao wenshao modified the milestones: 2.0.10, 2.0.11 Jul 23, 2022
@wenshao wenshao modified the milestones: 2.0.11, 2.0.12 Aug 7, 2022
@wenshao wenshao modified the milestones: 2.0.12, 2.0.13 Aug 20, 2022
@SuperEdison
Copy link
Author

    public static void main(String[] args) {
        A a = new A();
        a.setUId("a");
        System.out.println(JSON.toJSONString(a));
//        SpringApplication.run(DemoApplication.class, args);
    }

    @Data
    static public class A {
//        @JSONField(name = "uid")
        public String uId;
    }

如果加上get set方法
{"UId":"a","uId":"a"}
如果加上@JSONField(name = "uid")
{"UId":"a","uid":"a"}
我一直没搞懂到底是以什么标准去序列号=。=感觉越来越复杂了。之前说的按javabean去定义序列化的key,那还能理解,那现在如果属性变成public那就又变味了

@wenshao
Copy link
Member

wenshao commented Sep 10, 2022

  @Test
    public void test2() {
        assertEquals("abc", JSON.parseObject("{\"uId\":\"abc\"}", C.class).uId);
        assertEquals("abc", JSON.parseObject("{\"UId\":\"abc\"}", C.class).uId);
    }

    public static class C {
        private String uId;

        public String getUId() {
            return uId;
        }

        public void setUId(String uId) {
            this.uId = uId;
        }
    }

最新快照版本反序列化已经能支持自动识别多种输入

@wenshao wenshao added the fixed label Sep 10, 2022
wenshao added a commit that referenced this issue Sep 10, 2022
@wenshao
Copy link
Member

wenshao commented Sep 10, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.13
2.0.13版本已经发布,请用帮忙用新版本验证

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants