FastJSON、Gson和Jackson性能對比

  • 2019 年 10 月 4 日
  • 筆記

前言

Java對象在轉json的時候,如果對象裡面有屬性值為null的話,那麼在json序列化的時候要不要序列出來呢?對比以下json轉換方式

一、fastJson

1、fastJson在轉換java對象為json的時候,默認是不序列化null值對應的key的

也就是說當對象裡面的屬性為空的時候,在轉換成json時,不序列化那些為null值的屬性

具體案例如下:

AutoPartsSearchRequest 有以下屬性:

[java] view plain copy print?

public static void main(String[] args) {          AutoPartsSearchRequest request = new AutoPartsSearchRequest();          request.setKeywords("123");          request.setSortingField("234242");          String str = JSONObject.toJSONString(request);//fastjson默認轉換是不序列化null值對應的key的          System.out.println(str);      }  
  輸出結果:{"keywords":"123","sortingField":"234242"}  , 沒有序列化那些值為null的屬性

2、但是如果想把null對應的key序列化出來呢?

那就要仔細看看fastjson轉換java對象為json的時候的入參了:也就是這個方法:

JSONObject.toJSONString(Object object, SerializerFeature... features)

Fastjson的SerializerFeature序列化屬性:

[plain] view plain copy print?

QuoteFieldNames———-輸出key時是否使用雙引號,默認為true  WriteMapNullValue——–是否輸出值為null的欄位,默認為false  WriteNullNumberAsZero—-數值欄位如果為null,輸出為0,而非null  WriteNullListAsEmpty—–List欄位如果為null,輸出為[],而非null  WriteNullStringAsEmpty—字元類型欄位如果為null,輸出為」「,而非null  WriteNullBooleanAsFalse–Boolean欄位如果為null,輸出為false,而非null  

結合上面,SerializerFeature... features是個數組,那麼我們可以傳入我們想要的參數,比如想序列化null,案例如下:

[java] view plain copy print?

public static void main(String[] args) {          AutoPartsSearchRequest request = new AutoPartsSearchRequest();          request.setKeywords("123");          request.setSortingField("234242");          String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue);          System.out.println(str);      }  

輸出結果如下:

3、想字元類型欄位如果為null,轉換輸出為」「,而非null ,需要多加一個參數:WriteNullStringAsEmpty, 案例如下:

[java] view plain copy print?

public static void main(String[] args) {          AutoPartsSearchRequest request = new AutoPartsSearchRequest();          request.setKeywords("123");          request.setSortingField("234242");          String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue,                  SerializerFeature.WriteNullStringAsEmpty);          System.out.println(str);      }  

輸出結果如下:

二、Jackson

1、jackson默認是序列化null對應的key的,也就是說不管你對象屬性有沒有值,在轉換json的時候都會被序列化出來

[java] view plain copy print?

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {          AutoPartsSearchRequest request = new AutoPartsSearchRequest();          request.setKeywords("123");          request.setSortingField("234242");          ObjectMapper mapper = new ObjectMapper();          String str = mapper.writeValueAsString(request);          System.out.println(str);   //輸出結果(此處就不格式化了):{"sortingField":"234242","partsClassifyId":null,"partsSubClassifyId":null,"sortingDirection":null:......      }  

2、同理,想要不序列化null也是可以的,具體如下:

[java] view plain copy print?

1.實體上    @JsonInclude(Include.NON_NULL)    //將該標記放在屬性上,如果該屬性為NULL則不參與序列化  //如果放在類上邊,那對這個類的全部屬性起作用  //Include.Include.ALWAYS 默認  //Include.NON_DEFAULT 屬性為默認值不序列化  //Include.NON_EMPTY 屬性為 空(「」) 或者為 NULL 都不序列化  //Include.NON_NULL 屬性為NULL 不序列化      2.程式碼上  ObjectMapper mapper = new ObjectMapper();    mapper.setSerializationInclusion(Include.NON_NULL);    //通過該方法對mapper對象進行設置,所有序列化的對象都將按改規則進行系列化  //Include.Include.ALWAYS 默認  //Include.NON_DEFAULT 屬性為默認值不序列化  //Include.NON_EMPTY 屬性為 空(「」) 或者為 NULL 都不序列化  //Include.NON_NULL 屬性為NULL 不序列化  

注意:只對VO起作用,Map List不起作用,另外jackson還能過濾掉你設置的屬性,具體的就各位自己去研究源碼了

或者參照:jackson詳解

三、Gson

1、gson和fastjson一樣,默認是不序列化null值對應的key的,具體案例如下:

[java] view plain copy print?

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {          AutoPartsSearchRequest request = new AutoPartsSearchRequest();          request.setKeywords("123");          request.setSortingField("234242");          Gson g = new GsonBuilder().create();          String str = g.toJson(request);          System.out.println(str);   //輸出結果:{"sortingField":"234242","keywords":"123"}      }  

2、若是想序列化null值對應的key,只需要將以上創建程式碼改成以下程式碼就行:

[java] view plain copy print?

Gson g = new GsonBuilder().serializeNulls().create();  

案例就不寫了

3、若是想轉行null為空字元串"",則需要手動處理了

具體參考:gson轉換null為空字元串

Tags: