forked from meilisearch/meilisearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJacksonJsonHandler.java
More file actions
88 lines (80 loc) · 3.25 KB
/
JacksonJsonHandler.java
File metadata and controls
88 lines (80 loc) · 3.25 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.meilisearch.sdk.json;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.meilisearch.sdk.exceptions.JsonDecodingException;
import com.meilisearch.sdk.exceptions.JsonEncodingException;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.FilterableAttributesConfig;
import java.io.IOException;
public class JacksonJsonHandler implements JsonHandler {
private final ObjectMapper mapper;
/**
* this constructor uses a default ObjectMapper with enabled 'FAIL_ON_UNKNOWN_PROPERTIES'
* feature.
*/
public JacksonJsonHandler() {
this.mapper = new ObjectMapper();
this.mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
this.mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
registerFilterableAttributesModule(this.mapper);
}
/**
* @param mapper ObjectMapper
*/
public JacksonJsonHandler(ObjectMapper mapper) {
this.mapper = mapper;
registerFilterableAttributesModule(this.mapper);
}
/** {@inheritDoc} */
@Override
public String encode(Object o) throws MeilisearchException {
if (o != null && o.getClass() == String.class) {
return (String) o;
}
try {
this.mapper.setSerializationInclusion(Include.NON_NULL);
return mapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
throw new JsonEncodingException(e);
}
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <T> T decode(Object o, Class<T> targetClass, Class<?>... parameters)
throws MeilisearchException {
if (o == null) {
throw new JsonDecodingException("Response to deserialize is null");
}
if (targetClass == String.class) {
return (T) o;
}
try {
if (parameters == null || parameters.length == 0) {
return mapper.readValue((String) o, targetClass);
} else {
return mapper.readValue(
(String) o,
mapper.getTypeFactory().constructParametricType(targetClass, parameters));
}
} catch (IOException e) {
throw new JsonDecodingException(e);
}
}
private void registerFilterableAttributesModule(ObjectMapper mapper) {
SimpleModule filterableModule = new SimpleModule();
filterableModule.addSerializer(
FilterableAttributesConfig.class,
new JacksonFilterableAttributesConfigSerializer());
filterableModule.addDeserializer(
FilterableAttributesConfig.class,
new JacksonFilterableAttributesConfigDeserializer());
mapper.registerModule(filterableModule);
}
}