import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
/**
* Json工具类
*
* @author Val
*
*/
public class JsonUtils {
/**
* 将对象转换成Json字符串
*
* @param src
* @return
*/
public static String toJson(Object src) {
return GSON.toJson(src);
}
/**
* 将Json转换成对象
*
* @param json
* @param classOfT
* @return
*/
public static T toEntity(String json, Class classOfT) {
return GSON.fromJson(json, classOfT);
}
/**
* 将Json转化成Map
*
* @param json
* @return
*/
public static Map toMap(String json) {
return GSON.fromJson(json, MAP_TYPE);
}
/**
* 将Json字符串转化成List
*
* @param json
* @param typeOfT
* @return
*/
public static List toList(String json, Class typeOfT) {
List jsonObjectList = GSON.fromJson(json, JSON_OBJECT_TYPE);
List list = new ArrayList<>();
for (JsonObject jsonObject : jsonObjectList) {
list.add(toEntity(jsonObject.toString(), typeOfT));
}
return list;
}
/**
* Json字符串转JsonObject
*
* @param json
* @return
*/
public static JsonObject toJsonObject(String json) {
return JSON_PARSER.parse(json).getAsJsonObject();
}
/**
* 将JsonObject转换成Json字符串
*
* @param src
* @return
*/
public static String toJson(JsonObject jsonObject) {
return jsonObject.toString();
}
/**
* 禁止调用无参构造
*
* @throws Exception
*/
private JsonUtils() throws Exception {
throw new Exception("Error...");
}
private static final Gson GSON = new Gson();
private static final JsonParser JSON_PARSER = new JsonParser();
private static final Type MAP_TYPE = new TypeToken>() {
}.getType();
private static final Type JSON_OBJECT_TYPE = new TypeToken>() {
}.getType();
}
上面是当时孤陋寡闻时写出来的,导致toList和toMap有明显疏漏之处,现补充正确的写法
/**
* 将Json转化成Map
*
* @param json
* @param typeOfMap
* @param typeOfKey
* @param typeOfValue
* @param
* @param
* @param
* @return
*/
public static , K, V> M toMap(String json, Class typeOfMap, Class typeOfKey, Class typeOfValue) {
Type[] argumentTypes = new Type[]{typeOfKey, typeOfValue};
Type parameterizedType = ParameterizedTypeImpl.make(typeOfMap, argumentTypes, null);
return GSON.get().fromJson(json, parameterizedType);
}
/**
* 将json转化成Collection
*
* @param json
* @param typeOfCollection
* @param typeOfT
* @param
* @param
* @return
*/
public static , T> C toCollection(String json, Class typeOfCollection, Class typeOfT) {
Type[] argumentTypes = new Type[]{typeOfT};
Type parameterizedType = ParameterizedTypeImpl.make(typeOfCollection, argumentTypes, null);
return GSON.get().fromJson(json, parameterizedType);
}
主要借助了 Type parameterizedType = ParameterizedTypeImpl.make(typeOfMap, argumentTypes, null);来生成带泛型的Type
当然业务代码中复杂类型还是用TypeToken吧
@Test
public void testTypeToken() {
Map users = new HashMap<>();
users.put("user01", new User("user01", 26, 172.0D));
users.put("user02", new User("user02", 25, 180.0D));
System.out.println("users: " + users);
String json = GsonUtils.toJson(users);
System.out.println("json: " + json);
Map users2 = new Gson().fromJson(json, new TypeToken>() {
}.getType());
System.out.println("users2: " + users2);
Assert.assertEquals(users, users2);
}