package com.camilstaps.common; import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.InvocationTargetException; import java.text.ParseException; /** * Created by camilstaps on 11-4-15. */ public class DatedString { protected final Date date; protected final String string; public DatedString(Date date, String string) { this.date = date; this.string = string; } public String getString() { return string; } public Date getDate() { return date; } @Override public String toString() { JSONObject json = new JSONObject(); try { json.put("date", date.toString()); json.put("text", string); } catch (JSONException e) {} return json.toString(); } public static Object fromString(String s, Class castTo) throws JSONException, ParseException { JSONObject json = new JSONObject(s); try { return castTo.cast(castTo.getDeclaredConstructor(Date.class, String.class).newInstance(new Date(json.getString("date")), json.getString("text"))); } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } catch (InvocationTargetException e) { return null; } catch (NoSuchMethodException e) { return null; } } }