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

com.itranswarp.search.JsonUtil Maven / Gradle / Ivy

package com.itranswarp.search;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

class JsonUtil {

	@SuppressWarnings("unchecked")
	public static  Map parseAsMap(String s) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.readValue(s, Map.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String toJson(Object o) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.writeValueAsString(o);
		} catch (JsonProcessingException e) {
			throw new RuntimeException(e);
		}
	}

	public static  T fromJson(Class clazz, String s) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return mapper.readValue(s, clazz);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy