toJson method

Map<String, dynamic> toJson()

Converts the claim set into a Map suitable for encoding as JSON.

Implementation

Map<String, dynamic?> toJson() {
  final body = SplayTreeMap<String, dynamic?>();

  // Registered claims
  if (issuer != null) {
    body['iss'] = issuer!;
  }
  if (subject != null) {
    body['sub'] = subject!;
  }
  if (audience != null) {
    body['aud'] = audience!;
  }
  if (expiry != null) {
    body['exp'] = JwtDate.encode(expiry!);
  }
  if (notBefore != null) {
    body['nbf'] = JwtDate.encode(notBefore!);
  }
  if (issuedAt != null) {
    body['iat'] = JwtDate.encode(issuedAt!);
  }
  if (jwtId != null) {
    body['jti'] = jwtId!;
  }

  // Non-registered claims
  _otherClaims.forEach((k, v) {
    assert(!body.containsKey(k));
    try {
      body[k] = splay(v);
    } on FormatException catch (e) {
      throw JsonUnsupportedObjectError('JWT claim: $k (${e.message})');
    }
  });

  // Return result (SplayTreeMap means JSON has the keys in sorted order)

  return body;
}