Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Fix Bug of issue #3672, ObjectArrayCodec's PrettyFormat do not transfer
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Certseeds committed Mar 9, 2021
1 parent e4308b7 commit c1a8acf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public final void write(JSONSerializer serializer, Object object, Object fieldNa
out.write(',');
serializer.println();
}
serializer.write(array[i]);
Object item = array[i];
if (serializer.containsReference(item)) {
serializer.writeReference(item);
} else {
serializer.writeWithFieldName(item, i);
}
}
serializer.decrementIdent();
serializer.println();
Expand All @@ -94,14 +99,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
80 changes: 80 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_3600/Issue3672.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.alibaba.json.bvt.issue_3600;

import com.alibaba.fastjson.JSON;
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;

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));
Assert.assertFalse(JSON.toJSONString(root).contains("null"));
Assert.assertFalse(JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue).contains("null"));
}

@Test
public void test2() {
Issue3672Root2 root = new Issue3672Root2();
Issue3672A2 a = new Issue3672A2();
Issue3672B b = new Issue3672B();
Issue3672C c = new Issue3672C();
Issue3672D d = new Issue3672D();
root.setA(a);
a.setB(Lists.newArrayList(b).toArray(new Issue3672B[0]));
b.setC(c);
c.setD(d);
d.setE(Lists.newArrayList(c));
Assert.assertFalse(JSON.toJSONString(root).contains("null"));
Assert.assertFalse(JSON.toJSONString(root, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue).contains("null"));
}

@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;
}

@Data
private class Issue3672C {
private Issue3672D d;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public <T> T decodeObject(byte[] input, Class<T> clazz) throws Exception {
return (T) JSON.parseObject(input, clazz, Feature.SupportArrayToBean, Feature.DisableCircularReferenceDetect);
}

@Override
//@Override
public byte[] encodeToBytes(Object object) throws Exception {
// TODO Auto-generated method stub
return null;
}

@Override
//@Override
public void encode(OutputStream out, Object object) throws Exception {
out.write(encodeToBytes(object));
}
Expand Down

0 comments on commit c1a8acf

Please sign in to comment.