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

[QUESTION] 有个 writerProvider.register 相关的代码没生效,疑惑! #1893

Closed
noear opened this issue Sep 23, 2023 · 6 comments
Closed
Labels
fixed question Further information is requested
Milestone

Comments

@noear
Copy link
Contributor

noear commented Sep 23, 2023

像下面这个代码,为什么没有打印出:{"a":0,"b":0,"c":"1"}

public class XxxxTest {
    public static void main(String[] args){
        ObjectWriterProvider writerProvider = new ObjectWriterProvider();
        writerProvider.register(Long.class, (out, obj, fieldName, fieldType, features) -> {
            out.writeString(String.valueOf(obj));
        });

        JSONWriter.Context writeContext = new JSONWriter.Context(writerProvider,
                JSONWriter.Feature.WriteNullNumberAsZero);

        Demo demo = new Demo();
        String tmp = JSON.toJSONString(demo, writeContext);
        System.out.println(tmp); //{"a":0,"b":0,"c":1}
    }

    public static class Demo{
        public Long a;
        public Long b;
        public Long c = 1L;
    }
}
@noear noear added the question Further information is requested label Sep 23, 2023
@noear noear changed the title [QUESTION] 有个代码没生效,疑惑! [QUESTION] 有个 writerProvider.register 相关的代码没生效,疑惑! Sep 23, 2023
@wenshao
Copy link
Member

wenshao commented Sep 26, 2023

现在Long类型不支持自定义序列化

@noear
Copy link
Contributor Author

noear commented Sep 27, 2023

@wenshao 说出来你可能不信。我自己也晕。。。我在 solon 里的 fastjson2 适配,其特性 longAsString ,就是通过它实现的。。。但是我想写个小 demo 复现一个问题,却完全没效果。(就是关于那个 WriteNullNumberAsZero 后,没有变成 string 的问题)。。。这事儿,晕了我很久很久

@wenshao
Copy link
Member

wenshao commented Oct 28, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.42-SNAPSHOT/
@noear 帮验证下 2.0.42-SNAPSHOT版本WriteNullNumberAsZero和WriteLongAsString是否在你的场景起作用了

 @Test
    public void test() throws Exception {
        assertEquals(
                "{\"value\":\"0\"}",
                JSON.toJSONString(new Bean(),
                        JSONWriter.Feature.WriteNullNumberAsZero,
                        JSONWriter.Feature.WriteLongAsString)
        );
    }

    public static class Bean {
        public Long value;
    }

@noear
Copy link
Contributor Author

noear commented Oct 28, 2023

@wenshao 好,明天我试下

wenshao added a commit that referenced this issue Oct 29, 2023
@noear
Copy link
Contributor Author

noear commented Oct 29, 2023

@wenshao 测试了。有效!

@wenshao wenshao added this to the 2.0.42 milestone Oct 29, 2023
@wenshao wenshao added the fixed label Oct 29, 2023
@wenshao
Copy link
Member

wenshao commented Nov 5, 2023

https://github.com/alibaba/fastjson2/releases/tag/2.0.42
问题已修复,请用新版本

@wenshao wenshao closed this as completed Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants