com.majiaxueyuan.util.FastJsonUtil Maven / Gradle / Ivy
The newest version!
package com.majiaxueyuan.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class FastJsonUtil {
private static final SerializeConfig config;
static {
config = new SerializeConfig();
config.put(java.util.Date.class, new JSONLibDataFormatSerializer());
config.put(java.sql.Date.class, new JSONLibDataFormatSerializer());
}
private static final SerializerFeature[] features = { SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.PrettyFormat };
/**
* @param object object
* @return Return:String Description:object to String
*/
public static String toJSONString(Object object) {
return JSON.toJSONString(object, config, features);
}
/**
* @param object object
* @return Return:String Description:user date format
*/
public static String toJSONNoFeatures(Object object) {
return JSON.toJSONString(object, config);
}
/**
* @param jsonStr jsonStr
* @return Return:Object Description:json String to Object
*/
public static JSONObject toJsonObj(String jsonStr) {
return (JSONObject) JSON.parse(jsonStr);
}
/**
* @param jsonStr jsonStr
* @param clazz clazz
* @return Return:T Description:json String to Clazz Object
*/
public static T toBean(String jsonStr, Class clazz) {
return JSON.parseObject(jsonStr, clazz);
}
/**
* @param jsonStr jsonStr
* @return Return:Object[] Description:JSON String to Array
*/
public static Object[] toArray(String jsonStr) {
return toArray(jsonStr, null);
}
/**
* @param jsonStr jsonStr
* @param clazz clazz
* @return Return:Object[] Description:JSON String to Clazz Array
*/
public static Object[] toArray(String jsonStr, Class clazz) {
return JSON.parseArray(jsonStr, clazz).toArray();
}
/**
* @param jsonStr jsonStr
* @param clazz clazz
* @return
*/
public static List toList(String jsonStr, Class clazz) {
return JSON.parseArray(jsonStr, clazz);
}
/**
* Bean to JSONObject
*
* @param bean bean
* @return JSONObject JSONObject
*/
public static JSONObject beanToJsonObj(Object bean) {
String jsonStr = JSON.toJSONString(bean);
JSONObject objectJson = (JSONObject) JSON.parse(jsonStr);
return objectJson;
}
/**
* json String to map
*
* @param jsonStr jsonStr
*/
public static Map, ?> stringToCollect(String jsonStr) {
Map, ?> map = JSONObject.parseObject(jsonStr);
return map;
}
/**
* Map to JSON String
*
* @param map map
*/
public static String collectToString(Map, ?> map) {
String jsonStr = JSONObject.toJSONString(map);
return jsonStr;
}
/**
* @param t clazzObject
* @param file file
* @throws IOException
* Return:void Description:JSON to File
*/
public static void writeJsonToFile(T t, File file) throws IOException {
String jsonStr = JSONObject.toJSONString(t, SerializerFeature.PrettyFormat);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(jsonStr);
bw.close();
}
/**
* @param t clazzObject
* @param filename filename
* @throws IOException
* Return:void Description:Object to File
*/
public static void writeJsonToFile(T t, String filename) throws IOException {
writeJsonToFile(t, new File(filename));
}
/**
* @param cls clazz
* @param file file
* @throws IOException
* Return:T Description:JSON file to Clazz object
*/
public static T readJsonFromFile(Class cls, File file) throws IOException {
StringBuilder strBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
strBuilder.append(line);
}
br.close();
return JSONObject.parseObject(strBuilder.toString(), cls);
}
/**
* @param cls clazz
* @param filename filename
* @throws IOException
* Return:T Description:JSON File to clazz Object
*/
public static T readJsonFromFile(Class cls, String filename) throws IOException {
return readJsonFromFile(cls, new File(filename));
}
/**
* @param typeReference typeReference
* @param file file
* @throws IOException
* Return:T Description:Read JsonObject From Object
*/
public static T readJsonFromFile(TypeReference typeReference, File file) throws IOException {
StringBuilder strBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
strBuilder.append(line);
}
br.close();
return JSONObject.parseObject(strBuilder.toString(), typeReference);
}
/**
* @param typeReference typeReference
* @param filename filename
* @throws IOException
* Return:T Description:Read JsonObject From Object
*/
public static T readJsonFromFile(TypeReference typeReference, String filename) throws IOException {
return readJsonFromFile(typeReference, new File(filename));
}
}