io.evitadb.server.yaml.AbstractClassDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of evita_server Show documentation
Show all versions of evita_server Show documentation
Module contains evitaDB standalone server.
The newest version!
/*
*
* _ _ ____ ____
* _____ _(_) |_ __ _| _ \| __ )
* / _ \ \ / / | __/ _` | | | | _ \
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/FgForrest/evitaDB/blob/master/LICENSE
*
* 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 io.evitadb.server.yaml;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.evitadb.utils.Assert;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Serial;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* This implementation of Jackson {@link JsonDeserializer} allows instantiation and deserialization of abstract class
* concrete implementations by the key used configuration map.
*
* @author Jan Novotný ([email protected]), FG Forrest a.s. (c) 2017
*/
public class AbstractClassDeserializer extends StdDeserializer {
@Serial private static final long serialVersionUID = -5553513445246812598L;
/**
* Simple registry that keeps key string mapping to concrete classes.
*/
private final Map> registry = new HashMap<>(16);
/**
* Initializes deserializer with abstract class information.
*/
public AbstractClassDeserializer(@Nonnull Class abstractClass) {
super(abstractClass);
}
/**
* Registers new concrete class for the specific `keyValue`.
*/
public void registerConcreteClass(String keyValue, Class concreteClass) {
Assert.isTrue(this._valueClass.isAssignableFrom(concreteClass), "The class `" + concreteClass + "` must implement `" + this._valueClass + "`.");
registry.put(keyValue, concreteClass);
}
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
final ObjectMapper mapper = (ObjectMapper) p.getCodec();
final ObjectNode root = mapper.readTree(p);
Class concreteClass = null;
for (Entry> entry : registry.entrySet()) {
if (entry.getKey().equalsIgnoreCase(p.getParsingContext().getCurrentName())) {
concreteClass = entry.getValue();
break;
}
}
if (concreteClass == null) {
return null;
}
return mapper.treeToValue(root, concreteClass);
}
}