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] 2.0.5 版本,json 转换字段排序 Feature.OrderedField 未生效 #394

Closed
chimmhuang opened this issue Jun 2, 2022 · 7 comments
Labels
bug Something isn't working
Milestone

Comments

@chimmhuang
Copy link

chimmhuang commented Jun 2, 2022

问题描述

2.0.5 版本,json 转换字段排序 Feature.OrderedField 未生效

环境信息

请填写以下信息:

  • OS信息: [windows11]
  • JDK信息: [Openjdk11.0.10]
  • 版本信息:[Fastjson 2.0.5]

重现步骤

以下代码使用不同的 fastjson 版本即可复现问题,问题版本:2.0.5、升级前版本:1.2.60

        String json = """
                {
                    "head": {
                        "requestTime": "1654070371884",
                        "merchantcode": "111",
                        "ticket": "111",
                        "version": "1.0.0"
                    },
                    "body": {
                        "phone": "111",
                        "type": "1",
                        "ticket": ""
                    },
                    "sign": "111"
                }
                """;

        System.out.println(JSON.parseObject(json, Feature.OrderedField));

输出为

{
    "head": {
        "requestTime": "1654070371884",
        "merchantcode": "111",
        "ticket": "111",
        "version": "1.0.0"
    },
    "sign": "111",
    "body": {
        "phone": "111",
        "type": "1",
        "ticket": ""
    }
}

期望输出json的顺序不改变,但实际结果是顺序发生了改变。1.2.60 版本不存在此问题

@chimmhuang chimmhuang added the bug Something isn't working label Jun 2, 2022
@dance3366
Copy link

问题描述

2.0.5 版本,json 转换字段排序 Feature.OrderedField 未生效

环境信息

请填写以下信息:

  • OS信息: [windows11]
  • JDK信息: [Openjdk11.0.10]
  • 版本信息:[Fastjson 2.0.5]

重现步骤

以下代码使用不同的 fastjson 版本即可复现问题,问题版本:2.0.5、升级前版本:1.2.60

        String json = """
                {
                    "head": {
                        "requestTime": "1654070371884",
                        "merchantcode": "111",
                        "ticket": "111",
                        "version": "1.0.0"
                    },
                    "body": {
                        "phone": "111",
                        "type": "1",
                        "ticket": ""
                    },
                    "sign": "111"
                }
                """;

        System.out.println(JSON.parseObject(json, Feature.OrderedField));

输出为

{
    "head": {
        "requestTime": "1654070371884",
        "merchantcode": "111",
        "ticket": "111",
        "version": "1.0.0"
    },
    "sign": "111",
    "body": {
        "phone": "111",
        "type": "1",
        "ticket": ""
    }
}

期望输出json的顺序不改变,但实际结果是顺序发生了改变。1.2.60 版本不存在此问题

你可以不加Feature.OrderedField试试,2我测试的结果默认是有序的

@chimmhuang
Copy link
Author

现在的情况就是,加了 Feature.OrderedField 和 不加 Feature.OrderedField 是一个效果,都是被排序了,并不是我输入的顺序。在1.2.60 版本,不加 Feature.OrderedField 会被排序,加了 Feature.OrderedField 则是我输入的顺序。

@dance3366
Copy link

现在的情况就是,加了 Feature.OrderedField 和 不加 Feature.OrderedField 是一个效果,都是被排序了,并不是我输入的顺序。在1.2.60 版本,不加 Feature.OrderedField 会被排序,加了 Feature.OrderedField 则是我输入的顺序。

Feature.OrderedField 这个fastjson2已经没有了,你用的应该是兼容包,检查JSON是否引用的包名是否为2,如下
com.alibaba.fastjson2.JSON

@chimmhuang
Copy link
Author

现在的情况就是,加了 Feature.OrderedField 和 不加 Feature.OrderedField 是一个效果,都是被排序了,并不是我输入的顺序。在1.2.60 版本,不加 Feature.OrderedField 会被排序,加了 Feature.OrderedField 则是我输入的顺序。

Feature.OrderedField 这个fastjson2已经没有了,你用的应该是兼容包,检查JSON是否引用的包名是否为2,如下 com.alibaba.fastjson2.JSON

啊这。。。我用的是

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.5</version>
</dependency>

@dance3366
Copy link

现在的情况就是,加了 Feature.OrderedField 和 不加 Feature.OrderedField 是一个效果,都是被排序了,并不是我输入的顺序。在1.2.60 版本,不加 Feature.OrderedField 会被排序,加了 Feature.OrderedField 则是我输入的顺序。

Feature.OrderedField 这个fastjson2已经没有了,你用的应该是兼容包,检查JSON是否引用的包名是否为2,如下 com.alibaba.fastjson2.JSON

啊这。。。我用的是

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.5</version>
</dependency>

public static void main(String[] args) {
String str = "{\n" +
" "head": {\n" +
" "requestTime": "1654070371884",\n" +
" "merchantcode": "111",\n" +
" "ticket": "111",\n" +
" "version": "1.0.0"\n" +
" },\n" +
" "body": {\n" +
" "phone": "111",\n" +
" "type": "1",\n" +
" "ticket": ""\n" +
" },\n" +
" "sign": "111"\n" +
" }";
System.out.println(JSON.parseObject(str).toString(JSONWriter.Feature.PrettyFormat));
}

输出结果:
{
"head":{
"requestTime":"1654070371884",
"merchantcode":"111",
"ticket":"111",
"version":"1.0.0"
},
"body":{
"phone":"111",
"type":"1",
"ticket":""
},
"sign":"111"
}

@wenshao
Copy link
Member

wenshao commented Jun 2, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

问题已经修复,请帮忙用2.0.6-SNAPSHOT版本验证,2.0.6正式版预计6月5日前发布

@wenshao wenshao added this to the 2.0.6 milestone Jun 2, 2022
@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

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

@wenshao wenshao closed this as completed Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants