Created
December 18, 2016 07:19
-
-
Save ueno-yuhei/8fe3e3d47e7179c804ea4f31ab8057b0 to your computer and use it in GitHub Desktop.
Serialize出来ない型への対応を考える! ref: http://qiita.com/ueno-yuhei/items/930535cf3cf627b84934
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Instance> list; | |
// ↓ | |
ArrayList<Instance> list; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String, String> map; | |
// ↓ | |
HashMap<String, String> map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jsonの元の文字列を保持しておいて、受け取り先のActivity,Fragmentで再生成 | |
JSONObject json = new JSONObject("jsonString"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JsonObject json = new Gson().fromJson("jsonString", JsonObject.class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
date = dateFormat.parse(dateString); | |
} catch(ParseException e) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Uri uri = Uri.parse("URL"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String jsonString; | |
public transient JsonObject json; | |
public String uriString; | |
public transient Uri uri; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void generate() { | |
JsonObject json = new Gson().fromJson(jsonString, JsonObject.class); | |
uri = Uri.parse(uriString); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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