44import com .auth0 .jwt .interfaces .Claim ;
55import com .fasterxml .jackson .core .JsonParser ;
66import com .fasterxml .jackson .core .JsonProcessingException ;
7+ import com .fasterxml .jackson .core .ObjectCodec ;
78import com .fasterxml .jackson .core .type .TypeReference ;
89import com .fasterxml .jackson .databind .JsonNode ;
9- import com .fasterxml .jackson .databind .ObjectReader ;
1010
1111import java .io .IOException ;
1212import java .lang .reflect .Array ;
2121 */
2222class JsonNodeClaim implements Claim {
2323
24- private final ObjectReader objectReader ;
24+ private final ObjectCodec codec ;
2525 private final JsonNode data ;
2626
27- private JsonNodeClaim (JsonNode node , ObjectReader objectReader ) {
27+ private JsonNodeClaim (JsonNode node , ObjectCodec codec ) {
2828 this .data = node ;
29- this .objectReader = objectReader ;
29+ this .codec = codec ;
3030 }
3131
3232 @ Override
@@ -82,7 +82,7 @@ public <T> T[] asArray(Class<T> clazz) throws JWTDecodeException {
8282 T [] arr = (T []) Array .newInstance (clazz , data .size ());
8383 for (int i = 0 ; i < data .size (); i ++) {
8484 try {
85- arr [i ] = objectReader .treeToValue (data .get (i ), clazz );
85+ arr [i ] = codec .treeToValue (data .get (i ), clazz );
8686 } catch (JsonProcessingException e ) {
8787 throw new JWTDecodeException ("Couldn't map the Claim's array contents to " + clazz .getSimpleName (), e );
8888 }
@@ -99,7 +99,7 @@ public <T> List<T> asList(Class<T> clazz) throws JWTDecodeException {
9999 List <T > list = new ArrayList <>();
100100 for (int i = 0 ; i < data .size (); i ++) {
101101 try {
102- list .add (objectReader .treeToValue (data .get (i ), clazz ));
102+ list .add (codec .treeToValue (data .get (i ), clazz ));
103103 } catch (JsonProcessingException e ) {
104104 throw new JWTDecodeException ("Couldn't map the Claim's array contents to " + clazz .getSimpleName (), e );
105105 }
@@ -113,11 +113,11 @@ public Map<String, Object> asMap() throws JWTDecodeException {
113113 return null ;
114114 }
115115
116- try {
117- TypeReference < Map < String , Object >> mapType = new TypeReference < Map < String , Object >>() {
118- };
119- JsonParser thisParser = objectReader .treeAsTokens (data );
120- return thisParser .readValueAs (mapType );
116+ TypeReference < Map < String , Object >> mapType = new TypeReference < Map < String , Object >>() {
117+ };
118+
119+ try ( JsonParser parser = codec .treeAsTokens (data )) {
120+ return parser .readValueAs (mapType );
121121 } catch (IOException e ) {
122122 throw new JWTDecodeException ("Couldn't map the Claim value to Map" , e );
123123 }
@@ -129,8 +129,8 @@ public <T> T as(Class<T> clazz) throws JWTDecodeException {
129129 if (isMissing () || isNull ()) {
130130 return null ;
131131 }
132- return objectReader . treeAsTokens (data ). readValueAs ( clazz );
133- } catch (IOException e ) {
132+ return codec . treeToValue (data , clazz );
133+ } catch (JsonProcessingException e ) {
134134 throw new JWTDecodeException ("Couldn't map the Claim value to " + clazz .getSimpleName (), e );
135135 }
136136 }
@@ -160,21 +160,23 @@ public String toString() {
160160 *
161161 * @param claimName the Claim to search for.
162162 * @param tree the JsonNode tree to search the Claim in.
163+ * @param objectCodec the object codec in use for deserialization
163164 * @return a valid non-null Claim.
164165 */
165- static Claim extractClaim (String claimName , Map <String , JsonNode > tree , ObjectReader objectReader ) {
166+ static Claim extractClaim (String claimName , Map <String , JsonNode > tree , ObjectCodec objectCodec ) {
166167 JsonNode node = tree .get (claimName );
167- return claimFromNode (node , objectReader );
168+ return claimFromNode (node , objectCodec );
168169 }
169170
170171 /**
171172 * Helper method to create a Claim representation from the given JsonNode.
172173 *
173174 * @param node the JsonNode to convert into a Claim.
175+ * @param objectCodec the object codec in use for deserialization
174176 * @return a valid Claim instance. If the node is null or missing, a NullClaim will be returned.
175177 */
176- static Claim claimFromNode (JsonNode node , ObjectReader objectReader ) {
177- return new JsonNodeClaim (node , objectReader );
178+ static Claim claimFromNode (JsonNode node , ObjectCodec objectCodec ) {
179+ return new JsonNodeClaim (node , objectCodec );
178180 }
179181
180182}
0 commit comments