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

com.sghd.common.utils.json.JsonUtils Maven / Gradle / Ivy

The newest version!
package com.sghd.common.utils.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.sghd.common.utils.codec.CryptUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * MAP 转换相关的工具类 注意,Map的Key只能为简单类型 ,不可采用复杂类型.
 */
@SuppressWarnings("unchecked")
public final class JsonUtils {

	public static final TypeFactory TYPE_FACTORY = TypeFactory.defaultInstance();

	public static final ObjectMapper MAPPER;

	static {
		MAPPER = new ObjectMapper();
		MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
		MAPPER.enable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS);
		MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		MAPPER.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
		MAPPER.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
		MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		// Long 类型数据映射为字符串
		SimpleModule module = new SimpleModule();
		JsonSerializer longSerializer = new JsonSerializer() {
			public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
					JsonProcessingException {
				jgen.writeString(String.valueOf(value));
			}

		};
		JsonDeserializer longDeserializer = new JsonDeserializer() {
			public Long deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
					JsonProcessingException {
				return Long.valueOf(jp.getValueAsString());
			}
		};
		JsonSerializer bigIntSerializer = new JsonSerializer() {
			public void serialize(BigInteger value, JsonGenerator jgen, SerializerProvider provider)
					throws IOException, JsonProcessingException {
				jgen.writeString(String.valueOf(value));
			}
		};
		JsonSerializer bigDecSerializer = new JsonSerializer() {
			public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider)
					throws IOException, JsonProcessingException {
				jgen.writeString(String.valueOf(value));
			}
		};
		// BITSET
		JsonSerializer bitsetSerializer = new JsonSerializer() {
			public void serialize(BitSet value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
					JsonProcessingException {
				jgen.writeString(CryptUtils.byte2hex(value.toByteArray()));
			}
		};
		JsonDeserializer bitsetDeserializer = new JsonDeserializer() {
			public BitSet deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
					JsonProcessingException {
				return BitSet.valueOf(CryptUtils.hex2byte(jp.getValueAsString().getBytes()));
			}
		};

		module.addSerializer(long.class, longSerializer);
		module.addSerializer(Long.class, longSerializer);
		module.addSerializer(BigInteger.class, bigIntSerializer);
		module.addSerializer(BigDecimal.class, bigDecSerializer);
		module.addSerializer(BitSet.class, bitsetSerializer);

		module.addDeserializer(long.class, longDeserializer);
		module.addDeserializer(Long.class, longDeserializer);
		module.addDeserializer(BitSet.class, bitsetDeserializer);

		MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		MAPPER.registerModule(module);
	}

	private JsonUtils() {
		throw new IllegalAccessError("该类不允许实例化");
	}

	/**
	 * 将对象转换为 MAP 的字符串格式
	 * @param obj 被转换的对象
	 * @return 当参数为空时会返回null
	 */
	public static String object2String(Object obj) {
		if (obj == null) {
			return null;
		}
		StringWriter writer = new StringWriter();
		try {
			MAPPER.writeValue(writer, obj);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.format("将对象[{}]转换为JSON字符串时发生异常", obj, e);
			throw new RuntimeException(message.getMessage(), e);
		}
		return writer.toString();
	}

	/**
	 * 将 MAP 格式的字符串转换为 map
	 * @param json MAP,允许为空
	 * @return json为null时会返回空的Map实例
	 */
	public static Map string2Map(String json) {
		try {
			if (StringUtils.isBlank(json)) {
				return HashMap.class.newInstance();
			}
			JavaType type = TYPE_FACTORY.constructMapType(HashMap.class, String.class, Object.class);
			return MAPPER.readValue(json, type);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.format("将字符串[{}]转换为Map时出现异常", json);
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	/**
	 * 将 MAP 格式的字符串转换为数组
	 * @param 
	 * @param json 字符串
	 * @param clz 数组类型
	 * @return json为null时会返回null
	 */
	public static  T[] string2Array(String json, Class clz) {
		try {
			if (StringUtils.isBlank(json)) {
				return null;
			}
			JavaType type = TYPE_FACTORY.constructArrayType(clz);
			return (T[]) MAPPER.readValue(json, type);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.format("将字符串[{}]转换为数组时出现异常", json, e);
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	/**
	 * 将 MAP 格式的字符串转换为对象
	 * @param 
	 * @param json 字符串
	 * @param clz 对象类型
	 * @return json为null时会返回null
	 */
	public static  T string2Object(String json, Class clz) {
		try {
			if (StringUtils.isBlank(json)) {
				return null;
			}
			JavaType type = TYPE_FACTORY.constructType(clz);
			return (T) MAPPER.readValue(json, type);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.arrayFormat("将字符串[{}]转换为对象[{}]时出现异常",
					new Object[] { json, clz.getSimpleName(), e });
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	/***
	 * json 泛型转换
	 * @param tr 示例 new TypeReference>(){}
	 **/
	public static  T string2GenericObject(String json, TypeReference tr) {
		if (StringUtils.isBlank(json)) {
			return null;
		} else {
			try {
				return (T) MAPPER.readValue(json, tr);
			} catch (Exception e) {
				FormattingTuple message = MessageFormatter.arrayFormat("将字符串[{}]转换为[{}]时出现异常", new Object[] { json, tr });
				throw new RuntimeException(message.getMessage(), e);
			}
		}
	}

	/***
	 * json数组泛型转换
	 * @param tr 示例 new TypeReference>(){}
	 **/
	public static  T bytes2GenericObject(byte[] json, TypeReference tr) {
		if (json == null || json.length == 0) {
			return null;
		} else {
			try {
				return (T) MAPPER.readValue(json, tr);
			} catch (Exception e) {
				FormattingTuple message = MessageFormatter.arrayFormat("将字符串[{}]转换为[{}]时出现异常", new Object[] { json, tr });
				throw new RuntimeException(message.getMessage(), e);
			}
		}
	}
	
	@SuppressWarnings("rawtypes")
	public static  T map2Object(Map map,TypeReference tr) {
		if (map == null) {
			return null;
		} else {
			try {
				
				return (T) MAPPER.convertValue(map, tr);
			} catch (Exception e) {
				FormattingTuple message = MessageFormatter.arrayFormat("将map[{}]转换为[{}]时出现异常", new Object[] { map, tr });
				throw new RuntimeException(message.getMessage(), e);
			}
		}
	}

	/**
	 * 将 MAP 格式的字符串转换为集合
	 * @param 
	 * @param json 字符串
	 * @param collectionType 集合类型
	 * @param elementType 元素类型
	 * @return json为null时会返回空的集合实例
	 */
	public static , E> C string2Collection(String json, Class collectionType,
			Class elementType) {
		try {
			if (StringUtils.isBlank(json)) {
				return collectionType.newInstance();
			}
			JavaType type = TYPE_FACTORY.constructCollectionType(collectionType, elementType);
			return MAPPER.readValue(json, type);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.arrayFormat("将字符串[{}]转换为集合[{}]时出现异常", new Object[] { json, collectionType.getSimpleName()});
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	/**
	 * 将字符串转换为{@link HashMap}对象实例
	 * @param json 被转换的字符串
	 * @param keyType 键类型
	 * @param valueType 值类型
	 * @return json为null时会返回空的HashMap实例
	 */
	public static  Map string2Map(String json, Class keyType, Class valueType) {
		try {
			if (StringUtils.isBlank(json)) {
				return HashMap.class.newInstance();
			}
			JavaType type = TYPE_FACTORY.constructMapType(HashMap.class, keyType, valueType);
			return (Map) MAPPER.readValue(json, type);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.format("将字符串[{}]转换为Map时出现异常", json);
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	/**
	 * 将字符串转换为特定的{@link Map}对象实例
	 * @param json 被转换的字符串
	 * @param keyType 键类型
	 * @param valueType 值类型
	 * @param mapType 指定的{@link Map}类型
	 * @return json为空时会返回空的Map实例
	 */
	public static , K, V> M string2Map(String json, Class keyType, Class valueType,
			Class mapType) {
		try {
			if (StringUtils.isBlank(json)) {
				return mapType.newInstance();
			}
			JavaType type = TYPE_FACTORY.constructMapType(mapType, keyType, valueType);
			return MAPPER.readValue(json, type);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.format("将字符串[{}]转换为Map时出现异常", json);
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	public static Object string2Object(String json, Type type) {
		try {
			if (StringUtils.isBlank(json)) {
				return null;
			}
			JavaType javaType = TYPE_FACTORY.constructType(type);
			return MAPPER.readValue(json, javaType);
		} catch (Exception e) {
			FormattingTuple message = MessageFormatter.arrayFormat("将字符串[{}]转换为[{}]时出现异常", new Object[]{json, type.getClass().getSimpleName(), e});
			throw new RuntimeException(message.getMessage(), e);
		}
	}

	public static JsonNode readTree(String json) throws IOException {
		return MAPPER.readTree(json);
	}

	public static void main(String[] args) {
		String ss = "{playerId:\"11588737\",roomNumber:\"0\",token:\"ab6976b1ab6c6e4b382cd262951cee06\",P:\"D7CE9116AF18214978C09622F302769B04\",platformId:\"10001\",sign:\"SL4vc0FE/TkIHxMkjYzWVNxsB3We/hcYpAAZVmoIrxsLUICSRvrU1JkEzkmDo5YPLUnemH4AL4DZ0Pu+T1mCPkb5XR3sFEAF3DMUD5uBj5c4wvDXDkUeBs+pCR82QKGarPuELPb3ECODXqv2/FnGTtj7lAi0g3PJhGKIm29D24U=\"}";
		Map bd = JsonUtils.string2Map(ss);
		System.out.println(bd);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy