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

com.arangodb.serde.jackson.JacksonMapperProvider Maven / Gradle / Ivy

There is a newer version: 7.16.0
Show newest version
package com.arangodb.serde.jackson;

import com.arangodb.ArangoDBException;
import com.arangodb.ContentType;
import com.arangodb.internal.serde.JacksonUtils;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

/**
 * Not shaded in arangodb-java-driver-shaded.
 */
public class JacksonMapperProvider {
    private static final Logger LOG = LoggerFactory.getLogger(JacksonMapperProvider.class);

    public static ObjectMapper of(final ContentType contentType) {
        String formatName;
        if (contentType == ContentType.JSON) {
            formatName = "JSON";
        } else if (contentType == ContentType.VPACK) {
            formatName = "Velocypack";
        } else {
            throw new IllegalArgumentException("Unexpected value: " + contentType);
        }

        ServiceLoader sl = ServiceLoader.load(JsonFactory.class);
        Iterator iterator = sl.iterator();
        while (iterator.hasNext()) {
            JsonFactory jf;
            try {
                jf = iterator.next();
            } catch (ServiceConfigurationError e) {
                LOG.warn("ServiceLoader failed to load JsonFactory", e);
                continue;
            }
            if (formatName.equals(jf.getFormatName())) {
                if (contentType == ContentType.JSON) {
                    JacksonUtils.tryConfigureJsonFactory(jf);
                }
                return new ObjectMapper(jf);
            }
            LOG.debug("Required format ({}) not supported by JsonFactory: {}", formatName, jf.getClass().getName());
        }

        throw new ArangoDBException("No JsonFactory found for content type: " + contentType);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy