Skip to content

Commit a787e2d

Browse files
authored
Thread-safe classes should be Shared statically
Thread-safe classes should be Shared statically,This improves performance and reduces class creation costs.
1 parent 91ff7fc commit a787e2d

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎lib/src/main/java/com/auth0/jwt/JWTCreator.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@ public final class JWTCreator {
3030
private final Algorithm algorithm;
3131
private final String headerJson;
3232
private final String payloadJson;
33+
34+
private static final ObjectMapper mapper;
35+
private static final SimpleModule module;
36+
37+
static {
38+
mapper = new ObjectMapper();
39+
module = new SimpleModule();
40+
module.addSerializer(ClaimsHolder.class, new PayloadSerializer());
41+
mapper.registerModule(module);
42+
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
43+
}
3344

3445
private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<String, Object> payloadClaims) throws JWTCreationException {
3546
this.algorithm = algorithm;
3647
try {
37-
ObjectMapper mapper = new ObjectMapper();
38-
SimpleModule module = new SimpleModule();
39-
module.addSerializer(ClaimsHolder.class, new PayloadSerializer());
40-
mapper.registerModule(module);
41-
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
4248
headerJson = mapper.writeValueAsString(headerClaims);
4349
payloadJson = mapper.writeValueAsString(new ClaimsHolder(payloadClaims));
4450
} catch (JsonProcessingException e) {

0 commit comments

Comments
 (0)