io.leopard.json.JsonFastJsonImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leopard-json Show documentation
Show all versions of leopard-json Show documentation
规范、简化Json操作,可选Jackson、FastJson实现。并可在TopNB查看耗时统计信息。
package io.leopard.json;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class JsonFastJsonImpl implements IJson {
public JsonFastJsonImpl() {
JSON.toJSON(null);// 当fastjson包没有引入时,让初始化失败.
}
@Override
public String toJson(Object obj) {
return JSON.toJSONString(obj);
}
@Override
public String toFormatJson(Object obj) {
return JSON.toJSONString(obj, true);
}
@Override
public T toObject(String json, Class clazz) {
return JSON.parseObject(json, clazz);
}
@Override
public T toObject(String json, Class clazz, boolean ignoreUnknownField) {
// TODO ignoreUnknownField未实现
return JSON.parseObject(json, clazz);
}
@Override
public List toListObject(String json, Class clazz) {
return JSON.parseArray(json, clazz);
}
@Override
public List toListObject(String json, Class clazz, boolean ignoreUnknownField) {
return JSON.parseArray(json, clazz);
}
@Override
public List toListObject(List jsonList, Class clazz) {
if (jsonList == null || jsonList.isEmpty()) {
return null;
}
List list = new ArrayList();
for (String json : jsonList) {
list.add(this.toObject(json, clazz));
}
return list;
}
@Override
public List toListObject(List jsonList, Class clazz, boolean ignoreUnknownField) {
if (jsonList == null || jsonList.isEmpty()) {
return null;
}
List list = new ArrayList();
for (String json : jsonList) {
list.add(this.toObject(json, clazz, ignoreUnknownField));
}
return list;
}
@SuppressWarnings("unchecked")
@Override
public Map toMap(String json) {
return toObject(json, Map.class);
}
}