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]JSONWriter.Feature.BrowserCompatible处理map的Long类型key值不正确 #1401

Closed
MartinDai opened this issue Apr 24, 2023 · 4 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@MartinDai
Copy link

问题描述

添加JSONWriter.Feature.BrowserCompatible后输出的map的Long类型key值没有添加双引号,fastjson 1.x是可以正确处理的

#1350 提过这个问题然后说是修复了,但是我验证了还是不行的

环境信息

请填写以下信息:

  • OS信息: MacOS 13.3.1
  • JDK信息:Openjdk 1.8.0_312
  • 版本信息:Fastjson2 2.0.28

重现步骤

public class Test {

    Map<Long, String> map = new HashMap<>();

    public Test(){
        map.put(0L, "Test");
    }

    public Map<Long, String> getMap() {
        return map;
    }

    public static void main(String[] args) {
        Test test = new Test();
        String invalid = new String(JSON.toJSONString(test).getBytes(Charset.defaultCharset()), Charset.defaultCharset());
        String valid = new String(JSON.toJSONString(test, JSONWriter.Feature.BrowserCompatible).getBytes(Charset.defaultCharset()), Charset.defaultCharset());
        System.out.println(invalid);
        System.out.println(valid);
    }
}

期待的正确结果

{"map":{0:"Test"}}
{"map":{"0":"Test"}}

相关日志输出

{"map":{0:"Test"}}
{"map":{0:"Test"}}

@MartinDai MartinDai added the bug Something isn't working label Apr 24, 2023
@kraity
Copy link
Collaborator

kraity commented Apr 27, 2023

您好, Feature.BrowserCompatible特性不是针对非字符串Key.
需要使用 JSONWriter.Feature.WriteNonStringKeyAsString (非字符串Key作为字符串) 才能符合预期

@MartinDai
Copy link
Author

您好, Feature.BrowserCompatible特性不是针对非字符串Key. 需要使用 JSONWriter.Feature.WriteNonStringKeyAsString (非字符串Key作为字符串) 才能符合预期

但是在fastjson 1.x使用SerializerFeature.BrowserCompatible是可以的 这块在2.x不兼容吗

kraity added a commit to kraity/fastjson2 that referenced this issue Apr 27, 2023
wenshao pushed a commit that referenced this issue Apr 27, 2023
* test: add testcase for #1401

Signed-off-by: Kraity <[email protected]>

* perf: optimize serializing int and long value

Signed-off-by: Kraity <[email protected]>

---------

Signed-off-by: Kraity <[email protected]>
@wenshao wenshao added this to the 2.0.30 milestone Apr 29, 2023
@wenshao wenshao added fixed and removed fixed labels May 5, 2023
@wenshao
Copy link
Member

wenshao commented May 5, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.30-SNAPSHOT/
问题已修复,请帮忙用2.0.30-SNAPSHOT验证,2.0.30版本预计在周末5月7日前发布

@wenshao wenshao added the fixed label May 5, 2023
wenshao added a commit that referenced this issue May 5, 2023
@wenshao
Copy link
Member

wenshao commented May 6, 2023

@wenshao wenshao closed this as completed May 6, 2023
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

3 participants