-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathJsonSerializable.java
More file actions
29 lines (24 loc) · 987 Bytes
/
JsonSerializable.java
File metadata and controls
29 lines (24 loc) · 987 Bytes
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
package com.maxmind.geoip2;
import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.json.JsonMapper;
/**
* Interface for classes that can be serialized to JSON.
* Provides default implementation for toJson() method.
*/
public interface JsonSerializable {
/**
* @return JSON representation of this object. The structure is the same as
* the JSON provided by the GeoIP2 web service.
*/
default String toJson() {
JsonMapper mapper = JsonMapper.builder()
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.addModule(new InetAddressModule())
.changeDefaultPropertyInclusion(
value -> value.withValueInclusion(JsonInclude.Include.NON_NULL)
.withValueInclusion(JsonInclude.Include.NON_EMPTY))
.build();
return mapper.writeValueAsString(this);
}
}