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

cicada.core.JsonUtil Maven / Gradle / Ivy

package cicada.core;

import java.io.IOException;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author user
 */
public class JsonUtil
{

	static ObjectMapper mapper = new ObjectMapper();

	public static  List String2List(String json, Class obj) throws JsonParseException, JsonMappingException, IOException
	{
		JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, obj);
		List result = mapper.readValue(json, javaType);
		return result;
	}

	public static  T String2Obj(String json, Class obj) throws JsonParseException, JsonMappingException, IOException
	{
		// T result=new Main().jsonConverObject(josn3, User.class); 对象里有
		// 集合 转换
		T result = mapper.readValue(json, obj);
		return result;
	}

	public static  String obj2string(T obj) throws JsonProcessingException
	{
		if (obj == null)
		{
			return "";
		}
		String result = mapper.writeValueAsString(obj);
		return result;
	}

	public static  byte[] obj2byte(T obj) throws JsonProcessingException
	{
		if (obj == null)
		{
			return null;
		}
		byte[] result = mapper.writeValueAsBytes(obj);
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy