diff --git a/src/main/java/com/alibaba/fastjson/serializer/ObjectArrayCodec.java b/src/main/java/com/alibaba/fastjson/serializer/ObjectArrayCodec.java index bc63e95c54..7286cd8832 100644 --- a/src/main/java/com/alibaba/fastjson/serializer/ObjectArrayCodec.java +++ b/src/main/java/com/alibaba/fastjson/serializer/ObjectArrayCodec.java @@ -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(','); } diff --git a/src/test/java/com/alibaba/json/bvt/issue_3600/Issue3672.java b/src/test/java/com/alibaba/json/bvt/issue_3600/Issue3672.java index 8a4d2685f5..5e9183cd2e 100644 --- a/src/test/java/com/alibaba/json/bvt/issue_3600/Issue3672.java +++ b/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(); @@ -23,12 +25,46 @@ 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 @@ -36,11 +72,21 @@ 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;