Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix jsonb deserialize error on null list field ,for issue #2181 #2184

Merged
merged 4 commits into from
Jan 15, 2024
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 @@ -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