這是在一家工作過四個月的比利時公司時寫的。
當時忙於戶口調動的事兒,僅做了兩個項目,就離開了。
還是很感謝這家公司,戶口的調動除了我的中級職稱,也得到了這家公司老總的全力支援,遺憾沒能給予她更多的回報。
再次感謝謝。
這裡先挖個坑,後續有時間再填!
開始填坑:
package com.dday.dataaccesslayer.utils.helper;import java.io.FileNotFoundException;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Date;import org.json.JSONException;import org.json.JSONObject;import android.content.Context;import com.dday.dataaccesslayer.utils.StorageServiceFactory;import com.google.gson.FieldNamingPolicy;import com.google.gson.Gson;import com.google.gson.GsonBuilder;public class JsonHelper {public static T getEntityFromJson(String fromJsonStr, Class classOfT) throws JSONException {GsonBuilder gsonb = new GsonBuilder();gsonb.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);DateDeserializer ds = new DateDeserializer();gsonb.registerTypeAdapter(Date.class, ds);Gson gson = gsonb.create();JSONObject j = new JSONObject(fromJsonStr);return gson.fromJson(j.toString(), classOfT);}public static T getEntityFromInterStorage(Context context, String jsonFilePath, String charset, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {String jsonContent = InterStorageHelper.readInternal(context, jsonFilePath, charset);return getEntityFromJson(jsonContent, classOfT);}public static T getEntityFromInterStorage(Context context, String jsonFilePath, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {return getEntityFromInterStorage(context, jsonFilePath, "UTF-8", classOfT);}public static T getEntityFromExterPrivateStorage(Context context, String jsonFilePath, String charset, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {String jsonContent = StorageServiceFactory.getSharedInstance().getExterStorageService(context).getPrivateFile(jsonFilePath, charset);return getEntityFromJson(jsonContent, classOfT);}public static T getEntityFromExterPrivateStorage(Context context, String jsonFilePath, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {String jsonContent = StorageServiceFactory.getSharedInstance().getExterStorageService(context).getPrivateFile(jsonFilePath, "UTF-8");return getEntityFromJson(jsonContent, classOfT);}}
此類用到了GSON庫。
其中有一些其它相互關聯類型,這裡不便提供,僅供大家參考其中的邏輯。