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

com.lindzh.jetcd.JSONUtils Maven / Gradle / Ivy

The newest version!
package com.lindzh.jetcd;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {

	private static Logger logger = Logger.getLogger(JSONUtils.class);
	
	private static ObjectMapper objectMapper;
	static {
		objectMapper = new ObjectMapper();
		objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		objectMapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
	};
	
	public static ObjectMapper getJsonMapper() {
		return objectMapper;
	}

	public static String toJSON(Object obj) {
		try {
			return getJsonMapper().writeValueAsString(obj);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	public static  T fromJSON(String json, Class clz) {
		try {
			return getJsonMapper().readValue(json, clz);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	public static  T fromJSON(String json, TypeReference typeReference) {
		try {
			return getJsonMapper().readValue(json, typeReference);
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy