org.immutables.criteria.mongo.bson4jackson.JacksonCodecs Maven / Gradle / Ivy
/*
* Copyright 2019 Immutables Authors and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.immutables.criteria.mongo.bson4jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.databind.type.MapType;
import com.google.common.annotations.Beta;
import org.bson.BsonReader;
import org.bson.BsonValue;
import org.bson.BsonWriter;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecConfigurationException;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Utility class to convert to / from {@link CodecRegistry} and {@link ObjectMapper}.
*
* Please note that Jackson support is currently experimental.
*/
@Beta
public final class JacksonCodecs {
/**
* Selects native BSON codec for ser/deser
*/
private static final Predicate IS_BSON_TYPE = type -> {
Class> raw = type.getRawClass();
return BsonValue.class.isAssignableFrom(raw) || Document.class.isAssignableFrom(raw) || Bson.class.isAssignableFrom(raw);
};
/**
* Create {@link CodecRegistry} adapter on the top of existing mapper instance
*/
public static CodecRegistry registryFromMapper(final ObjectMapper mapper) {
Objects.requireNonNull(mapper, "mapper");
return CodecRegistries.fromRegistries(new NativeCodecRegistry(mapper), JacksonCodecRegistry.of(mapper));
}
private static Codec findCodecOrNull(CodecRegistry registry, Class type) {
try {
return registry.get(type);
} catch (CodecConfigurationException e) {
return null;
}
}
private static JsonSerializer serializer(final Codec codec) {
Objects.requireNonNull(codec, "codec");
return new CodecSerializer<>(codec);
}
private static JsonDeserializer deserializer(final Codec codec) {
Objects.requireNonNull(codec, "codec");
return new JsonDeserializer() {
@Override
public T deserialize(JsonParser parser, DeserializationContext ctxt) {
@SuppressWarnings("unchecked")
final BsonReader reader = ((Wrapper) parser).unwrap();
return codec.decode(reader, DecoderContext.builder().build());
}
};
}
static Serializers serializers(final CodecRegistry registry) {
Objects.requireNonNull(registry, "registry");
return new Serializers.Base() {
@Override
public JsonSerializer> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
try {
Codec> codec = registry.get(type.getRawClass());
return serializer(codec);
} catch (CodecConfigurationException e) {
return null;
}
}
@Override
public JsonSerializer> findMapSerializer(SerializationConfig config, MapType type, BeanDescription beanDesc, JsonSerializer