|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package ru.olegcherednik.jackson.utils; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| 22 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 23 | +import com.fasterxml.jackson.annotation.PropertyAccessor; |
| 24 | +import com.fasterxml.jackson.core.JsonParser; |
| 25 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 28 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 29 | +import com.fasterxml.jackson.module.afterburner.AfterburnerModule; |
| 30 | +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; |
| 31 | +import ru.olegcherednik.jackson.utils.enumid.EnumIdModule; |
| 32 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsDateSerializer; |
| 33 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsInstantSerializer; |
| 34 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsLocalDateTimeSerializer; |
| 35 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsLocalTimeSerializer; |
| 36 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsOffsetDateTimeSerializer; |
| 37 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsOffsetTimeSerializer; |
| 38 | +import ru.olegcherednik.jackson.utils.serializers.JacksonUtilsZonedDateTimeSerializer; |
| 39 | + |
| 40 | +import java.time.Instant; |
| 41 | +import java.time.LocalDateTime; |
| 42 | +import java.time.LocalTime; |
| 43 | +import java.time.OffsetDateTime; |
| 44 | +import java.time.OffsetTime; |
| 45 | +import java.time.ZoneId; |
| 46 | +import java.time.ZoneOffset; |
| 47 | +import java.time.ZonedDateTime; |
| 48 | +import java.util.Date; |
| 49 | +import java.util.Optional; |
| 50 | +import java.util.function.Supplier; |
| 51 | +import java.util.function.UnaryOperator; |
| 52 | + |
| 53 | +/** |
| 54 | + * @author Oleg Cherednik |
| 55 | + * @since 02.01.2021 |
| 56 | + */ |
| 57 | +public class JacksonObjectMapperSupplier implements Supplier<ObjectMapper> { |
| 58 | + |
| 59 | + public static final UnaryOperator<ZoneId> ZONE_MODIFIER_USE_ORIGINAL = zoneId -> zoneId; |
| 60 | + public static final UnaryOperator<ZoneId> ZONE_MODIFIER_TO_UTC = zoneId -> ZoneOffset.UTC; |
| 61 | + |
| 62 | + protected final UnaryOperator<ZoneId> zoneModifier; |
| 63 | + protected final boolean useMilliseconds; |
| 64 | + |
| 65 | + public static JacksonObjectMapperSupplier.Builder builder() { |
| 66 | + return new Builder(); |
| 67 | + } |
| 68 | + |
| 69 | + protected JacksonObjectMapperSupplier(Builder builder) { |
| 70 | + zoneModifier = builder.zoneModifier; |
| 71 | + useMilliseconds = builder.useMilliseconds; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public ObjectMapper get() { |
| 76 | + ObjectMapper mapper = new ObjectMapper(); |
| 77 | + config(mapper); |
| 78 | + registerModule(mapper); |
| 79 | + return mapper; |
| 80 | + } |
| 81 | + |
| 82 | + protected ObjectMapper config(ObjectMapper mapper) { |
| 83 | + return mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE) |
| 84 | + .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) |
| 85 | + |
| 86 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL) |
| 87 | + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) |
| 88 | + |
| 89 | + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
| 90 | + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) |
| 91 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 92 | + .disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) |
| 93 | + |
| 94 | + .enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) |
| 95 | + .enable(JsonParser.Feature.ALLOW_COMMENTS) |
| 96 | + .enable(JsonParser.Feature.ALLOW_YAML_COMMENTS); |
| 97 | + } |
| 98 | + |
| 99 | + protected ObjectMapper registerModule(ObjectMapper mapper) { |
| 100 | + JacksonUtilsInstantSerializer instantSerializer = |
| 101 | + new JacksonUtilsInstantSerializer(zoneModifier, useMilliseconds); |
| 102 | + JacksonUtilsLocalDateTimeSerializer localDateTimeSerializer = |
| 103 | + new JacksonUtilsLocalDateTimeSerializer(useMilliseconds); |
| 104 | + JacksonUtilsLocalTimeSerializer localTimeSerializer = new JacksonUtilsLocalTimeSerializer(useMilliseconds); |
| 105 | + JacksonUtilsOffsetDateTimeSerializer offsetDateTimeSerializer = |
| 106 | + new JacksonUtilsOffsetDateTimeSerializer(zoneModifier, useMilliseconds); |
| 107 | + JacksonUtilsOffsetTimeSerializer offsetTimeSerializer = |
| 108 | + new JacksonUtilsOffsetTimeSerializer(zoneModifier, useMilliseconds); |
| 109 | + JacksonUtilsZonedDateTimeSerializer dateTimeSerializer = |
| 110 | + new JacksonUtilsZonedDateTimeSerializer(zoneModifier, useMilliseconds); |
| 111 | + JacksonUtilsDateSerializer dateSerializer = new JacksonUtilsDateSerializer(instantSerializer); |
| 112 | + |
| 113 | + return mapper.registerModule(new ParameterNamesModule()) |
| 114 | + .registerModule(new AfterburnerModule()) |
| 115 | + .registerModule(new EnumIdModule()) |
| 116 | + .registerModule(new JavaTimeModule().addSerializer(Instant.class, instantSerializer) |
| 117 | + .addSerializer(LocalDateTime.class, localDateTimeSerializer) |
| 118 | + .addSerializer(LocalTime.class, localTimeSerializer) |
| 119 | + .addSerializer(OffsetDateTime.class, offsetDateTimeSerializer) |
| 120 | + .addSerializer(OffsetTime.class, offsetTimeSerializer) |
| 121 | + .addSerializer(ZonedDateTime.class, dateTimeSerializer) |
| 122 | + .addSerializer(Date.class, dateSerializer) |
| 123 | + .addKeySerializer(Instant.class, instantSerializer) |
| 124 | + .addKeySerializer(LocalDateTime.class, localDateTimeSerializer) |
| 125 | + .addKeySerializer(LocalTime.class, localTimeSerializer) |
| 126 | + .addKeySerializer(OffsetDateTime.class, offsetDateTimeSerializer) |
| 127 | + .addKeySerializer(OffsetTime.class, offsetTimeSerializer) |
| 128 | + .addKeySerializer(ZonedDateTime.class, dateTimeSerializer) |
| 129 | + .addKeySerializer(Date.class, dateSerializer)); |
| 130 | + } |
| 131 | + |
| 132 | + public static class Builder { |
| 133 | + |
| 134 | + private UnaryOperator<ZoneId> zoneModifier = ZONE_MODIFIER_TO_UTC; |
| 135 | + private boolean useMilliseconds = true; |
| 136 | + |
| 137 | + protected Builder() { |
| 138 | + } |
| 139 | + |
| 140 | + public Builder zone(ZoneId zone) { |
| 141 | + return zoneModifier(z -> zone); |
| 142 | + } |
| 143 | + |
| 144 | + public Builder zoneModifier(UnaryOperator<ZoneId> zoneModifier) { |
| 145 | + this.zoneModifier = Optional.ofNullable(zoneModifier).orElse(ZONE_MODIFIER_TO_UTC); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public Builder withUseMilliseconds(boolean useMilliseconds) { |
| 150 | + this.useMilliseconds = useMilliseconds; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + public JacksonObjectMapperSupplier build() { |
| 155 | + return new JacksonObjectMapperSupplier(this); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments