Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

【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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant