1- package com .auth0 .jwtdecodejava .impl . jackson ;
1+ package com .auth0 .jwtdecodejava .impl ;
22
3- import com .auth0 .jwtdecodejava .impl . BaseClaim ;
3+ import com .auth0 .jwtdecodejava .interfaces . Claim ;
44import com .fasterxml .jackson .databind .JsonNode ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import com .fasterxml .jackson .databind .node .NullNode ;
67import com .sun .istack .internal .NotNull ;
7-
88import java .lang .reflect .Array ;
99import java .util .ArrayList ;
1010import java .util .Date ;
1111import java .util .List ;
12+ import java .util .Map ;
1213
13- public class ClaimImpl extends BaseClaim {
14+ public class ClaimImpl implements Claim {
1415
1516 private final JsonNode data ;
1617
17- public ClaimImpl (@ NotNull String name , @ NotNull JsonNode node ) {
18- super (name );
19- this .data = node ;
18+ public ClaimImpl (@ NotNull JsonNode node ) {
19+ this .data = node == null ? NullNode .getInstance () : node ;
2020 }
2121
2222 @ Override
@@ -31,38 +31,41 @@ public boolean isNull() {
3131
3232 @ Override
3333 public Boolean asBoolean () {
34- return data .asBoolean ();
34+ return isNull () ? null : data .asBoolean ();
3535 }
3636
3737 @ Override
3838 public Integer asInt () {
39- return data .asInt ();
39+ return isNull () ? null : data .asInt ();
4040 }
4141
4242 @ Override
4343 public Double asDouble () {
44- return data .asDouble ();
44+ return isNull () ? null : data .asDouble ();
4545 }
4646
4747 @ Override
4848 public String asString () {
49- return data .asText ();
49+ return isNull () ? null : data .asText ();
5050 }
5151
5252 @ Override
5353 public Date asDate () {
54+ if (isNull ()) {
55+ return null ;
56+ }
5457 long seconds = data .asLong ();
5558 return new Date (seconds * 1000 );
5659 }
5760
5861 @ Override
5962 @ SuppressWarnings ("unchecked" )
6063 public <T > T [] asArray (Class <T > tClazz ) throws Exception {
61- ObjectMapper mapper = new ObjectMapper ();
62- if (!data .isArray ()) {
64+ if (data .isNull () || !data .isArray ()) {
6365 return (T []) Array .newInstance (tClazz , 0 );
6466 }
6567
68+ ObjectMapper mapper = new ObjectMapper ();
6669 T [] arr = (T []) Array .newInstance (tClazz , data .size ());
6770 for (int i = 0 ; i < data .size (); i ++) {
6871 arr [i ] = mapper .treeToValue (data .get (i ), tClazz );
@@ -72,15 +75,31 @@ public <T> T[] asArray(Class<T> tClazz) throws Exception {
7275
7376 @ Override
7477 public <T > List <T > asList (Class <T > tClazz ) throws Exception {
75- ObjectMapper mapper = new ObjectMapper ();
76- if (!data .isArray ()) {
78+ if (data .isNull () || !data .isArray ()) {
7779 return new ArrayList <>(0 );
7880 }
7981
82+ ObjectMapper mapper = new ObjectMapper ();
8083 List <T > list = new ArrayList <>();
8184 for (int i = 0 ; i < data .size (); i ++) {
8285 list .add (mapper .treeToValue (data .get (i ), tClazz ));
8386 }
8487 return list ;
8588 }
89+
90+ public static Claim extractClaim (@ NotNull String claimName , @ NotNull Map <String , JsonNode > tree ) {
91+ JsonNode node = tree .get (claimName );
92+ return claimFromNode (node );
93+ }
94+
95+ @ NotNull
96+ public static Claim claimFromNode (JsonNode node ) {
97+ if (node == null || node .isMissingNode ()) {
98+ return new MissingClaim ();
99+ }
100+ if (node .isObject () && node .size () == 0 ) {
101+ return new NullClaim ();
102+ }
103+ return new ClaimImpl (node );
104+ }
86105}
0 commit comments