javascript
MSON,让JSON序列化更快
問題
我們經常需要在主線程中讀取一些配置文件或者緩存數據,最常用的結構化存儲數據的方式就是將對象序列化為JSON字符串保存起來,這種方式特別簡單而且可以和SharedPrefrence配合使用,因此應用廣泛。但是目前用到的Gson在序列化JSON時很慢,在讀取解析這些必要的配置文件時性能不佳,導致卡頓啟動速度減慢等問題。
Gson的問題在哪里呢?筆者用AndroidStudio的profile工具分析了activity.onCreate方法的耗時情況。
如圖1所示,可以發現Gson序列化占用了大部分的執行時間,從圖2可以更直觀地看到Gson.fromJson占用了61%的執行時間。分析Gson的源碼可以發現,它在序列化時大量使用了反射,每一個field,每一個get、set都需要用反射,由此帶來了性能問題。
如何優化
知道了性能的瓶頸之后,我們如何去修改呢?我能想到的方法就是盡量減少反射。
Android框架中由JSONObject來提供輕量級的JSON序列化工具,所以我選擇用Android框架中的JSONObject來做序列化,然后手動復制到bean就可以去掉所有的反射。
我做了個簡單的測試,分別用Gson和JSONObject的方式去序列化一個bean,看下各自速度如何。
使用JSONObject的實現方式如下:
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();} }測試代碼:
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)); }測試結果
| 序列化耗時(ms) | 56 | 9 |
| 反序列化耗時(ms) | 97 | 7 |
執行1000次JSONObject,花費的時間是Gson的幾十分之一。
工具
雖然JSONObject能夠解決我們的問題,但在項目中有大量的存量代碼都使用了Gson序列化,一處處去修改既耗費時間又容易出錯,也不方便增加減少字段。
那么有沒有一種方式在使用時和Gson一樣簡單且性能又特別好呢?
我們調研了Java的AnnotationProcessor(注解處理器),它能夠在編譯前對源碼做處理。我們可以通過使用AnnotationProcessor為帶有特定注解的bean自動生成相應的序列化和反序列化實現,用戶只需要調用這些方法來完成序列化工作。
我們繼承“AbstractProcessor”,在處理方法中找到有JsonType注解的bean來處理,代碼如下:
@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; }然后生成對應的序列化方法,關鍵代碼如下:
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的轉換工具,我們封裝了IJSONObject和IJsonArray,這樣可以接入更高效的JSON解析和格式化工具。
繼續優化
繼續深入測試發現,當JSON數據量比較大時用JSONObject處理會比較慢,究其原因是JSONObject會一次性將字符串讀進來解析成一個map,這樣會有比較大的內存浪費和頻繁內存創建。經過調研Gson內部的實現細節,發現Gson底層有流式的解析器而且可以按需解析,可以做到匹配上的字段才去解析。根據這個發現我們將我們IJSONObject和IJsonArray換成了Gson底層的流解析來進一步優化我們的速度。
代碼如下:
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流解析過程中我們對于不認識的字段直接調用skipValue來節省不必要的時間浪費,而且是一個token接一個token讀文本流這樣內存中不會存一個大的JSON字符串。
兼容性
兼容性主要體現在能支持的數據類型上,目前MSON支持了基礎數據類型,包裝類型、枚舉、數組、List、Set、Map、SparseArray以及各種嵌套類型(比如:Map<String, Map<String, List<String[]>>>)。
性能及兼容性對比
我們使用一個比較復雜的bean(包含了各種數據類型、嵌套類型)分別測試了Gson、fastjson和MSON的兼容性和性能。
測試用例如下:
@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;@JsonIgnorepublic 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 序列化會崩潰所以忽略掉了,下同@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;@JsonIgnorepublic List<Map<String, Integer>> k9;@JsonIgnorepublic 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; }測試發現:
測試結果如下:
| 序列化耗時(ms) | 20 | 47 | 55 |
| 反序列化耗時(ms) | 1 | 20 | 43 |
方法數
MSON本身方法數很少只有60個,在使用時會對每一個標注了JsonType的Bean生成2個方法,分別是:
public String toJson(Bean bean) {...} // 1 public Bean fromJson(String data) {...} // 2另外MSON不需要對任何類做keep處理。
MSON使用方法
下面介紹MSON的使用方法,流程特別簡單:
1. 在Bean上加注解
@JsonType public class Bean {public String name;public int age;@JsonField("_desc")public String description; //使用JsonField 標注字段在json中的keypublic transient boolean state; //使用transient 不會被序列化@JsonIgnorepublic int state2; //使用JsonIgnore注解 不會被序列化}2. 在需要序列化的地方
MSON.fromJson(json, clazz); // 反序列化 MSON.toJson(bean); // 序列化總結
本文介紹了一種高性能的JSON序列化工具MSON,以及它的產生原因和實現原理。目前我們已經有好多性能要求比較高的地方在使用,可以大幅的降低JSON的序列化時間。
招聘信息
美團平臺客戶端技術團隊長期招聘技術專家,有興趣的同學可以發送簡歷到:fangjintao#meituan.com。
詳情請點擊:詳細JD
總結
以上是生活随笔為你收集整理的MSON,让JSON序列化更快的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BAT研发Java面试36题总结:Spr
- 下一篇: gradle idea java ssm