MSON,讓JSON序列化更快
問(wèn)題
我們經(jīng)常需要在主線程中讀取一些配置文件或者緩存數(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)題。
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)绾稳バ薷哪兀课夷芟氲降姆椒ň褪潜M量減少反射。
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)方式如下:
- public class Bean {
 - public String key;
 - public String title;
 - public String[] values;
 - public String defaultValue;
 - public static Bean fromJsonString(String json) {
 - try {
 - JSONObject jsonObject = new JSONObject(json);
 - Bean bean = new Bean();
 - bean.key = jsonObject.optString("key");
 - bean.title = jsonObject.optString("title");
 - JSONArray jsonArray = jsonObject.optJSONArray("values");
 - if (jsonArray != null && jsonArray.length() > 0) {
 - int len = jsonArray.length();
 - bean.values = new String[len];
 - for (int i=0; i<len; ++i) {
 - bean.values[i] = jsonArray.getString(i);
 - }
 - }
 - bean.defaultValue = jsonObject.optString("defaultValue");
 - return bean;
 - } catch (JSONException e) {
 - e.printStackTrace();
 - }
 - return null;
 - }
 - public static String toJsonString(Bean bean) {
 - if (bean == null) {
 - return null;
 - }
 - JSONObject jsonObject = new JSONObject();
 - try {
 - jsonObject.put("key", bean.key);
 - jsonObject.put("title", bean.title);
 - if (bean.values != null) {
 - JSONArray array = new JSONArray();
 - for (String str:bean.values) {
 - array.put(str);
 - }
 - jsonObject.put("values", array);
 - }
 - jsonObject.put("defaultValue", bean.defaultValue);
 - } catch (JSONException e) {
 - e.printStackTrace();
 - }
 - return jsonObject.toString();
 - }
 - }
 
測(cè)試代碼:
- private void test() {
 - String a = "{\"key\":\"123\", \"title\":\"asd\", \"values\":[\"a\", \"b\", \"c\", \"d\"], \"defaultValue\":\"a\"}";
 - Gson Gson = new Gson();
 - Bean testBean = Gson.fromJson(a, new TypeToken<Bean>(){}.getType());
 - long now = System.currentTimeMillis();
 - for (int i=0; i<1000; ++i) {
 - Gson.fromJson(a, new TypeToken<Bean>(){}.getType());
 - }
 - Log.d("time", "Gson parse use time="+(System.currentTimeMillis() - now));
 - now = System.currentTimeMillis();
 - for (int i=0; i<1000; ++i) {
 - Bean.fromJsonString(a);
 - }
 - Log.d("time", "jsonobject parse use time="+(System.currentTimeMillis() - now));
 - now = System.currentTimeMillis();
 - for (int i=0; i<1000; ++i) {
 - Gson.toJson(testBean);
 - }
 - Log.d("time", "Gson tojson use time="+(System.currentTimeMillis() - now));
 - now = System.currentTimeMillis();
 - for (int i=0; i<1000; ++i) {
 - Bean.toJsonString(testBean);
 - }
 - 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),用戶只需要調(diào)用這些方法來(lái)完成序列化工作。
我們繼承“AbstractProcessor”,在處理方法中找到有JsonType注解的bean來(lái)處理,代碼如下:
- @Override
 - public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
 - Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(JsonType.class);
 - for (Element element : elements) {
 - if (element instanceof TypeElement) {
 - processTypeElement((TypeElement) element);
 - }
 - }
 - return false;
 - }
 
然后生成對(duì)應(yīng)的序列化方法,關(guān)鍵代碼如下:
- JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(fullClassName);
 - ClassModel classModel = new ClassModel().setModifier("public final").setClassName(simpleClassName);
 - ......
 - JavaFile javaFile = new JavaFile();
 - javaFile.setPackageModel(new PackageModel().setPackageName(packageName))
 - .setImportModel(new ImportModel()
 - .addImport(elementClassName)
 - .addImport("com.meituan.android.MSON.IJsonObject")
 - .addImport("com.meituan.android.MSON.IJsonArray")
 - .addImport("com.meituan.android.MSON.exceptions.JsonParseException")
 - .addImports(extension.getImportList())
 - ).setClassModel(classModel);
 - List<? extends Element> enclosedElements = element.getEnclosedElements();
 - for (Element e : enclosedElements) {
 - if (e.getKind() == ElementKind.FIELD) {
 - processFieldElement(e, extension, toJsonMethodBlock, fromJsonMethodBlock);
 - }
 - }
 - try (Writer writer = sourceFile.openWriter()) {
 - writer.write(javaFile.toSourceString());
 - writer.flush();
 - 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)化我們的速度。
代碼如下:
- Friend object = new Friend();
 - reader.beginObject();
 - while (reader.hasNext()) {
 - String field = reader.nextName();
 - if ("id".equals(field)) {
 - object.id = reader.nextInt();
 - } else if ("name".equals(field)) {
 - if (reader.peek() == JsonToken.NULL) {
 - reader.nextNull();
 - object.name = null;
 - } else {
 - object.name = reader.nextString();
 - }
 - } else {
 - reader.skipValue();
 - }
 - }
 - 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ù)類型上,目前MSON支持了基礎(chǔ)數(shù)據(jù)類型,包裝類型、枚舉、數(shù)組、List、Set、Map、SparseArray以及各種嵌套類型(比如:Map<String, Map<String, List<String[]>>>)。
性能及兼容性對(duì)比
我們使用一個(gè)比較復(fù)雜的bean(包含了各種數(shù)據(jù)類型、嵌套類型)分別測(cè)試了Gson、fastjson和MSON的兼容性和性能。
測(cè)試用例如下:
- @JsonType
 - public class Bean {
 - public Day day;
 - public List<Day> days;
 - public Day[] days1;
 - @JsonField("filed_a")
 - public byte a;
 - public char b;
 - public short c;
 - public int d;
 - public long e;
 - public float f;
 - public double g;
 - public boolean h;
 - @JsonField("filed_a1")
 - public byte[] a1;
 - public char[] b1;
 - public short[] c1;
 - public int[] d1;
 - public long[] e1;
 - public float[] f1;
 - public double[] g1;
 - public boolean[] h1;
 - public Byte a2;
 - public Character b2;
 - public Short c2;
 - public Integer d2;
 - public Long e2;
 - public Float f2;
 - public Double g2;
 - public Boolean h2;
 - @JsonField("name")
 - public String i2;
 - public Byte[] a3;
 - public Character[] b3;
 - public Short[] c3;
 - public Integer[] d3;
 - public Long[] e3;
 - public Float[] f3;
 - public Double[] g3;
 - public Boolean[] h3;
 - public String[] i3;
 - @JsonIgnore
 - public String i4;
 - public transient String i5;
 - public static String i6;
 - public List<String> k;
 - public List<Integer> k1;
 - public Collection<Integer> k2;
 - public ArrayList<Integer> k3;
 - public Set<Integer> k4;
 - public HashSet<Integer> k5;
 - // fastjson 序列化會(huì)崩潰所以忽略掉了,下同
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public List<int[]> k6;
 - public List<String[]> k7;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public List<List<Integer>> k8;
 - @JsonIgnore
 - public List<Map<String, Integer>> k9;
 - @JsonIgnore
 - public Map<String, String> l;
 - public Map<String, List<Integer>> l1;
 - public Map<Long, List<Integer>> l2;
 - public Map<Map<String, String>, String> l3;
 - public Map<String, Map<String, List<String>>> l4;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public SparseArray<SimpleBean2> m1;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public SparseIntArray m2;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public SparseLongArray m3;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public SparseBooleanArray m4;
 - public SimpleBean2 bean;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public SimpleBean2[] bean1;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public List<SimpleBean2> bean2;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public Set<SimpleBean2> bean3;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public List<SimpleBean2[]> bean4;
 - @com.alibaba.fastjson.annotation.JSONField(serialize = false, deserialize = false)
 - public Map<String, SimpleBean2> bean5;
 - }
 
測(cè)試發(fā)現(xiàn)
Gson的兼容性***,能兼容幾乎所有的類型,MSON其次,fastjson對(duì)嵌套類型支持比較弱。
性能方面MSON***,Gson和fastjson相當(dāng)。
測(cè)試結(jié)果如下:
方法數(shù)
MSON本身方法數(shù)很少只有60個(gè),在使用時(shí)會(huì)對(duì)每一個(gè)標(biāo)注了JsonType的Bean生成2個(gè)方法,分別是:
- public String toJson(Bean bean) {...} // 1
 - public Bean fromJson(String data) {...} // 2
 
另外MSON不需要對(duì)任何類做keep處理。
MSON使用方法
下面介紹MSON的使用方法,流程特別簡(jiǎn)單:
1. 在Bean上加注解
- @JsonType
 - public class Bean {
 - public String name;
 - public int age;
 - @JsonField("_desc")
 - public String description; //使用JsonField 標(biāo)注字段在json中的key
 - public transient boolean state; //使用transient 不會(huì)被序列化
 - @JsonIgnore
 - public int state2; //使用JsonIgnore注解 不會(huì)被序列化
 - }
 
2. 在需要序列化的地方
- MSON.fromJson(json, clazz); // 反序列化
 - MSON.toJson(bean); // 序列化
 
結(jié)語(yǔ)
本文介紹了一種高性能的JSON序列化工具M(jìn)SON,以及它的產(chǎn)生原因和實(shí)現(xiàn)原理。目前我們已經(jīng)有好多性能要求比較高的地方在使用,可以大幅的降低JSON的序列化時(shí)間。
【本文為51CTO專欄機(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)】



















 
 
 





 
 
 
 