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

step.core.yaml.deserializers.StepYamlDeserializer Maven / Gradle / Ivy

There is a newer version: 3.25.5
Show newest version
/*******************************************************************************
 * Copyright (C) 2020, exense GmbH
 *
 * This file is part of STEP
 *
 * STEP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * STEP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with STEP.  If not, see .
 ******************************************************************************/
package step.core.yaml.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory;
import com.fasterxml.jackson.databind.deser.ResolvableDeserializer;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;

public abstract class StepYamlDeserializer extends JsonDeserializer {

    protected ObjectMapper yamlObjectMapper;

    public StepYamlDeserializer() {
    }

    public StepYamlDeserializer(ObjectMapper yamlObjectMapper){
        this.yamlObjectMapper = yamlObjectMapper;
    }

    protected JsonDeserializer getDefaultDeserializerForClass(JsonParser p, DeserializationContext ctxt, Class clazz) throws IOException {

        DeserializationConfig config = ctxt.getConfig();
        JavaType type = TypeFactory.defaultInstance().constructType(clazz);
        JsonDeserializer defaultDeserializer = BeanDeserializerFactory.instance.buildBeanDeserializer(ctxt, type, config.introspect(type));

        if (defaultDeserializer instanceof ResolvableDeserializer) {
            ((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
        }

        return defaultDeserializer;
    }
}