This is an automated email from the ASF dual-hosted git repository.
zrlw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git
The following commit(s) were added to refs/heads/master by this push:
new 18fc847e Fix FastJsonObjectInput to support reading object by type
(#657)
18fc847e is described below
commit 18fc847eff89e5b98a43af2a0171de13e059824c
Author: zrlw <[email protected]>
AuthorDate: Thu Jun 19 13:35:50 2025 +0800
Fix FastJsonObjectInput to support reading object by type (#657)
---
.../serialize/fastjson/FastJsonObjectInput.java | 23 +++++++++++++---------
.../fastjson/FastJsonObjectInputOutputTest.java | 2 +-
.../fastjson/FastJsonSerializationTest.java | 17 ++++++++++------
3 files changed, 26 insertions(+), 16 deletions(-)
diff --git
a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java
b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java
index 84853003..c6ae43d5 100644
---
a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java
+++
b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java
@@ -49,11 +49,7 @@ public class FastJsonObjectInput implements
DefaultJsonDataInput {
return readObject(cls, type, null);
}
- public Object readObject(ParserConfig.AutoTypeCheckHandler handler) throws
IOException {
- return readObject(Object.class, null, handler);
- }
-
- private <T> T readObject(Class<T> cls, Type type,
ParserConfig.AutoTypeCheckHandler handler) throws IOException {
+ public <T> T readObject(Class<T> cls, Type type,
ParserConfig.AutoTypeCheckHandler handler) throws IOException {
int length = readLength();
byte[] bytes = new byte[length];
int read = is.read(bytes, 0, length);
@@ -67,14 +63,23 @@ public class FastJsonObjectInput implements
DefaultJsonDataInput {
parserConfig.addAutoTypeCheckHandler(handler);
}
- Object result = JSON.parseObject(new String(bytes), cls,
+ Object result;
+ if (cls != null) {
+ result = JSON.parseObject(new String(bytes), cls,
parserConfig,
Feature.SupportNonPublicField,
Feature.SupportAutoType
- );
- if (result != null && cls != null &&
!ClassUtils.isMatch(result.getClass(), cls)) {
- throw new IllegalArgumentException(
+ );
+ if (result != null && !ClassUtils.isMatch(result.getClass(), cls))
{
+ throw new IllegalArgumentException(
"deserialize failed. expected class: " + cls + " but
actual class: " + result.getClass());
+ }
+ } else {
+ result = JSON.parseObject(new String(bytes), type,
+ parserConfig,
+ Feature.SupportNonPublicField,
+ Feature.SupportAutoType
+ );
}
return (T) result;
diff --git
a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java
b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java
index de685df6..78d65068 100644
---
a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java
+++
b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java
@@ -195,7 +195,7 @@ public class FastJsonObjectInputOutputTest {
fastJsonObjectOutput.flushBuffer();
pos.close();
- Object result = fastJsonObjectInput.readObject(new TestPojoHandler());
+ Object result = fastJsonObjectInput.readObject(null, null, new
TestPojoHandler());
Assertions.assertNotNull(result);
Assertions.assertEquals("Bob", ((TestPojo) result).getData());
diff --git
a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerializationTest.java
b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerializationTest.java
index 0f1f6f42..31d5b321 100644
---
a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerializationTest.java
+++
b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonSerializationTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.dubbo.common.serialize.fastjson;
+import com.alibaba.fastjson.TypeReference;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
@@ -29,6 +30,7 @@ import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
@@ -82,8 +84,8 @@ class FastJsonSerializationTest {
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInput objectInput = serialization.deserialize(url,
inputStream);
- // this will not throw exception
- // Assertions.assertThrows(IOException.class,
objectInput::readUTF);
+ // Todo should throw exception but return "{}"
+ Assertions.assertEquals("{}", objectInput.readUTF());
}
// write pojo, read failed
@@ -159,7 +161,7 @@ class FastJsonSerializationTest {
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInput objectInput = serialization.deserialize(url,
inputStream);
- // @Todo this not pass
+ // Todo this not pass: it will not throw IOException but return
"{}"
// Assertions.assertThrows(IOException.class,
objectInput::readEvent);
}
@@ -291,7 +293,8 @@ class FastJsonSerializationTest {
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInput objectInput = serialization.deserialize(url,
inputStream);
- Assertions.assertThrows(IOException.class,
objectInput::readObject);
+ Type trustedPojoListType = new
TypeReference<LinkedList<TrustedPojo>>() {}.getType();
+ Assertions.assertEquals(pojos, objectInput.readObject(null,
trustedPojoListType));
}
// write pojo, read pojo
@@ -324,7 +327,8 @@ class FastJsonSerializationTest {
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInput objectInput = serialization.deserialize(url,
inputStream);
- Assertions.assertThrows(IOException.class, () ->
objectInput.readObject(List.class));
+ Type trustedPojoListType = new
TypeReference<LinkedList<TrustedPojo>>() {}.getType();
+ Assertions.assertEquals(pojos, objectInput.readObject(null,
trustedPojoListType));
}
// write list, read list
@@ -342,7 +346,8 @@ class FastJsonSerializationTest {
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectInput objectInput = serialization.deserialize(url,
inputStream);
- Assertions.assertThrows(IOException.class, () ->
objectInput.readObject(LinkedList.class));
+ Type trustedPojoListType = new
TypeReference<LinkedList<TrustedPojo>>() {}.getType();
+ Assertions.assertEquals(pojos, objectInput.readObject(null,
trustedPojoListType));
}
frameworkModel.destroy();