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

com.sportradar.mts.sdk.api.rest.DeserializerJaxbApi Maven / Gradle / Ivy

/*
 * Copyright (C) Sportradar AG. See LICENSE for full license governing this code
 */

package com.sportradar.mts.sdk.api.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBIntrospector;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.io.StringWriter;

/**
 * An implementation of the {@link Deserializer} used to deserialize/unmarshall the content of the Unified API
 * endpoint request
 */
public class DeserializerJaxbApi implements Deserializer {
    private static final Logger logger = LoggerFactory.getLogger(DeserializerJaxbApi.class);
    private final Unmarshaller unmarshaller;
    private final Marshaller marshaller;

    public DeserializerJaxbApi(Unmarshaller unmarshaller, Marshaller marshaller) {
        this.unmarshaller = unmarshaller;
        this.marshaller = marshaller;
    }

    @SuppressWarnings("unchecked")
    @Override
    public synchronized  T deserialize(InputStream inStr, Class clazz){
        try {
            return (T) JAXBIntrospector.getValue(unmarshaller.unmarshal(inStr));
        } catch (JAXBException e) {
            logger.warn("There was a problem unmarshalling an object, ex: ", e);
        }
        return null;
    }

    @Override
    public synchronized  String serialize(T inObj) {
        try {
            StringWriter writer = new StringWriter();
            marshaller.marshal(inObj, writer);
            return  writer.toString();
        } catch (JAXBException e) {
            logger.warn("There was a problem marshaling the provided data, ex: ", e);
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy