日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MSON,讓JSON序列化更快

 問題

我們經(jīng)常需要在主線程中讀取一些配置文件或者緩存數(shù)據(jù),最常用的結(jié)構(gòu)化存儲(chǔ)數(shù)據(jù)的方式就是將對象序列化為JSON字符串保存起來,這種方式特別簡單而且可以和SharedPrefrence配合使用,因此應(yīng)用廣泛。但是目前用到的Gson在序列化JSON時(shí)很慢,在讀取解析這些必要的配置文件時(shí)性能不佳,導(dǎo)致卡頓啟動(dòng)速度減慢等問題。

Gson的問題在哪里呢?筆者用AndroidStudio的profile工具分析了activity.onCreate方法的耗時(shí)情況。

如圖1,可以發(fā)現(xiàn)Gson序列化占用了大部分的執(zhí)行時(shí)間,從圖2可以更直觀地看到Gson.fromJson占用了61%的執(zhí)行時(shí)間。分析Gson的源碼可以發(fā)現(xiàn),它在序列化時(shí)大量使用了反射,每一個(gè)field,每一個(gè)get、set都需要用反射,由此帶來了性能問題。

如何優(yōu)化

知道了性能的瓶頸之后,我們?nèi)绾稳バ薷哪??我能想到的方法就是盡量減少反射。

Android框架中由JSONObject來提供輕量級的JSON序列化工具,所以我選擇用Android框架中的JSONObject來做序列化,然后手動(dòng)復(fù)制到bean就可以去掉所有的反射。

我做了個(gè)簡單的測試,分別用Gson和JSONObject的方式去序列化一個(gè)bean,看下各自速度如何。

