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

com.gccloud.starter.common.json.ExtendObjDeserializer Maven / Gradle / Ivy

package com.gccloud.starter.common.json;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.datatype.jsonorg.JSONObjectDeserializer;
import com.gccloud.starter.common.entity.ExtendObj;
import org.json.JSONObject;
import org.springframework.util.ReflectionUtils;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * 反序列化
 */
public class ExtendObjDeserializer extends JsonDeserializer {

    public static final ExtendObjDeserializer INSTANCE = new ExtendObjDeserializer();

    private static final Field EXTEND_OBJ_MAP_FIELD = ReflectionUtils.findField(ExtendObj.class, "map");

    static {
        ReflectionUtils.makeAccessible(EXTEND_OBJ_MAP_FIELD);
    }

    @Override
    public ExtendObj deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        JSONObject object = JSONObjectDeserializer.instance.deserialize(p, ctxt);
        ExtendObj extendObj = new ExtendObj();
        Object fieldValue = ReflectionUtils.getField(EXTEND_OBJ_MAP_FIELD, object);
        ReflectionUtils.setField(EXTEND_OBJ_MAP_FIELD, extendObj, fieldValue);
        return extendObj;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy