22
33import java .io .IOException ;
44import java .io .InputStream ;
5+ import java .lang .reflect .Type ;
56import java .nio .charset .Charset ;
67import java .text .SimpleDateFormat ;
78import java .util .ArrayList ;
9+ import java .util .HashMap ;
810import java .util .List ;
11+ import java .util .Map ;
912
1013import com .ejlchina .okhttps .internal .HttpException ;
1114import com .fasterxml .jackson .core .JsonProcessingException ;
15+ import com .fasterxml .jackson .core .type .TypeReference ;
1216import com .fasterxml .jackson .databind .JsonNode ;
1317import com .fasterxml .jackson .databind .ObjectMapper ;
1418import com .fasterxml .jackson .databind .SerializationFeature ;
@@ -20,6 +24,9 @@ public class JacksonMsgConvertor implements MsgConvertor, ConvertProvider {
2024
2125 private ObjectMapper objectMapper ;
2226
27+ private final Map <Type , TypeReference <?>> cache = new HashMap <>();
28+
29+
2330 public JacksonMsgConvertor () {
2431 this (new ObjectMapper ());
2532 }
@@ -84,10 +91,28 @@ public byte[] serialize(Object object, String dateFormat, Charset charset) {
8491 }
8592 }
8693
94+ private <T > TypeReference <T > toTypeRef (Type type ) {
95+ TypeReference <T > typeRef ;
96+ synchronized (cache ) {
97+ //noinspection unchecked
98+ typeRef = (TypeReference <T >) cache .get (type );
99+ if (typeRef == null ) {
100+ typeRef = new TypeReference <T >() {
101+ @ Override
102+ public Type getType () {
103+ return type ;
104+ }
105+ };
106+ cache .put (type , typeRef );
107+ }
108+ }
109+ return typeRef ;
110+ }
111+
87112 @ Override
88- public <T > T toBean (Class < T > type , InputStream in , Charset charset ) {
113+ public <T > T toBean (Type type , InputStream in , Charset charset ) {
89114 try {
90- return objectMapper .readValue (in , type );
115+ return objectMapper .readValue (in , toTypeRef ( type ) );
91116 } catch (IOException e ) {
92117 throw new HttpException ("Jackson 解析异常" , e );
93118 }
0 commit comments