各JSON技術の比較(Jackson,Gson,Fastjson)の比較
13061 ワード
一、各JSON技術の紹介と優劣
1.json-lib
json-libは最初は最も広く応用されているjson解析ツールでもあり、json-libの悪いところは確かにcommons-beanutilsを含む多くの第三者パッケージに依存している.jar,commons-collections-3.2.jar,commons-lang-2.6.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.JArは、複雑なタイプの変換に対して、json-libがjsonからbeanに変換するには欠陥があります.例えば、あるクラスに別のクラスのlistやmapの集合が現れ、json-libがjsonからbeanへの変換に問題が発生します.json-libは機能と性能の面で現在のインターネット化の需要を満たすことができない.
2.オープンソースのJackson
json-libフレームワークに比べて、Jacksonに依存するjarパッケージは少なく、簡単で使いやすく、性能も比較的高い.また、Jacksonコミュニティは比較的活発で、更新速度も速い.Jacksonは複雑なタイプのjson変換beanに対して問題が発生し,いくつかの集合Map,Listの変換に問題が発生する.Jacksonは複雑なタイプのbean変換Jsonに対して、変換するjsonフォーマットは標準的なJsonフォーマットではありません
3.GoogleのGson
Gsonは現在最も機能的なJson解析神器であり、Gsonは当初、Google社の内部ニーズに対応するためにGoogleが独自に開発したが、2008年5月に第1版が公開されてから多くの会社やユーザーに応用されている.Gsonの応用は主にtoJsonとfromJsonの2つの変換関数であり,依存性がなく,例外的に追加のjarを必要とせず,JDK上で直接走ることができる.このようなオブジェクト変換を使用する前に、JSON文字列を対応するオブジェクトに変換するには、オブジェクトのタイプとそのメンバーを作成する必要があります.クラスにはgetとsetの方法さえあれば、Gsonは複雑なタイプのjsonからbeanまたはbeanからjsonへの変換を完全に行うことができ、JSON解析の神器である.Gsonは機能的には申し分ありませんが、FastJsonより性能が劣っています.
4.アリババのFastJson
FastjsonはJava言語で作成された高性能のJSONプロセッサで、アリババ社が開発した.依存せず、例外的な追加jarは必要なく、JDKを直接走ることができます.FastJsonは複雑なタイプのBean変換Jsonでいくつかの問題が発生し、参照のタイプが発生する可能性があり、Json変換エラーが発生し、参照を作成する必要があります.FastJsonは独自のアルゴリズムを採用し、parseの速度を極致に向上させ、すべてのjsonライブラリを上回った.
4種類のJson技術の比較をまとめると、プロジェクト選択時にGoogleのGsonとアリババのFastJsonの2種類を並行して使用することができ、機能要求のみで性能要求がなければgoogleのGsonを使用することができ、性能上の要求があればGsonを使用してbeanをjsonに変換してデータの正確さを確保し、FastJsonを使用してJsonをBeanに変換することができる
二、GoogleのGsonパッケージの使用概要.
Gsonクラス:jsonを解析する最も基本的なツールクラスJsonParserクラス:JSONからJsonElementsまでの解析ツリーJsonElementクラスを解析する解析器:クラスが表すJSON要素JsonObjectクラス:JSONオブジェクトタイプJsonArrayクラス:JsonObject配列TypeTokenクラス:汎用リストのようなtypeの作成に使用
三、アリババのFastJsonバッグの使用紹介.
三、json-libパッケージの使用概要.
1.json-lib
json-libは最初は最も広く応用されているjson解析ツールでもあり、json-libの悪いところは確かにcommons-beanutilsを含む多くの第三者パッケージに依存している.jar,commons-collections-3.2.jar,commons-lang-2.6.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.JArは、複雑なタイプの変換に対して、json-libがjsonからbeanに変換するには欠陥があります.例えば、あるクラスに別のクラスのlistやmapの集合が現れ、json-libがjsonからbeanへの変換に問題が発生します.json-libは機能と性能の面で現在のインターネット化の需要を満たすことができない.
2.オープンソースのJackson
json-libフレームワークに比べて、Jacksonに依存するjarパッケージは少なく、簡単で使いやすく、性能も比較的高い.また、Jacksonコミュニティは比較的活発で、更新速度も速い.Jacksonは複雑なタイプのjson変換beanに対して問題が発生し,いくつかの集合Map,Listの変換に問題が発生する.Jacksonは複雑なタイプのbean変換Jsonに対して、変換するjsonフォーマットは標準的なJsonフォーマットではありません
3.GoogleのGson
Gsonは現在最も機能的なJson解析神器であり、Gsonは当初、Google社の内部ニーズに対応するためにGoogleが独自に開発したが、2008年5月に第1版が公開されてから多くの会社やユーザーに応用されている.Gsonの応用は主にtoJsonとfromJsonの2つの変換関数であり,依存性がなく,例外的に追加のjarを必要とせず,JDK上で直接走ることができる.このようなオブジェクト変換を使用する前に、JSON文字列を対応するオブジェクトに変換するには、オブジェクトのタイプとそのメンバーを作成する必要があります.クラスにはgetとsetの方法さえあれば、Gsonは複雑なタイプのjsonからbeanまたはbeanからjsonへの変換を完全に行うことができ、JSON解析の神器である.Gsonは機能的には申し分ありませんが、FastJsonより性能が劣っています.
4.アリババのFastJson
FastjsonはJava言語で作成された高性能のJSONプロセッサで、アリババ社が開発した.依存せず、例外的な追加jarは必要なく、JDKを直接走ることができます.FastJsonは複雑なタイプのBean変換Jsonでいくつかの問題が発生し、参照のタイプが発生する可能性があり、Json変換エラーが発生し、参照を作成する必要があります.FastJsonは独自のアルゴリズムを採用し、parseの速度を極致に向上させ、すべてのjsonライブラリを上回った.
4種類のJson技術の比較をまとめると、プロジェクト選択時にGoogleのGsonとアリババのFastJsonの2種類を並行して使用することができ、機能要求のみで性能要求がなければgoogleのGsonを使用することができ、性能上の要求があればGsonを使用してbeanをjsonに変換してデータの正確さを確保し、FastJsonを使用してJsonをBeanに変換することができる
二、GoogleのGsonパッケージの使用概要.
Gsonクラス:jsonを解析する最も基本的なツールクラスJsonParserクラス:JSONからJsonElementsまでの解析ツリーJsonElementクラスを解析する解析器:クラスが表すJSON要素JsonObjectクラス:JSONオブジェクトタイプJsonArrayクラス:JsonObject配列TypeTokenクラス:汎用リストのようなtypeの作成に使用
(1)maven
com.google.code.gson
gson
2.2.4
(2)
public class Book{
private String id;
private String name;
public Book() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student{
private String name;
private int age;
private String sex;
private String describe;
private Set books;
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Set getBooks() {
return books;
}
public void setBooks(Set books) {
this.books = books;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}
(3)bean json
Gson gson = new Gson();
String json = gson.toJson(obj);
obj
(4)json bean
Gson gson = new Gson();
String json = "{\"id\":\"2\",\"name\":\"Json \"}";
Book book = gson.fromJson(json, Book.class);
(5)json bean, List,Set
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() {}.getType());
// json Set
Set set = gson.fromJson(json,new TypeToken() {}.getType());
(6) json json json
a) Json
String json = "[{\"id\":\"1\",\"name\":\"Json \"},{\"id\":\"2\",\"name\":\"java \"}]";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json);
json = gson.toJson(je);
b) json, json
String json = "[{\"id\":\"1\",\"name\":\"Json \"},{\"id\":\"2\",\"name\":\"java \"}]";
boolean jsonFlag;
try {
new JsonParser().parse(str).getAsJsonObject();
jsonFlag = true;
} catch (Exception e) {
jsonFlag = false;
}
c) json
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
String propertyName = 'id';
String propertyValue = "";
try {
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
propertyValue = jsonObj.get(propertyName).toString();
} catch (Exception e) {
propertyValue = null;
}
d) json
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
String propertyName = 'id';
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(propertyName);
json = jsonObj.toString();
e) json
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
String propertyName = 'desc';
Object propertyValue = "json ";
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));
json = jsonObj.toString();
f) json
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
String propertyName = 'name';
Object propertyValue = "json ";
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(propertyName);
jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));
json = jsonObj.toString();
g) json
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
String propertyName = 'name';
boolean isContains = false ;
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
isContains = jsonObj.has(propertyName);
h)json
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Gson gson = builder.create();
gson json , Date ,
i)json Html
Gson gson = new Gson();
Html ,
GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
Gson gson = builder.create();
三、アリババのFastJsonバッグの使用紹介.
(1)maven
com.alibaba
fastjson
1.1.22
(2)
(3)bean json
json
JSON.toJSONString(obj,true);
json
JSON.toJSONString(obj,false);
obj
, 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);
(4)json bean
String json = "{\"id\":\"2\",\"name\":\"Json \"}";
Book book = JSON.parseObject(json, Book.class);
(5)json bean, List,Map
String json = "[{\"id\":\"1\",\"name\":\"Json \"},{\"id\":\"2\",\"name\":\"java \"}]";
// json List
List list = JSON.parseObject(json,new TypeReference(){});
// json Set
Set set = JSON.parseObject(json,new TypeReference(){});
(6) json json
a) json
String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject obj = JSON.parseObject(json);
propertyValue = obj.get(propertyName));
b) json
String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
propertyValue = set.remove(propertyName);
json = obj.toString();
c) json
String propertyName = 'desc';
Object propertyValue = "json ";
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject obj = JSON.parseObject(json);
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();
d) json
String propertyName = 'name';
Object propertyValue = "json ";
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
if(set.contains(propertyName))
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();
e) json
String propertyName = 'name';
boolean isContain = false;
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
isContain = set.contains(propertyName);
f)json
Object obj = new Date();
String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss.SSS");
JSON.toJSONStringWithDateFormat,
三、json-libパッケージの使用概要.
(1)maven
net.sf.json-lib
json-lib
jdk15
2.2.2
commons-beanutils
commons-beanutils
1.8.3
commons-collections
commons-collections
3.2
commons-lang
commons-lang
2.6
commons-logging
commons-logging
1.1.1
net.sf.ezmorph
ezmorph
1.0.6
(2)
(3)bean json
a) Json,obj , List,Map
String json = JSONObject.fromObject(obj).toString();
b) List,Map Json
String json = JSONArray.fromObject(list).toString();
String json = JSONArray.fromObject(map).toString();
(4)json bean
String json = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject jsonObj = JSONObject.fromObject(json);
Book book = (Book)JSONObject.toBean(jsonObj,Book.class);
(5)json List,
String json = "[{\"id\":\"1\",\"name\":\"Json \"},{\"id\":\"2\",\"name\":\"Java \"}]";
JSONArray jsonArray = JSONArray.fromObject(json);
JSONObject jsonObject;
T bean;
int size = jsonArray.size();
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
jsonObject = jsonArray.getJSONObject(i);
bean = (T) JSONObject.toBean(jsonObject, beanClass);
list.add(bean);
}
(6)json Map
String jsonString = "{\"id\":\"1\",\"name\":\"Json \"}";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Iterator keyIter = jsonObject.keys();
String key;
Object value;
Map valueMap = new HashMap();
while (keyIter.hasNext()) {
key = (String) keyIter.next();
value = jsonObject.get(key).toString();
valueMap.put(key, value);
}
(7)json , JsonConfig, Gson FastJson
,
class DateJsonValueProcessor implements JsonValueProcessor{
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
private DateFormat dateFormat;
public DateJsonValueProcessor(String datePattern) {
try {
dateFormat = new SimpleDateFormat(datePattern);
} catch (Exception ex) {
dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
}
}
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}
private Object process(Object value) {
return dateFormat.format[1];
Map birthDays = new HashMap();
birthDays.put("WolfKing",new Date());
JSONObject jsonObject = JSONObject.fromObject(birthDays, jsonConfig);
String json = jsonObject.toString();
System.out.println(json);
}
}
(8)JsonObject json
a) json
String jsonString = "{\"id\":\"1\",\"name\":\"Json \"}";
Object key = "name";
Object value = null;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
value = jsonObject.get(key);
jsonString = jsonObject.toString();
b) json
String jsonString = "{\"id\":\"1\",\"name\":\"Json \"}";
Object key = "name";
Object value = null;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
value = jsonObject.remove(key);
jsonString = jsonObject.toString();
c) json , ,
String jsonString = "{\"id\":\"1\",\"name\":\"Json \"}";
Object key = "desc";
Object value = "json ";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
jsonObject.put(key,value);
jsonString = jsonObject.toString();
d) json
String jsonString = "{\"id\":\"1\",\"name\":\"Json \"}";
boolean containFlag = false;
Object key = "desc";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
containFlag = jsonObject.containsKey(key);