自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MSON,讓JSON序列化更快

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

 問(wèn)題

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

[[217432]]

Gson的問(wèn)題在哪里呢?筆者用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都需要用反射,由此帶來(lái)了性能問(wèn)題。

如何優(yōu)化

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

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

我做了個(gè)簡(jiǎn)單的測(cè)試,分別用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<len; ++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.     } 

測(cè)試代碼:

  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<Bean>(){}.getType()); 
  6.  
  7.     long now = System.currentTimeMillis(); 
  8.     for (int i=0; i<1000; ++i) { 
  9.         Gson.fromJson(a, new TypeToken<Bean>(){}.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)); 

測(cè)試結(jié)果

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

工具

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

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

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

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

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

然后生成對(duì)應(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<? extends Element> 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ù)深入測(cè)試發(fā)現(xiàn),當(dāng)JSON數(shù)據(jù)量比較大時(shí)用JSONObject處理會(huì)比較慢,究其原因是JSONObject會(huì)一次性將字符串讀進(jìn)來(lái)解析成一個(gè)map,這樣會(huì)有比較大的內(nèi)存浪費(fèi)和頻繁內(nèi)存創(chuàng)建。經(jīng)過(guò)調(diào)研Gson內(nèi)部的實(shí)現(xiàn)細(xì)節(jié),發(fā)現(xiàn)Gson底層有流式的解析器而且可以按需解析,可以做到匹配上的字段才去解析。根據(jù)這個(gè)發(fā)現(xiàn)我們將我們IJSONObject和IJsonArray換成了Gson底層的流解析來(lái)進(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流解析過(guò)程中我們對(duì)于不認(rèn)識(shí)的字段直接調(diào)用skipValue來(lái)節(jié)省不必要的時(shí)間浪費(fèi),而且是一個(gè)token接一個(gè)token讀文本流這樣內(nèi)存中不會(huì)存一個(gè)大的JSON字符串。

兼容性

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

性能及兼容性對(duì)比

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

測(cè)試用例如下:

  1. @JsonType 
  2. public class Bean { 
  3.     public Day day
  4.     public List<Day> 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<String> k; 
  53.     public List<Integer> k1; 
  54.     public Collection<Integer> k2; 
  55.     public ArrayList<Integer> k3; 
  56.     public Set<Integer> k4; 
  57.     public HashSet<Integer> k5; 
  58.     // fastjson 序列化會(huì)崩潰所以忽略掉了,下同 
  59.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false
  60.     public List<int[]> k6; 
  61.     public List<String[]> k7; 
  62.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false
  63.     public List<List<Integer>> k8; 
  64.  
  65.     @JsonIgnore 
  66.     public List<Map<String, Integer>> k9; 
  67.     @JsonIgnore 
  68.     public Map<String, String> l; 
  69.     public Map<String, List<Integer>> l1; 
  70.     public Map<Long, List<Integer>> l2; 
  71.     public Map<Map<String, String>, String> l3; 
  72.     public Map<String, Map<String, List<String>>> l4; 
  73.  
  74.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)  
  75.     public SparseArray<SimpleBean2> 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<SimpleBean2> bean2; 
  88.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false
  89.     public Set<SimpleBean2> bean3; 
  90.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false
  91.     public List<SimpleBean2[]> bean4; 
  92.     @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false
  93.     public Map<String, SimpleBean2> bean5; 

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

Gson的兼容性***,能兼容幾乎所有的類(lèi)型,MSON其次,fastjson對(duì)嵌套類(lèi)型支持比較弱。

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

測(cè)試結(jié)果如下:

方法數(shù)

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

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

另外MSON不需要對(duì)任何類(lèi)做keep處理。

MSON使用方法

下面介紹MSON的使用方法,流程特別簡(jiǎn)單:

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é)語(yǔ)

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

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

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來(lái)源: 51CTO專(zhuān)欄
相關(guān)推薦

2013-03-11 13:55:03

JavaJSON

2021-11-18 07:39:41

Json 序列化Vue

2009-07-29 13:39:02

JSON序列化和反序列ASP.NET AJA

2022-08-06 08:41:18

序列化反序列化Hessian

2016-10-20 15:54:08

Python數(shù)據(jù)序列化

2009-08-24 17:14:08

C#序列化

2024-01-30 13:32:51

JSON反序列化序列化

2011-06-01 15:05:02

序列化反序列化

2024-10-07 08:26:53

2023-12-13 13:49:52

Python序列化模塊

2009-08-06 11:16:25

C#序列化和反序列化

2011-05-18 15:20:13

XML

2018-03-19 10:20:23

Java序列化反序列化

2024-04-28 08:56:58

大數(shù)據(jù)框架Json

2009-06-14 22:01:27

Java對(duì)象序列化反序列化

2019-11-20 10:07:23

web安全PHP序列化反序列化

2009-08-25 14:24:36

C#序列化和反序列化

2011-06-01 14:50:48

2010-01-08 11:11:38

JSON 序列化

2011-06-01 14:26:11

序列化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)