Skip to content

[BUG] WriteNullStringAsEmpty 特性,在某些情况下会失效 #2642

@noear

Description

@noear

以下为复现代码:

public class TestQuickConfig {
    @Test
    public void test(){
        ObjectWriterProvider provider = new ObjectWriterProvider();
        provider.register(Long.class, (out, obj, fieldName, fieldType, features) -> {
            String val = obj.toString();
            out.writeString(val);
        });

        JSONWriter.Context context = new JSONWriter.Context(provider,
                JSONWriter.Feature.WriteNullStringAsEmpty,
                JSONWriter.Feature.WriteNulls);

        DemoDo demoDo = new DemoDo();
        String json = JSON.toJSONString(demoDo,context);

        System.out.println(json); //实际上 s1 输出为 null

        assert "{\"n0\":null,\"n1\":\"1\",\"s0\":\"\",\"s1\":\"noear\"}".equals(json);
    }

    @Getter
    @Setter
    public static class DemoDo implements Serializable {
        String s0;
        String s1 = "noear";

        Long n0;
        Long n1 = 1L; //当有 Long 字段时,才触发这个问题
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions