All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.basistech.util.jackson.EnumModule Maven / Gradle / Ivy

There is a newer version: 38.0.3
Show newest version
/*
* Copyright 2014 Basis Technology Corp.
*
* 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 com.basistech.util.jackson;

import com.basistech.util.LanguageCode;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.BeanProperty;
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.KeyDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.ContextualKeyDeserializer;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.deser.impl.CreatorCollector;
import com.fasterxml.jackson.databind.deser.std.MapDeserializer;
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.fasterxml.jackson.databind.module.SimpleKeyDeserializers;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
import com.fasterxml.jackson.databind.type.MapType;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Module for common enum types when not embedded in the rest of the ADM.
 * This provides specialized serialization when the enum constants themselves
 * are not desirable.  For example, {@code LanguageCode} is usually
 * deserialized from "ARABIC", but this module deserializes it from "ara"
 * instead.
 */
public class EnumModule extends SimpleModule {
    /**
     * This is a version of the MapDeserializer that  works around
     * https://github.com/FasterXML/jackson-databind/issues/944 -- failure to use
     * the correct deserializer when deserializing a map where there is a customized
     * deserializer for the key. The code in createContextual is a copy of the
     * method sitting in the Jackson repo teed up for 2.6.3. We can get rid of this when
     * that is released. */
    private static class PatchedMapDeserializer extends MapDeserializer {

        PatchedMapDeserializer(JavaType mapType, ValueInstantiator valueInstantiator, KeyDeserializer keyDeser, JsonDeserializer valueDeser, TypeDeserializer valueTypeDeser) {
            super(mapType, valueInstantiator, keyDeser, valueDeser, valueTypeDeser);
        }

        @Override
        public JsonDeserializer createContextual(DeserializationContext ctxt,
                                                    BeanProperty property) throws JsonMappingException {
            KeyDeserializer kd = _keyDeserializer;
            if (kd == null) {
                if (_mapType.getKeyType().getRawClass() == LanguageCode.class) {
                    kd = new LanguageCodeKeyDeserializer();
                } else {
                    kd = ctxt.findKeyDeserializer(_mapType.getKeyType(), property);
                }
            } else {
                if (kd instanceof ContextualKeyDeserializer) {
                    kd = ((ContextualKeyDeserializer) kd).createContextual(ctxt, property);
                }
            }
            JsonDeserializer vd = _valueDeserializer;
            // #125: May have a content converter
            if (property != null) {
                vd = findConvertingContentDeserializer(ctxt, property, vd);
            }
            final JavaType vt = _mapType.getContentType();
            if (vd == null) {
                vd = ctxt.findContextualValueDeserializer(vt, property);
            } else { // if directly assigned, probably not yet contextual, so:
                vd = ctxt.handleSecondaryContextualization(vd, property, vt);
            }
            TypeDeserializer vtd = _valueTypeDeserializer;
            if (vtd != null) {
                vtd = vtd.forProperty(property);
            }
            HashSet ignored = _ignorableProperties;
            AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
            if (intr != null && property != null) {
                AnnotatedMember member = property.getMember();
                if (member != null) {
                    String[] moreToIgnore = intr.findPropertiesToIgnore(member, false);
                    if (moreToIgnore != null) {
                        ignored = (ignored == null) ? new HashSet() : new HashSet(ignored);
                        for (String str : moreToIgnore) {
                            ignored.add(str);
                        }
                    }
                }
            }
            return withResolved(kd, vtd, vd, ignored);
        }
    }

    /**
     * This plugs in the above.
     */
    @SuppressWarnings("unchecked")
    private class CustomDeserializers extends SimpleDeserializers {
        @Override
        public JsonDeserializer findMapDeserializer(MapType type,
                                                       DeserializationConfig config,
                                                       BeanDescription beanDesc,
                                                       KeyDeserializer keyDeserializer,
                                                       TypeDeserializer elementTypeDeserializer,
                                                       JsonDeserializer elementDeserializer) throws JsonMappingException {
            if (type.getKeyType().getRawClass() == LanguageCode.class) {
                JavaType linkedHashMap = config.getTypeFactory().constructSpecializedType(type, LinkedHashMap.class);
                beanDesc = config.introspectForCreation(linkedHashMap);

                List ctors = beanDesc.getConstructors();
                CreatorCollector creators =  new CreatorCollector(beanDesc, false);
                for (AnnotatedConstructor ctor : ctors) {
                    if (ctor.getParameterCount() == 1 && ctor.getRawParameterType(0) == int.class) {
                        creators.addIntCreator(ctor, false);
                    }
                }
                AnnotatedConstructor defaultCtor = beanDesc.findDefaultConstructor();
                creators.setDefaultCreator(defaultCtor);
                ValueInstantiator valueInstantiator = creators.constructValueInstantiator(config);
                return new PatchedMapDeserializer(linkedHashMap, valueInstantiator, keyDeserializer, (JsonDeserializer) elementDeserializer, elementTypeDeserializer);
            } else {
                return null;
            }
        }
    }

    public EnumModule() {
        super(ModuleVersion.VERSION);
    }

    public void setupModule(SetupContext context) {
        context.setMixInAnnotations(LanguageCode.class, LanguageCodeMixin.class);
        SimpleSerializers keySerializers = new SimpleSerializers();
        keySerializers.addSerializer(new LanguageCodeKeySerializer());
        keySerializers.addSerializer(Object.class, new DynamicKeySerializer());
        context.addKeySerializers(keySerializers);
        SimpleKeyDeserializers keyDeserializers = new SimpleKeyDeserializers();
        keyDeserializers.addDeserializer(LanguageCode.class, new LanguageCodeKeyDeserializer());
        context.addDeserializers(new CustomDeserializers());
    }

    /**
     * Register a Jackson module for Rosette's top-level enums an {@link ObjectMapper}.
     * @param mapper the mapper.
     * @return the same mapper, for convenience.
     */
    public static ObjectMapper setupObjectMapper(ObjectMapper mapper) {
        final EnumModule module = new EnumModule();
        mapper.registerModule(module);
        return mapper;
    }
}