com.litongjava.tio.utils.json.FastJson2Utils Maven / Gradle / Ivy
Show all versions of tio-utils Show documentation
package com.litongjava.tio.utils.json;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;
/**
* @author Tong Li
*/
public class FastJson2Utils {
public static String toJson(Object object) {
return JSON.toJSONString(object);
}
public static byte[] toJSONBytes(Object input) {
return JSON.toJSONBytes(input);
}
/**
* 支持传入更多 SerializerFeature
*
* 例如:
* SerializerFeature.WriteMapNullValue 支持对 null 值字段的转换
*/
public static String toJson(Object input, JSONWriter.Feature... features) {
return JSON.toJSONString(input, features);
}
public static byte[] toJSONBytes(Object input, JSONWriter.Feature... features) {
return JSON.toJSONBytes(input, features);
}
public static T parse(String jsonString, Class type) {
return JSON.parseObject(jsonString, type);
}
public static T parse(byte[] input, Class type) {
return JSON.parseObject(input, type);
}
public static JSONObject parseObject(String bodyString) {
return JSON.parseObject(bodyString);
}
public static JSONObject parseObject(byte[] bytes) {
return JSON.parseObject(bytes);
}
public static JSONArray parseArray(String jsonString) {
return JSON.parseArray(jsonString);
}
public static JSONArray parseArray(byte[] input) {
return JSON.parseArray(input);
}
public static List parseArray(String jsonString, Class clazz) {
return JSON.parseArray(jsonString, clazz);
}
public static List parseArray(byte[] input, Class clazz) {
return JSON.parseArray(input, clazz);
}
public static List