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

fix AfterFilter NullPointer exception in #3443 #3475

Merged
merged 2 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protected final void writeKeyValue(String key, Object value) {
JSONSerializer serializer = serializerLocal.get();
char seperator = seperatorLocal.get();

boolean ref = serializer.references.containsKey(value);
boolean ref = serializer.containsReference(value);
serializer.writeKeyValue(seperator, key, value);
if (!ref) {
if (!ref && serializer.references != null) {
serializer.references.remove(value);
}
if (seperator != ',') {
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_3300/Issue3443.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.alibaba.json.bvt.issue_3300;

import com.alibaba.fastjson.serializer.*;
import junit.framework.TestCase;

public class Issue3443 extends TestCase {
public void testCustomJsonSerializerAndAfterFilter() throws Exception {
SerializeWriter serializeWriter = new SerializeWriter();
try {
JSONSerializer jsonSerializer = new JSONSerializer(serializeWriter, new SerializeConfig());

Parameter parameter = new Parameter();
parameter.setParameterDesc(new ParameterDesc("vipExpireDate", "VIP expire date."));

jsonSerializer.config(SerializerFeature.DisableCircularReferenceDetect, true);
jsonSerializer.getAfterFilters().add(new CustomFilter());
jsonSerializer.write(parameter);
assertEquals("{\"parameterDesc\":{\"ParameterDesc\":\"VIP expire date.\"}}", serializeWriter.toString());
} finally {
serializeWriter.close();
}
}

static class Parameter {
private ParameterDesc parameterDesc;

public ParameterDesc getParameterDesc() {
return parameterDesc;
}

public void setParameterDesc(ParameterDesc parameterType) {
this.parameterDesc = parameterType;
}
}

static class ParameterDesc {
private String parameterName;
private String parameterUsage;
// do some work...

public ParameterDesc(String parameterName, String parameterUsage) {
this.parameterName = parameterName;
this.parameterUsage = parameterUsage;
}


}

static class CustomFilter extends AfterFilter {

@Override
public void writeAfter(Object object) {
if (object instanceof ParameterDesc) {
writeKeyValue("ParameterDesc", "VIP expire date.");
}
}
}
}