Skip to content
This repository has been archived by the owner on Jun 26, 2022. It is now read-only.

Commit

Permalink
add more-test-case and improve for alibaba#3672
Browse files Browse the repository at this point in the history
</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>
  • Loading branch information
Certseeds committed Jul 10, 2021
1 parent 6e956d2 commit aea8123
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
Expand Up @@ -94,14 +94,12 @@ public final void write(JSONSerializer serializer, Object object, Object fieldNa
} else {
Class<?> clazz = item.getClass();

if (clazz == preClazz) {
preWriter.write(serializer, item, i, null, 0);
} else {
if (clazz != preClazz) {
preClazz = clazz;
preWriter = serializer.getObjectWriter(clazz);

preWriter.write(serializer, item, i, null, 0);
}
preWriter.write(serializer, item, i, null, 0);
}
out.append(',');
}
Expand Down
64 changes: 55 additions & 9 deletions src/test/java/com/alibaba/json/bvt/issue_3600/Issue3672.java
@@ -1,18 +1,20 @@
package com.alibaba.json.bvt.issue_3600;

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

import java.util.ArrayList;

import junit.framework.TestCase;
public class Issue3672 {
private Issue3672Root root;
private Issue3672Root2 root2;

public class Issue3672 extends TestCase {
public void test_for_issue() throws Exception {

public Issue3672Root init_root() {
Issue3672Root root = new Issue3672Root();
Issue3672A a = new Issue3672A();
Issue3672B b = new Issue3672B();
Expand All @@ -23,24 +25,68 @@ public void test_for_issue() throws Exception {
b.setC(c);
c.setD(d);
d.setE(Lists.newArrayList(c));
String str1 = JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
String str2 = JSON.toJSONString(root);
return root;
}

public Issue3672Root2 init_root2() {
Issue3672Root2 root2 = new Issue3672Root2();
Issue3672A2 a = new Issue3672A2();
Issue3672B b = new Issue3672B();
Issue3672C c = new Issue3672C();
Issue3672D d = new Issue3672D();
root2.setA(a);
a.setB(Lists.newArrayList(b).toArray(new Issue3672B[0]));
b.setC(c);
c.setD(d);
d.setE(Lists.newArrayList(c));
return root2;
}

JSONObject obj1 = JSON.parseObject(str1);
JSONObject obj2 = JSON.parseObject(str2);
assertEquals(obj1.toString(), obj2.toString());
@Test
public void test_root_withObjectArray() {
root = init_root();
Assert.assertFalse(JSON.toJSONString(root).contains("null"));
Assert.assertFalse(JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue).contains("null"));
}

@Test
public void test_root2_with_Issue3672BArray() {
root2 = init_root2();
Assert.assertFalse(JSON.toJSONString(root2).contains("null"));
Assert.assertFalse(JSON.toJSONString(root2, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue).contains("null"));
}

@Test
public void test_pretty_order_same_with_normal() {
root = init_root();
root2 = init_root2();
String removeELements = "[\t\n ]";
Assert.assertEquals(JSON.toJSONString(root).replaceAll(removeELements, "")
, JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue).replaceAll(removeELements, ""));
Assert.assertEquals(JSON.toJSONString(root2).replaceAll(removeELements, "")
, JSON.toJSONString(root2, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue).replaceAll(removeELements, ""));
}

@Data
private class Issue3672Root {
private Issue3672A a;
}

@Data
private class Issue3672Root2 {
private Issue3672A2 a;
}

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

@Data
private class Issue3672A2 {
private Issue3672B[] b;
}

@Data
private class Issue3672B {
private Issue3672C c;
Expand Down

0 comments on commit aea8123

Please sign in to comment.