diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderList.java b/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderList.java index a45f5d0595..0e73a6bd36 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderList.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderList.java @@ -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( diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2181.java b/core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2181.java new file mode 100644 index 0000000000..a414d05828 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2181.java @@ -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); + } +} diff --git a/pom.xml b/pom.xml index b29ec3d7b9..5eaf43c4a5 100644 --- a/pom.xml +++ b/pom.xml @@ -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