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

it.espr.mvc.json.JsonFinder Maven / Gradle / Ivy

There is a newer version: 0.3.4
Show newest version
package it.espr.mvc.json;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

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

public class JsonFinder {

	private static final Logger log = LoggerFactory.getLogger(JsonFinder.class);

	private static final Map> jsonImpls = new LinkedHashMap<>();

	{
		jsonImpls.put("com.google.gson.Gson", GsonImpl.class);
		jsonImpls.put("com.fasterxml.jackson.databind.ObjectMapper", JacksonImpl.class);
	}

	public Class find() {
		for (Entry> jsonView : jsonImpls.entrySet()) {
			try {
				log.debug("Looking for json view {}", jsonView.getKey());
				Class c = (Class) Class.forName(jsonView.getKey(), false, this.getClass().getClassLoader());
				if (c != null) {
					log.debug("Found json view {}", jsonView.getKey());
					return jsonView.getValue();
				}
			} catch (Exception e) {
				log.debug("Couldn't find a {} json impl on classpath", jsonView.getKey(), e);
			}
		}
		log.info("Couldn't find any of {} json libraries on classpath.", jsonImpls.keySet());
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy