-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathObjectMapperSingleton.java
More file actions
46 lines (41 loc) · 1.55 KB
/
ObjectMapperSingleton.java
File metadata and controls
46 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dev.toonformat.jtoon.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.module.afterburner.AfterburnerModule;
import java.util.TimeZone;
/**
* Provides a singleton ObjectMapper instance.
*/
public final class ObjectMapperSingleton {
/**
* Holds the singleton ObjectMapper.
*/
private static volatile ObjectMapper INSTANCE;
private ObjectMapperSingleton() {
throw new UnsupportedOperationException("Utility class cannot be instantiated");
}
/**
* Returns the singleton ObjectMapper.
*
* @return ObjectMapper
*/
public static ObjectMapper getInstance() {
ObjectMapper result = INSTANCE;
if (result == null) {
synchronized (ObjectMapperSingleton.class) {
result = INSTANCE;
if (result == null) {
INSTANCE = result = JsonMapper.builder()
.changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.ALWAYS))
.addModule(new AfterburnerModule()) // Speeds up Jackson by 20–40% in most real-world cases
.defaultTimeZone(TimeZone.getTimeZone("UTC")) // set a default timezone for dates
.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
.build();
}
}
}
return result;
}
}