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】1.2.66 版本,添加属性 Feature.InitStringFieldAsEmpty 解析结果错误 #3050

Closed
moqimoqidea opened this issue Mar 4, 2020 · 0 comments · Fixed by #3142
Closed

Comments

@moqimoqidea
Copy link

解析结果(62 和 66 版本):

bean = Person{name='', address='', id='', age=10}

可完整复现的代码:

public class Person {

    public static void main(String[] args) {
        String line = "{\"name\":5, \"address\":\"beijing\", \"id\":\"100\", \"age\": 10}";

        Person bean = JSON.parseObject(line, Person.class, Feature.InitStringFieldAsEmpty);
        System.out.println("bean = " + bean);
    }

    private String name;
    private String address;
    private String id;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", id='" + id + '\'' +
                ", age=" + age +
                '}';
    }
}

因为 name 在 class 中设置为 String 字段,实际上传递过来为数字,且添加了 Feature.InitStringFieldAsEmpty 属性,导致所有 String 字段解析为空字符串。相关代码在 1.2.7 下正常工作。

1.2.7 结果:

bean = Person{name='5', address='beijing', id='100', age=10}

由于 JSON 类解析做兼容的缘故,设定所有字段为字符串类型,且引发了此 JSON 解析行为。

@moqimoqidea moqimoqidea changed the title 【bug】1.62+ 版本,添加属性 Feature.InitStringFieldAsEmpty 解析结果错误 【bug】1.66 版本,添加属性 Feature.InitStringFieldAsEmpty 解析结果错误 Mar 5, 2020
@moqimoqidea moqimoqidea changed the title 【bug】1.66 版本,添加属性 Feature.InitStringFieldAsEmpty 解析结果错误 【bug】1.2.66 版本,添加属性 Feature.InitStringFieldAsEmpty 解析结果错误 Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant