22
33import com .auth0 .jwt .exceptions .JWTDecodeException ;
44import com .auth0 .jwt .interfaces .Claim ;
5+ import com .fasterxml .jackson .core .JsonParser ;
56import com .fasterxml .jackson .core .JsonProcessingException ;
67import com .fasterxml .jackson .core .type .TypeReference ;
78import com .fasterxml .jackson .databind .JsonNode ;
@@ -66,11 +67,10 @@ public <T> T[] asArray(Class<T> tClazz) throws JWTDecodeException {
6667 return null ;
6768 }
6869
69- ObjectMapper mapper = new ObjectMapper ();
7070 T [] arr = (T []) Array .newInstance (tClazz , data .size ());
7171 for (int i = 0 ; i < data .size (); i ++) {
7272 try {
73- arr [i ] = mapper .treeToValue (data .get (i ), tClazz );
73+ arr [i ] = getObjectMapper () .treeToValue (data .get (i ), tClazz );
7474 } catch (JsonProcessingException e ) {
7575 throw new JWTDecodeException ("Couldn't map the Claim's array contents to " + tClazz .getSimpleName (), e );
7676 }
@@ -84,11 +84,10 @@ public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException {
8484 return null ;
8585 }
8686
87- ObjectMapper mapper = new ObjectMapper ();
8887 List <T > list = new ArrayList <>();
8988 for (int i = 0 ; i < data .size (); i ++) {
9089 try {
91- list .add (mapper .treeToValue (data .get (i ), tClazz ));
90+ list .add (getObjectMapper () .treeToValue (data .get (i ), tClazz ));
9291 } catch (JsonProcessingException e ) {
9392 throw new JWTDecodeException ("Couldn't map the Claim's array contents to " + tClazz .getSimpleName (), e );
9493 }
@@ -102,21 +101,21 @@ public Map<String, Object> asMap() throws JWTDecodeException {
102101 return null ;
103102 }
104103
105- ObjectMapper mapper = new ObjectMapper ();
106104 try {
107105 TypeReference <Map <String , Object >> mapType = new TypeReference <Map <String , Object >>() {
108106 };
109- return mapper .treeAsTokens (data ).readValueAs (mapType );
107+ ObjectMapper thisMapper = getObjectMapper ();
108+ JsonParser thisParser = thisMapper .treeAsTokens (data );
109+ return thisParser .readValueAs (mapType );
110110 } catch (IOException e ) {
111111 throw new JWTDecodeException ("Couldn't map the Claim value to Map" , e );
112112 }
113113 }
114114
115115 @ Override
116116 public <T > T as (Class <T > tClazz ) throws JWTDecodeException {
117- ObjectMapper mapper = new ObjectMapper ();
118117 try {
119- return mapper .treeAsTokens (data ).readValueAs (tClazz );
118+ return getObjectMapper () .treeAsTokens (data ).readValueAs (tClazz );
120119 } catch (IOException e ) {
121120 throw new JWTDecodeException ("Couldn't map the Claim value to " + tClazz .getSimpleName (), e );
122121 }
@@ -151,4 +150,9 @@ static Claim claimFromNode(JsonNode node) {
151150 }
152151 return new JsonNodeClaim (node );
153152 }
153+
154+ //Visible for testing
155+ ObjectMapper getObjectMapper () {
156+ return new ObjectMapper ();
157+ }
154158}
0 commit comments