Skip to content

Commit c1b0b63

Browse files
committed
fix ErrorOnNoneSerializable error on GenericType, for issue #3499
1 parent fa3dac1 commit c1b0b63

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

core/src/main/java/com/alibaba/fastjson2/reader/FieldReaderObject.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ public void readFieldValueJSONB(JSONReader jsonReader, T object) {
182182
jsonReader.skipValue();
183183
return;
184184
} else if ((contextFeatures & JSONReader.Feature.ErrorOnNoneSerializable.mask) != 0) {
185-
throw new JSONException("not support none-Serializable");
185+
if (fieldClass != Object.class || (jsonReader.isObject() || jsonReader.isArray())) {
186+
throw new JSONException("not support none-Serializable");
187+
}
186188
}
187189
}
188190

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.alibaba.fastjson2.issues_3400;
2+
3+
import com.alibaba.fastjson2.JSONB;
4+
import com.alibaba.fastjson2.JSONReader;
5+
import com.alibaba.fastjson2.JSONWriter;
6+
import com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
import lombok.Setter;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.io.Serializable;
14+
15+
public class Issue3499 {
16+
@Test
17+
public void test() {
18+
Message<String> obj = new Message<>("Hello");
19+
byte[] bytes = JSONB.toBytes(obj, JSONWriter.Feature.WriteClassName,
20+
JSONWriter.Feature.FieldBased,
21+
JSONWriter.Feature.ErrorOnNoneSerializable,
22+
JSONWriter.Feature.ReferenceDetection,
23+
JSONWriter.Feature.WriteNulls,
24+
JSONWriter.Feature.NotWriteDefaultValue,
25+
JSONWriter.Feature.NotWriteHashMapArrayListClassName,
26+
JSONWriter.Feature.WriteNameAsSymbol);
27+
ContextAutoTypeBeforeHandler handler = new ContextAutoTypeBeforeHandler(true, obj.getClass());
28+
Object result = JSONB.parseObject(bytes, obj.getClass(), handler,
29+
JSONReader.Feature.UseDefaultConstructorAsPossible,
30+
JSONReader.Feature.ErrorOnNoneSerializable,
31+
JSONReader.Feature.IgnoreAutoTypeNotMatch,
32+
JSONReader.Feature.UseNativeObject,
33+
JSONReader.Feature.FieldBased);
34+
}
35+
36+
@Getter
37+
@Setter
38+
@NoArgsConstructor
39+
@AllArgsConstructor
40+
static class Message<T>
41+
implements Serializable {
42+
T msg;
43+
}
44+
}

0 commit comments

Comments
 (0)