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

JSON.toJSONString(obj, SerializerFeature.PrettyFormat)导致循环引用path异常 #3672

Closed
txgj opened this issue Mar 5, 2021 · 2 comments
Milestone

Comments

@txgj
Copy link

txgj commented Mar 5, 2021

    IssueCR root = new IssueCR();
    IssueCA a = new IssueCA();
    IssueCB b= new IssueCB();
    IssueCC c = new IssueCC();
    IssueCD d = new IssueCD();

    root.setA(a);
    a.setB(Lists.newArrayList(b).toArray());
    b.setC(c);
    c.setD(d);
    d.setE(Lists.newArrayList(c));


    System.out.println(JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
    System.out.println(JSON.toJSONString(root));

第一个输出: { "a":{ "b":[ { "c":{ "d":{ "e":[ {"$ref":"$.a.b.null.c"} ] } } } ] } }

第二个输出: {"a":{"b":[{"c":{"d":{"e":[{"$ref":"$.a.b[0].c"}]}}}]}}

因为使用SerializerFeature.PrettyFormat时,在处理数组的序列化时,走了不同的逻辑,导致出现{"$ref":"$.a.b.null.c"}

@Certseeds
Copy link
Contributor

Certseeds commented Mar 9, 2021

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import lombok.Data;
import org.junit.Test;

import java.util.ArrayList;

public class Issue3672 {
    @Test
    public void test1() {
        Issue3672Root root = new Issue3672Root();
        Issue3672A a = new Issue3672A();
        Issue3672B b = new Issue3672B();
        Issue3672C c = new Issue3672C();
        Issue3672D d = new Issue3672D();
        root.setA(a);
        a.setB(Lists.newArrayList(b).toArray());
        b.setC(c);
        c.setD(d);
        d.setE(Lists.newArrayList(c));
        System.out.println(JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
        System.out.println(JSON.toJSONString(root));
    }

    @Data
    private class Issue3672Root {
        private Issue3672A a;
    }

    @Data
    private class Issue3672A {
        private Object[] b;
    }

    @Data
    private class Issue3672B {
        private Issue3672C c;
    }

    @Data
    private class Issue3672C {
        private Issue3672D d;
    }

    @Data
    private class Issue3672D {
        private ArrayList<Issue3672C> e;
    }

}

运行输出如下

{
	"a":{
		"b":[
			{
				"c":{
					"d":{
						"e":[
							{"$ref":"$.a.b.null.c"}
						]
					}
				}
			}
		]
	}
}
{"a":{"b":[{"c":{"d":{"e":[{"$ref":"$.a.b[0].c"}]}}}]}}

复现如上

@Certseeds
Copy link
Contributor

Certseeds commented Mar 9, 2021

PS:

a.setB(Lists.newArrayList(b).toArray());
...
private Object[] b;

to

a.setB(Lists.newArrayList(b).toArray(new Issue3672B[0]));
...
private Issue3672B[] b;

输出就不会有null了

Certseeds added a commit to Certseeds/fastjson that referenced this issue Mar 9, 2021
… transfer

array id to write(), so it will appear null.

<type>:
- [x] Bug fix
- [x] This change requires a documentation update

Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
@wenshao wenshao added this to the 1.2.76 milestone Apr 5, 2021
@wenshao wenshao closed this as completed in bab56ae Apr 5, 2021
@Certseeds Certseeds mentioned this issue Apr 11, 2021
Certseeds added a commit to Certseeds/fastjson that referenced this issue Jul 10, 2021
</subject>

Branch: issue3672-2nd

<type>:
- [ ] Bug fix
- [ ] Bug fix (Test)
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
- [ ] This change requires a documentation update

<body>

<footer>

Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
Certseeds added a commit to Certseeds/fastjson that referenced this issue Jun 26, 2022
</subject>

Branch: issue3672-2nd

<type>:
- [ ] Bug fix
- [ ] Bug fix (Test)
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
- [ ] This change requires a documentation update

<body>

<footer>

Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants