mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 08:58:38 +01:00
Adding a uniform configuration for all json/yaml mapper use cases: part 1
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2020 Signal Messenger, LLC
|
||||
* Copyright 2013 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
@@ -7,28 +7,55 @@ package org.whispersystems.textsecuregcm.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
|
||||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.vdurmont.semver4j.Semver;
|
||||
import java.io.IOException;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class SystemMapper {
|
||||
|
||||
private static final ObjectMapper MAPPER = build();
|
||||
private static final ObjectMapper JSON_MAPPER = configureMapper(new ObjectMapper());
|
||||
|
||||
private static final ObjectMapper YAML_MAPPER = configureMapper(new YAMLMapper());
|
||||
|
||||
|
||||
@Nonnull
|
||||
public static ObjectMapper getMapper() {
|
||||
return MAPPER;
|
||||
public static ObjectMapper jsonMapper() {
|
||||
return JSON_MAPPER;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private static ObjectMapper build() {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
|
||||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper;
|
||||
public static ObjectMapper yamlMapper() {
|
||||
return YAML_MAPPER;
|
||||
}
|
||||
|
||||
public static ObjectMapper configureMapper(final ObjectMapper mapper) {
|
||||
return mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
|
||||
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
|
||||
.registerModules(
|
||||
applicationModule(),
|
||||
new JavaTimeModule(),
|
||||
new Jdk8Module());
|
||||
}
|
||||
|
||||
private static Module applicationModule() {
|
||||
return new SimpleModule()
|
||||
.addDeserializer(Semver.class, new JsonDeserializer<>() {
|
||||
@Override
|
||||
public Semver deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
|
||||
final String strValue = p.readValueAs(String.class);
|
||||
return strValue != null ? new Semver(strValue) : null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user