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] WriteNullStringAsEmpty 特性,在某些情况下会失效 #2642

Closed
noear opened this issue May 29, 2024 · 5 comments
Closed

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

noear opened this issue May 29, 2024 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@noear
Copy link
Contributor

noear commented May 29, 2024

以下为复现代码:

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 字段时,才触发这个问题
    }
}
@noear noear added the bug Something isn't working label May 29, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 29, 2024
wenshao added a commit that referenced this issue May 29, 2024
@wenshao
Copy link
Member

wenshao commented May 29, 2024

我用2.0.51-SNAPSHOT没有重现问题。你的功能也可以用JSONWriter.Feature.WriteLongAsString来实现啊

@noear
Copy link
Contributor Author

noear commented May 30, 2024

好晕啊。。。我重新拉了包,真的没错了。

@noear noear closed this as completed May 30, 2024
@noear
Copy link
Contributor Author

noear commented May 30, 2024

这个 JSONWriter.Feature.WriteLongAsString 特性好,以前是不是没有的?

@wenshao wenshao added the fixed label May 30, 2024
@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

@noear
Copy link
Contributor Author

noear commented Jun 1, 2024

ok

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