Gson 与 FastJson 对比

总结

1.在项目选型的时候可以使用Google的Gson和阿里巴巴的FastJson两种并行使用
2.只是功能要求,没有性能要求,可以使用google的Gson
3.有性能上的要求可以使用Gson将bean转换json确保数据的正确,使用FastJson将Json转换Bean

一、Google的Gson

Gson是目前功能最全的Json解析神器,Gson的应用主要为toJsonfromJson两个转换函数,无依赖,不需要例外额外的jar,能够直接跑在JDK上。
而在使用这种对象转换之前需先创建好对象的类型以及其成员才能成功的将JSON字符串成功转换成相对应的对象。
类里面只要有get和set方法,Gson完全可以将复杂类型的json到bean或bean到json的转换,是JSON解析的神器。
Gson在功能上面无可挑剔,但是性能上面比FastJson有所差距。

1. bean转换json:toJson

Gson gson = new Gson();
String json = gson.toJson(obj);

2. json转换bean:fromJson

Gson gson = new Gson();
String json = "{\"id\":\"2\",\"name\":\"Json技术\"}";
Book book = gson.fromJson(json, Book.class);

3. json转换复杂的bean,比如List,Set : fromJson

// 将json转换成复杂类型的bean,需要使用TypeToken
Gson gson = new Gson();
String json = "[{\"id\":\"1\",\"name\":\"Json技术\"},{\"id\":\"2\",\"name\":\"java技术\"}]";
// 将json转换成List
List list = gson.fromJson(json, new TypeToken<List>() {}.getType());
// 将json转换成Set
Set set = gson.fromJson(json, new TypeToken<Set>() {}.getType());

4. 通过json对象直接操作json以及一些json的工具

GsonBuilder gsonBuilder = new GsonBuilder();
ResponseData responseData = gsonBuilder.create().fromJson(result, ResponseData.class);

5.JsonElement

JsonElement可以存JsonObject、JsonArray、JsonPrimitive、JsonNull类型的数据,便于多类型同时管理:

/**
 * A class representing an element of Json. It could either be a {@link JsonObject}, a
 * {@link JsonArray}, a {@link JsonPrimitive} or a {@link JsonNull}.
 *
 * @author Inderjeet Singh
 * @author Joel Leitch
 */
public abstract class JsonElement {
  //......
  public boolean isJsonArray() {
    return this instanceof JsonArray;
  }
 public boolean isJsonObject() {
    return this instanceof JsonObject;
  } 
  public boolean isJsonPrimitive() {
    return this instanceof JsonPrimitive;
  }
  public boolean isJsonNull() {
    return this instanceof JsonNull;
  }
public JsonObject getAsJsonObject() {
    if (isJsonObject()) {
      return (JsonObject) this;
    }
    throw new IllegalStateException("Not a JSON Object: " + this);
  }
public JsonArray getAsJsonArray() {
    if (isJsonArray()) {
      return (JsonArray) this;
    }
    throw new IllegalStateException("Not a JSON Array: " + this);
  }
  //......

二、阿里巴巴的Fastjson

Fastjson是一个Java语言编写的高性能的JSON处理器,由阿里巴巴公司开发。
无依赖,不需要例外额外的jar,能够直接跑在JDK上。
FastJson在复杂类型的Bean转换Json上会出现一些问题,可能会出现引用的类型,导致Json转换出错,需要制定引用。
FastJson采用独创的算法,将parse的速度提升到极致,超过所有json库

1. bean转换json: toJSONString

// 将对象转换成格式化的json
JSON.toJSONString(obj, true);
// 将对象转换成非格式化的json
JSON.toJSONString(obj, false);
// 对于复杂类型的转换,对于重复的引用在转成json串后在json串中出现引用的字符,比如 
//  $ref":"$[0].books[1]
Student stu = new Student();
Set books= new HashSet();
Book book = new Book();
books.add(book);
stu.setBooks(books);
List list = new ArrayList();
for(int i=0;i<5;i++) {
    list.add(stu);
}
String json = JSON.toJSONString(list, true);

2. json转换bean:parseObject

String json = "{\"id\":\"2\",\"name\":\"Json技术\"}";
Book book = JSON.parseObject(json, Book.class);

3. json转换复杂的bean,比如List,Map:parseObject

String json = "[{\"id\":\"1\",\"name\":\"Json技术\"},{\"id\":\"2\",\"name\":\"java技术\"}]";
// 将json转换成List
List list = JSON.parseObject(json, new TypeReference<ARRAYLIST>(){});
// 将json转换成Set
Set set = JSON.parseObject(json, new TypeReference<HASHSET>(){});

4. 通过json对象直接操作json

String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json技术\"}";
JSONObject obj = JSON.parseObject(json);
propertyValue = obj.get(propertyName));

参考的博客:https://blog.csdn.net/sinat_36277898/article/details/95342406


版权声明:本文为HA_LLL原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。