com.anaptecs.spring.base.serializers.BookingIDDeserializer Maven / Gradle / Ivy
/*
* anaptecs GmbH, Ricarda-Huch-Str. 71, 72760 Reutlingen, Germany
*
* Copyright 2004 - 2019. All rights reserved.
*/
package com.anaptecs.spring.base.serializers;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.anaptecs.jeaf.core.api.JEAF;
import com.anaptecs.jeaf.json.api.JSONMessages;
import com.anaptecs.jeaf.rest.composite.api.CompositeTypeConverter;
import com.anaptecs.jeaf.rest.composite.api.jeaf.CompositeTypeConverterServiceProvider;
import com.anaptecs.jeaf.xfun.api.errorhandling.JEAFSystemException;
import com.anaptecs.spring.base.BookingCode;
import com.anaptecs.spring.base.BookingID;
import com.anaptecs.spring.base.InventoryType;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
/**
* Class implements a JSON / Jackson deserializer for class BookingID.
*/
public class BookingIDDeserializer extends JsonDeserializer {
/**
* List contains all classes that are involved in the serialization process. This information is required by some
* serialization mechanisms for efficiency and security reasons.
*/
private static final List> SERIALIZED_CLASSES;
static {
List> lClasses = Arrays.asList(BookingID.class, InventoryType.class, BookingCode.class);
SERIALIZED_CLASSES = Collections.unmodifiableList(lClasses);
}
/**
* Deserializer is used to convert a String representation of composite data type BookingID back into an real object.
*/
private CompositeTypeConverter deserializer;
@Override
public BookingID deserialize( JsonParser pParser, DeserializationContext pContext ) throws IOException {
// Parse JSON content.
JsonNode lNode = pParser.getCodec().readTree(pParser);
// We expect that data types are always serialized as plain text which will result in a TextNode.
if (lNode instanceof TextNode) {
// Convert public string representation of BookingID into real object.
return this.getDeserializer().deserializeObject(lNode.asText(), BookingID.class, SERIALIZED_CLASSES);
}
// Node is not a TextNode
else {
throw new JEAFSystemException(JSONMessages.UNEXPECTED_NODE_TYPE_FOR_DESERIALIZATION, BookingID.class.getName(),
lNode.toString(), lNode.getClass().getName());
}
}
/**
* Method returns the {@link CompositeTypeConverter} that should be used to serialize objects.
*
* @return {@link CompositeTypeConverter} Deserializer that should be used. The method never returns null.
*/
private CompositeTypeConverter getDeserializer( ) {
if (deserializer == null) {
deserializer = JEAF.getServiceProvider(CompositeTypeConverterServiceProvider.class);
}
return deserializer;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy