Skip to content

Commit

Permalink
fix jsonb deserialize error on null list field ,for issue #2181 (#2184)
Browse files Browse the repository at this point in the history
fix jsonb deserialize error on null list field ,for issue #2181
  • Loading branch information
rowstop authored Jan 15, 2024
1 parent c10c354 commit 297b55d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ public void readFieldValue(JSONReader jsonReader, T object) {
public Object readFieldValue(JSONReader jsonReader) {
if (jsonReader.jsonb) {
int entryCnt = jsonReader.startArray();

if (entryCnt == -1) {
return null;
}
Object[] array = new Object[entryCnt];
ObjectReader itemObjectReader
= getItemObjectReader(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.alibaba.fastjson2.issues_2100;

import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.dubbo.common.serialize.fastjson2.FastJson2ObjectInput;
import org.apache.dubbo.common.serialize.fastjson2.FastJson2ObjectOutput;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author 张治保
* @since 2024/1/15
*/
public class Issue2181 {
@ToString
@Getter
@Setter
public static class CommonException extends RuntimeException {
Integer code;
String message;
List params;

public CommonException(Integer code, String message) {
// super(message);
this.message = message;
this.code = code;
}

}

/**
* @see FastJson2ObjectOutput#writeObject(Object)
* @see FastJson2ObjectInput#readObject(Class)
*/
@Test
void test() {
//writeObject
CommonException error = new CommonException(1, "error");
byte[] bytes = JSONB.toBytes(
error,
JSONWriter.Feature.WriteClassName,
JSONWriter.Feature.FieldBased,
JSONWriter.Feature.ErrorOnNoneSerializable,
JSONWriter.Feature.ReferenceDetection,
JSONWriter.Feature.WriteNulls,
JSONWriter.Feature.NotWriteDefaultValue,
JSONWriter.Feature.NotWriteHashMapArrayListClassName,
JSONWriter.Feature.WriteNameAsSymbol);
// readObject
Object result = JSONB.parseObject(
bytes,
Exception.class,
JSONReader.autoTypeFilter(true, CommonException.class),
JSONReader.Feature.UseDefaultConstructorAsPossible,
JSONReader.Feature.ErrorOnNoneSerializable,
JSONReader.Feature.IgnoreAutoTypeNotMatch,
JSONReader.Feature.UseNativeObject,
JSONReader.Feature.FieldBased);
assertNotNull(result);
assertSame(result.getClass(), CommonException.class);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@
com/alibaba/fastjson2/util/BeanUtilsTest.java,
com/alibaba/fastjson2/issues_1000/Issue1395.java,
com/alibaba/fastjson2/issues_2100/Issue2164.java,
com/alibaba/fastjson2/issues_2100/Issue2181.java,
com/alibaba/fastjson/v2issues/Issue1432.java
</excludes>
</configuration>
Expand Down

0 comments on commit 297b55d

Please sign in to comment.