使用JSONObject的實(shí)現(xiàn)方式如下:

 
 
 
 
  1. public class Bean { 
  2.  
  3.     public String key; 
  4.     public String title; 
  5.     public String[] values; 
  6.     public String defaultValue; 
  7.  
  8.     public static Bean fromJsonString(String json) { 
  9.         try { 
  10.             JSONObject jsonObject = new JSONObject(json); 
  11.             Bean bean = new Bean(); 
  12.             bean.key = jsonObject.optString("key"); 
  13.             bean.title = jsonObject.optString("title"); 
  14.             JSONArray jsonArray = jsonObject.optJSONArray("values"); 
  15.             if (jsonArray != null && jsonArray.length() > 0) { 
  16.                 int len = jsonArray.length(); 
  17.                 bean.values = new String[len]; 
  18.                 for (int i=0; i
  19.                     bean.values[i] = jsonArray.getString(i); 
  20.                 } 
  21.             } 
  22.             bean.defaultValue = jsonObject.optString("defaultValue"); 
  23.  
  24.             return bean; 
  25.         } catch (JSONException e) { 
  26.             e.printStackTrace(); 
  27.         } 
  28.  
  29.         return null; 
  30.     } 
  31.  
  32.     public static String toJsonString(Bean bean) { 
  33.         if (bean == null) { 
  34.             return null; 
  35.         } 
  36.         JSONObject jsonObject = new JSONObject(); 
  37.         try { 
  38.             jsonObject.put("key", bean.key); 
  39.             jsonObject.put("title", bean.title); 
  40.             if (bean.values != null) { 
  41.                 JSONArray array = new JSONArray(); 
  42.                 for (String str:bean.values) { 
  43.                     array.put(str); 
  44.                 } 
  45.                 jsonObject.put("values", array); 
  46.             } 
  47.             jsonObject.put("defaultValue", bean.defaultValue); 
  48.         } catch (JSONException e) { 
  49.             e.printStackTrace(); 
  50.         } 
  51.  
  52.         return jsonObject.toString(); 
  53.     } 

測試代碼:

 
 
 
 
  1. private void test() { 
  2.     String a = "{\"key\":\"123\", \"title\":\"asd\", \"values\":[\"a\", \"b\", \"c\", \"d\"], \"defaultValue\":\"a\"}"; 
  3.  
  4.     Gson Gson = new Gson(); 
  5.     Bean testBean = Gson.fromJson(a, new TypeToken(){}.getType()); 
  6.  
  7.     long now = System.currentTimeMillis(); 
  8.     for (int i=0; i<1000; ++i) { 
  9.         Gson.fromJson(a, new TypeToken(){}.getType()); 
  10.     } 
  11.     Log.d("time", "Gson parse use time="+(System.currentTimeMillis() - now)); 
  12.  
  13.     now = System.currentTimeMillis(); 
  14.     for (int i=0; i<1000; ++i) { 
  15.         Bean.fromJsonString(a); 
  16.     } 
  17.     Log.d("time", "jsonobject parse use time="+(System.currentTimeMillis() - now)); 
  18.  
  19.     now = System.currentTimeMillis(); 
  20.     for (int i=0; i<1000; ++i) { 
  21.         Gson.toJson(testBean); 
  22.     } 
  23.     Log.d("time", "Gson tojson use time="+(System.currentTimeMillis() - now)); 
  24.  
  25.     now = System.currentTimeMillis(); 
  26.     for (int i=0; i<1000; ++i) { 
  27.         Bean.toJsonString(testBean); 
  28.     } 
  29.     Log.d("time", "jsonobject tojson use time="+(System.currentTimeMillis() - now)); 

測試結(jié)果

執(zhí)行1000次JSONObject,花費(fèi)的時(shí)間是Gson的幾十分之一。

工具

雖然JSONObject能夠解決我們的問題,但在項(xiàng)目中有大量的存量代碼都使用了Gson序列化,一處處去修改既耗費(fèi)時(shí)間又容易出錯(cuò),也不方便增加減少字段。

那么有沒有一種方式在使用時(shí)和Gson一樣簡單且性能又特別好呢?

我們調(diào)研了Java的AnnotationProcessor(注解處理器),它能夠在編譯前對源碼做處理。我們可以通過使用AnnotationProcessor為帶有特定注解的bean自動(dòng)生成相應(yīng)的序列化和反序列化實(shí)現(xiàn),用戶只需要調(diào)用這些方法來完成序列化工作。

我們繼承“AbstractProcessor”,在處理方法中找到有JsonType注解的bean來處理,代碼如下:

 
 
 
 
  1. @Override 
  2. public boolean process(Set set, RoundEnvironment roundEnvironment) { 
  3.     Set elements = roundEnvironment.getElementsAnnotatedWith(JsonType.class); 
  4.     for (Element element : elements) { 
  5.         if (element instanceof TypeElement) { 
  6.             processTypeElement((TypeElement) element); 
  7.         } 
  8.     } 
  9.     return false; 

然后生成對應(yīng)的序列化方法,關(guān)鍵代碼如下:

 
 
 
 
  1. JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(fullClassName); 
  2. ClassModel classModel = new ClassModel().setModifier("public final").setClassName(simpleClassName); 
  3. ...... 
  4. JavaFile javaFile = new JavaFile(); 
  5. javaFile.setPackageModel(new PackageModel().setPackageName(packageName)) 
  6.         .setImportModel(new ImportModel() 
  7.                 .addImport(elementClassName) 
  8.                 .addImport("com.meituan.android.MSON.IJsonObject") 
  9.                 .addImport("com.meituan.android.MSON.IJsonArray") 
  10.                 .addImport("com.meituan.android.MSON.exceptions.JsonParseException") 
  11.                 .addImports(extension.getImportList()) 
  12.         ).setClassModel(classModel); 
  13.  
  14. List enclosedElements = element.getEnclosedElements(); 
  15. for (Element e : enclosedElements) { 
  16.     if (e.getKind() == ElementKind.FIELD) { 
  17.         processFieldElement(e, extension, toJsonMethodBlock, fromJsonMethodBlock); 
  18.     } 
  19. try (Writer writer = sourceFile.openWriter()) { 
  20.     writer.write(javaFile.toSourceString()); 
  21.     writer.flush(); 
  22.     writer.close(); 

為了今后接入別的字符串和JSONObject的轉(zhuǎn)換工具,我們封裝了IJSONObject和IJsonArray,這樣可以接入更高效的JSON解析和格式化工具。

繼續(xù)優(yōu)化

繼續(xù)深入測試發(fā)現(xiàn),當(dāng)JSON數(shù)據(jù)量比較大時(shí)用JSONObject處理會(huì)比較慢,究其原因是JSONObject會(huì)一次性將字符串讀進(jìn)來解析成一個(gè)map,這樣會(huì)有比較大的內(nèi)存浪費(fèi)和頻繁內(nèi)存創(chuàng)建。經(jīng)過調(diào)研Gson內(nèi)部的實(shí)現(xiàn)細(xì)節(jié),發(fā)現(xiàn)Gson底層有流式的解析器而且可以按需解析,可以做到匹配上的字段才去解析。根據(jù)這個(gè)發(fā)現(xiàn)我們將我們IJSONObject和IJsonArray換成了Gson底層的流解析來進(jìn)一步優(yōu)化我們的速度。

代碼如下:

 
 
 
 
  1. Friend object = new Friend(); 
  2. reader.beginObject(); 
  3. while (reader.hasNext()) { 
  4.     String field = reader.nextName(); 
  5.     if ("id".equals(field)) { 
  6.         object.id = reader.nextInt(); 
  7.     } else if ("name".equals(field)) { 
  8.         if (reader.peek() == JsonToken.NULL) { 
  9.             reader.nextNull(); 
  10.             object.name = null; 
  11.         } else { 
  12.             object.name = reader.nextString(); 
  13.         } 
  14.     } else { 
  15.         reader.skipValue(); 
  16.     } 
  17. reader.endObject(); 

代碼中可以看到,Gson流解析過程中我們對于不認(rèn)識(shí)的字段直接調(diào)用skipValue來節(jié)省不必要的時(shí)間浪費(fèi),而且是一個(gè)token接一個(gè)token讀文本流這樣內(nèi)存中不會(huì)存一個(gè)大的JSON字符串。

兼容性

兼容性主要體現(xiàn)在能支持的數(shù)據(jù)類型上,目前MSON支持了基礎(chǔ)數(shù)據(jù)類型,包裝類型、枚舉、數(shù)組、List、Set、Map、SparseArray以及各種嵌套類型(比如:Map>>)。

性能及兼容性對比

我們使用一個(gè)比較復(fù)雜的bean(包含了各種數(shù)據(jù)類型、嵌套類型)分別測試了Gson、fastjson和MSON的兼容性和性能。

測試用例如下:

 
 
 
 
  1. @JsonType 
  2. public class Bean { 
  3.     public Day day; 
  4.     public List days; 
  5.     public Day[] days1; 
  6.     @JsonField("filed_a") 
  7.     public byte a; 
  8.     public char b; 
  9.     public short c; 
  10.     public int d; 
  11.     public long e; 
  12.     public float f; 
  13.     public double g; 
  14.     public boolean h; 
  15.  
  16.     @JsonField("filed_a1") 
  17.     public byte[] a1; 
  18.     public char[] b1; 
  19.     public short[] c1; 
  20.     public int[] d1; 
  21.     public long[] e1; 
  22.     public float[] f1; 
  23.     public double[] g1; 
  24.     public boolean[] h1; 
  25.  
  26.     public Byte a2; 
  27.     public Character b2; 
  28.     public Short c2; 
  29.     public Integer d2; 
  30.     public Long e2; 
  31.     public Float f2; 
  32.     public Double g2; 
  33.     public Boolean h2; 
  34.     @JsonField("name") 
  35.     public String i2; 
  36.  
  37.     public Byte[] a3; 
  38.     public Character[] b3; 
  39.     public Short[] c3; 
  40.     public Integer[] d3; 
  41.     public Long[] e3; 
  42.     public Float[] f3; 
  43.     public Double[] g3; 
  44.     public Boolean[] h3; 
  45.     public String[] i3; 
  46.  
  47.     @JsonIgnore 
  48.     public String i4; 
  49.     public transient String i5; 
  50.     public static String i6; 
  51.  
  52.     public List k; 
  53.     public List k1; 
  54.     public Collection k2; 
  55.     public ArrayList k3; 
  56.     public Set k4; 
  57.     public HashSet k5; 
  58.     // fastjson 序列化會(huì)崩潰所以忽略掉了,下同 
  59.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  60.     public List k6; 
  61.     public List k7; 
  62.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  63.     public List> k8; 
  64.  
  65.     @JsonIgnore 
  66.     public List> k9; 
  67.     @JsonIgnore 
  68.     public Map l; 
  69.     public Map> l1; 
  70.     public Map> l2; 
  71.     public Map, String> l3; 
  72.     public Map>> l4; 
  73.  
  74.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)  
  75.     public SparseArray m1; 
  76.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  77.     public SparseIntArray m2; 
  78.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  79.     public SparseLongArray m3; 
  80.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  81.     public SparseBooleanArray m4; 
  82.  
  83.     public SimpleBean2 bean; 
  84.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  85.     public SimpleBean2[] bean1; 
  86.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  87.     public List bean2; 
  88.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  89.     public Set bean3; 
  90.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  91.     public List bean4; 
  92.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false) 
  93.     public Map bean5; 

測試發(fā)現(xiàn)

Gson的兼容性***,能兼容幾乎所有的類型,MSON其次,fastjson對嵌套類型支持比較弱。

性能方面MSON***,Gson和fastjson相當(dāng)。

測試結(jié)果如下:

方法數(shù)

MSON本身方法數(shù)很少只有60個(gè),在使用時(shí)會(huì)對每一個(gè)標(biāo)注了JsonType的Bean生成2個(gè)方法,分別是:

 
 
 
 
  1. public String toJson(Bean bean) {...}              // 1 
  2. public Bean fromJson(String data) {...}            // 2 

另外MSON不需要對任何類做keep處理。

MSON使用方法

下面介紹MSON的使用方法,流程特別簡單:

1. 在Bean上加注解

 
 
 
 
  1. @JsonType 
  2. public class Bean { 
  3.  
  4.     public String name; 
  5.     public int age; 
  6.     @JsonField("_desc") 
  7.     public String description;  //使用JsonField 標(biāo)注字段在json中的key 
  8.     public transient boolean state; //使用transient 不會(huì)被序列化 
  9.     @JsonIgnore 
  10.     public int state2; //使用JsonIgnore注解 不會(huì)被序列化 
  11.  

2. 在需要序列化的地方

 
 
 
 
  1. MSON.fromJson(json, clazz); // 反序列化 
  2. MSON.toJson(bean); // 序列化 

結(jié)語

本文介紹了一種高性能的JSON序列化工具M(jìn)SON,以及它的產(chǎn)生原因和實(shí)現(xiàn)原理。目前我們已經(jīng)有好多性能要求比較高的地方在使用,可以大幅的降低JSON的序列化時(shí)間。

【本文為專欄機(jī)構(gòu)“美團(tuán)點(diǎn)評技術(shù)團(tuán)隊(duì)”的原創(chuàng)稿件,轉(zhuǎn)載請通過微信公眾號聯(lián)系機(jī)構(gòu)獲取授權(quán)】


文章標(biāo)題:MSON,讓JSON序列化更快
地址分享:http://m.5511xx.com/article/dhoejph.html