io.github.kits.json.JsonKit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimthen-kits Show documentation
Show all versions of whimthen-kits Show documentation
Easy to use java tool library.
The newest version!
package io.github.kits.json;
import io.github.kits.interfaces.JsonSupport;
import io.github.kits.json.support.FastJsonSupport;
import io.github.kits.json.support.GsonSupport;
import io.github.kits.json.support.MyJsonSupport;
import java.util.List;
/**
* 自定义Json解析工具类
*
* @project: kits
* @created: with IDEA
* @author: kits
* @date: 2018 08 20 下午2:4636 | 八月. 星期一
*/
public class JsonKit {
private static JsonSupport jsonSupport = new MyJsonSupport();
public static void jsonSupport(JsonSupport support) {
jsonSupport = support;
}
/**
* 将对象转换为json字符串
*
* @param object 资源对象
* @return 转换后的结果
*/
public static String toJson(Object object) {
return jsonSupport.toJson(object);
}
/**
* 将json转换为对象
*
* @param jsonStr json字符串
* @param clazz 转换后的类型
* @param 泛型约束
* @return 转换后的对象
*/
public static T toObject(String jsonStr, Class clazz) {
return (T) jsonSupport.toObject(jsonStr, clazz);
}
/**
* 将json转换为指定类型的对象集合
*
* @param jsonStr json字符串
* @param targetClass 目标类型
* @param 泛型约束
* @return 转换后的对象
*/
public static List toList(String jsonStr, Class targetClass) {
return jsonSupport.toList(jsonStr, targetClass);
}
public static String prettyJson(Object object) {
return jsonSupport.prettyJson(object);
}
}