Skip to content

Instantly share code, notes, and snippets.

@ueno-yuhei
Created December 18, 2016 07:19
Show Gist options
  • Save ueno-yuhei/8fe3e3d47e7179c804ea4f31ab8057b0 to your computer and use it in GitHub Desktop.
Save ueno-yuhei/8fe3e3d47e7179c804ea4f31ab8057b0 to your computer and use it in GitHub Desktop.
Serialize出来ない型への対応を考える! ref: http://qiita.com/ueno-yuhei/items/930535cf3cf627b84934
List<Instance> list;
// ↓
ArrayList<Instance> list;
Map<String, String> map;
// ↓
HashMap<String, String> map;
// jsonの元の文字列を保持しておいて、受け取り先のActivity,Fragmentで再生成
JSONObject json = new JSONObject("jsonString");
JsonObject json = new Gson().fromJson("jsonString", JsonObject.class);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
date = dateFormat.parse(dateString);
} catch(ParseException e) {
}
Uri uri = Uri.parse("URL");
public String jsonString;
public transient JsonObject json;
public String uriString;
public transient Uri uri;
public void generate() {
JsonObject json = new Gson().fromJson(jsonString, JsonObject.class);
uri = Uri.parse(uriString);
}
import android.net.Uri;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class SampleModel implements Serializable {
private static final long serialVersionUID = 4226701820259487692L;
// public Map map;
public HashMap map;
// public List list;
public ArrayList list;
public transient Date date;
public String dateString;
public transient Uri uri;
public String uriString;
public transient JSONObject jsonObject;
public String jsonObjectString;
public transient JsonObject gsonObject;
public String gsonObjectString;
public SampleModel(JSONObject json) throws JSONException {
dateString = json.getString("date");
uriString = json.getString("uri");
jsonObjectString = json.getJSONObject("json_object").toString();
gsonObjectString = json.getJSONObject("json_object").toString();
generate();
}
// Activityなどに渡した後に、このメソッドを呼ぶ
public void generate() throws JSONException {
try {
// Date
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
date = dateFormat.parse(dateString);
// Uri
uri = Uri.parse(uriString);
// jsoo
jsonObject = new JSONObject(jsonObjectString);
// gson
gsonObject = new Gson().fromJson(gsonObjectString, JsonObject.class);
} catch (ParseException e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment