Skip to content

Commit 5672e52

Browse files
cyejingkexianjun
authored andcommitted
fix fastjson serialize type (#3767)
1 parent 8683da7 commit 5672e52

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

dubbo-serialization/dubbo-serialization-fastjson/src/main/java/com/alibaba/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 com.alibaba.dubbo.common.serialize.fastjson;
1818

1919
import com.alibaba.dubbo.common.serialize.ObjectInput;
20-
import com.alibaba.dubbo.common.utils.PojoUtils;
2120
import com.alibaba.fastjson.JSON;
2221

2322
import java.io.BufferedReader;
@@ -99,8 +98,8 @@ public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException
9998
@Override
10099
@SuppressWarnings("unchecked")
101100
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
102-
Object value = readObject(cls);
103-
return (T) PojoUtils.realize(value, cls, type);
101+
String json = readLine();
102+
return (T) JSON.parseObject(json, type);
104103
}
105104

106105
private String readLine() throws IOException, EOFException {

dubbo-serialization/dubbo-serialization-fastjson/src/test/java/com/alibaba/dubbo/common/serialize/fastjson/FastJsonObjectInputTest.java

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@
1616
*/
1717
package com.alibaba.dubbo.common.serialize.fastjson;
1818

19+
import static org.hamcrest.CoreMatchers.not;
20+
import static org.hamcrest.CoreMatchers.nullValue;
21+
import static org.hamcrest.core.Is.is;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThat;
24+
import static org.junit.Assert.assertTrue;
25+
26+
import com.alibaba.dubbo.common.serialize.fastjson.model.Organization;
1927
import com.alibaba.dubbo.common.serialize.fastjson.model.Person;
2028
import com.alibaba.fastjson.JSONObject;
21-
import org.junit.Test;
22-
2329
import java.io.ByteArrayInputStream;
2430
import java.io.EOFException;
2531
import java.io.IOException;
2632
import java.io.StringReader;
27-
28-
import static org.hamcrest.CoreMatchers.not;
29-
import static org.hamcrest.CoreMatchers.nullValue;
30-
import static org.hamcrest.core.Is.is;
31-
import static org.junit.Assert.assertThat;
33+
import java.lang.reflect.Method;
34+
import java.lang.reflect.Type;
35+
import java.util.List;
36+
import javax.lang.model.util.Types;
37+
import org.junit.Test;
3238

3339
public class FastJsonObjectInputTest {
3440
private FastJsonObjectInput fastJsonObjectInput;
@@ -144,4 +150,43 @@ public void testReadObjectWithoutClass() throws IOException, ClassNotFoundExcept
144150
assertThat(readObject.getString("name"), is("John"));
145151
assertThat(readObject.getInteger("age"), is(30));
146152
}
147-
}
153+
154+
@Test
155+
public void testReadObjectWithTowType() throws Exception {
156+
fastJsonObjectInput = new FastJsonObjectInput(new StringReader("[{\"name\":\"John\",\"age\":30},{\"name\":\"Born\",\"age\":24}]"));
157+
158+
Method methodReturnType = getClass().getMethod("towLayer");
159+
Type type = methodReturnType.getGenericReturnType();
160+
List<Person> o = fastJsonObjectInput.readObject(List.class, type);
161+
162+
assertTrue(o instanceof List);
163+
assertTrue(o.get(0) instanceof Person);
164+
165+
assertThat(o.size(), is(2));
166+
assertThat(o.get(1).getName(), is("Born"));
167+
}
168+
169+
@Test
170+
public void testReadObjectWithThreeType() throws Exception {
171+
fastJsonObjectInput = new FastJsonObjectInput(new StringReader("{\"data\":[{\"name\":\"John\",\"age\":30},{\"name\":\"Born\",\"age\":24}]}"));
172+
173+
Method methodReturnType = getClass().getMethod("threeLayer");
174+
Type type = methodReturnType.getGenericReturnType();
175+
Organization<List<Person>> o = fastJsonObjectInput.readObject(Organization.class, type);
176+
177+
assertTrue(o instanceof Organization);
178+
assertTrue(o.getData() instanceof List);
179+
assertTrue(o.getData().get(0) instanceof Person);
180+
181+
assertThat(o.getData().size(), is(2));
182+
assertThat(o.getData().get(1).getName(), is("Born"));
183+
}
184+
185+
public List<Person> towLayer() {
186+
return null;
187+
}
188+
189+
public Organization<List<Person>> threeLayer() {
190+
return null;
191+
}
192+
}
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 com.alibaba.dubbo.common.serialize.fastjson.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)