Skip to content

Commit 3b145c7

Browse files
cyejingkexianjun
authored andcommitted
fix fastjson serialization with generic return type (#3771)
1 parent 90ffbc2 commit 3b145c7

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

dubbo-serialization/dubbo-serialization-fastjson/src/main/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInput.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.dubbo.common.serialize.fastjson;
1818

1919
import org.apache.dubbo.common.serialize.ObjectInput;
20-
import org.apache.dubbo.common.utils.PojoUtils;
2120

2221
import com.alibaba.fastjson.JSON;
2322

@@ -103,8 +102,8 @@ public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException
103102
@Override
104103
@SuppressWarnings("unchecked")
105104
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
106-
Object value = readObject(cls);
107-
return (T) PojoUtils.realize(value, cls, type);
105+
String json = readLine();
106+
return (T) JSON.parseObject(json, type);
108107
}
109108

110109
private String readLine() throws IOException, EOFException {

dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import com.alibaba.fastjson.JSONObject;
2020

21+
import java.lang.reflect.Method;
22+
import java.lang.reflect.Type;
23+
import java.util.List;
24+
import org.apache.dubbo.common.serialize.model.Organization;
2125
import org.apache.dubbo.common.serialize.model.Person;
2226

2327
import org.junit.jupiter.api.Assertions;
@@ -32,6 +36,7 @@
3236
import static org.hamcrest.CoreMatchers.nullValue;
3337
import static org.hamcrest.core.Is.is;
3438
import static org.hamcrest.MatcherAssert.assertThat;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
3540

3641
public class FastJsonObjectInputTest {
3742
private FastJsonObjectInput fastJsonObjectInput;
@@ -151,4 +156,44 @@ public void testReadObjectWithoutClass() throws IOException, ClassNotFoundExcept
151156
assertThat(readObject.getString("name"), is("John"));
152157
assertThat(readObject.getInteger("age"), is(30));
153158
}
154-
}
159+
160+
161+
@Test
162+
public void testReadObjectWithTowType() throws Exception {
163+
fastJsonObjectInput = new FastJsonObjectInput(new StringReader("[{\"name\":\"John\",\"age\":30},{\"name\":\"Born\",\"age\":24}]"));
164+
165+
Method methodReturnType = getClass().getMethod("towLayer");
166+
Type type = methodReturnType.getGenericReturnType();
167+
List<Person> o = fastJsonObjectInput.readObject(List.class, type);
168+
169+
assertTrue(o instanceof List);
170+
assertTrue(o.get(0) instanceof Person);
171+
172+
assertThat(o.size(), is(2));
173+
assertThat(o.get(1).getName(), is("Born"));
174+
}
175+
176+
@Test
177+
public void testReadObjectWithThreeType() throws Exception {
178+
fastJsonObjectInput = new FastJsonObjectInput(new StringReader("{\"data\":[{\"name\":\"John\",\"age\":30},{\"name\":\"Born\",\"age\":24}]}"));
179+
180+
Method methodReturnType = getClass().getMethod("threeLayer");
181+
Type type = methodReturnType.getGenericReturnType();
182+
Organization<List<Person>> o = fastJsonObjectInput.readObject(Organization.class, type);
183+
184+
assertTrue(o instanceof Organization);
185+
assertTrue(o.getData() instanceof List);
186+
assertTrue(o.getData().get(0) instanceof Person);
187+
188+
assertThat(o.getData().size(), is(2));
189+
assertThat(o.getData().get(1).getName(), is("Born"));
190+
}
191+
192+
public List<Person> towLayer() {
193+
return null;
194+
}
195+
196+
public Organization<List<Person>> threeLayer() {
197+
return null;
198+
}
199+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.common.serialize.model;
18+
19+
public class Organization<T> {
20+
21+
private T data;
22+
23+
public T getData() {
24+
return data;
25+
}
26+
27+
public void setData(T data) {
28+
this.data = data;
29+
}
30+
}

0 commit comments

Comments
 (0)