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

com.cherokeesoft.db.utils.serialise.MapForUpdateDeserializer Maven / Gradle / Ivy

There is a newer version: 1.8.29
Show newest version
package com.cherokeesoft.db.utils.serialise;

import com.cherokeesoft.db.utils.update.MapSerialisable;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Deprecated
public class MapForUpdateDeserializer extends JsonDeserializer implements ContextualDeserializer {
    private JavaType type;

    public MapForUpdateDeserializer() {
    }

    public MapForUpdateDeserializer(JavaType type) {
        this.type = type;
    }

    @Override
    public MapSerialisable deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        try {
            HashMap hashMap = deserializationContext.readValue(jsonParser, HashMap.class);
            Class rawClass = (Class) type.getRawClass();
            MapSerialisable result = rawClass.newInstance();
            doFillPresentProperties(result, hashMap, (ObjectMapper) jsonParser.getCodec());
            return result;
        } catch (Exception r) {
            throw new RuntimeException(r);
        }
    }

    private void doFillPresentProperties(MapSerialisable requestMapSerialised, HashMap hashMap, ObjectMapper codec) {
        try {
            Field[] declaredFields = requestMapSerialised.getClass().getDeclaredFields();
            for(Field field: declaredFields) {
                String name = field.getName();
                Type genericType = field.getGenericType();

                if(hashMap.containsKey(name)) {
                    Object val = hashMap.get(name);
                    requestMapSerialised.getRawMapRequest().put(name, val);
                    field.setAccessible(true);
                    field.set(requestMapSerialised, codec.readValue(codec.writeValueAsBytes(val), new TypeReference() {
                        @Override
                        public Type getType() {
                            return genericType;
                        }
                    }));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JsonDeserializer createContextual(DeserializationContext deserializationContext, BeanProperty beanProperty) throws JsonMappingException {
        JavaType type = Optional.ofNullable(deserializationContext.getContextualType()).orElse(beanProperty.getMember().getType());
        return new MapForUpdateDeserializer(type);
    }
}