包含JodaTime对象为JSON序列化的自定义对象 [英] Serializing custom object that contains JodaTime objects into JSON
问题描述
运行用于创建一个对象,并将其存储到第二活动后的的ArrayList
内部的辛格尔顿
当我从创立返回到第一活动
收到此错误活动
。
logcat的:
10-09 15:00:48.125:E / AndroidRuntime(4266):致命异常:主要
10-09 15:00:48.125:E / AndroidRuntime(4266):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.nanospark.cnc / com.nanospark.cnc.ContactCreateActivity}:java.lang.RuntimeException的:失败调用公共org.joda.time.Chronology()无参数
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.os.Handler.dispatchMessage(Handler.java:99)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.os.Looper.loop(Looper.java:137)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.ActivityThread.main(ActivityThread.java:5103)
10-09 15:00:48.125:E / AndroidRuntime(4266):在java.lang.reflect.Method.invokeNative(本机方法)
10-09 15:00:48.125:E / AndroidRuntime(4266):在java.lang.reflect.Method.invoke(Method.java:525)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 15:00:48.125:E / AndroidRuntime(4266):在dalvik.system.NativeStart.main(本机方法)
10-09 15:00:48.125:E / AndroidRuntime(4266):java.lang.RuntimeException的:产生的原因未能调用公共org.joda.time.Chronology()无参数
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.ConstructorConstructor $ 3.construct(ConstructorConstructor.java:107)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:173)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.Gson.fromJson(Gson.java:805)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.Gson.fromJson(Gson.java:770)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.Gson.fromJson(Gson.java:719)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.nanospark.cnc.GlobalData.retrieveGlobalDataFromStorage(GlobalData.java:92)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.nanospark.cnc.ContactCreateActivity.onCreate(ContactCreateActivity.java:38)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.Activity.performCreate(Activity.java:5133)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-09 15:00:48.125:E / AndroidRuntime(4266):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-09 15:00:48.125:E / AndroidRuntime(4266):11 ...更多
10-09 15:00:48.125:E / AndroidRuntime(4266):java.lang.InstantiationException:产生的原因无法实例化类org.joda.time.Chronology
10-09 15:00:48.125:E / AndroidRuntime(4266):在java.lang.reflect.Constructor.constructNative(本机方法)
10-09 15:00:48.125:E / AndroidRuntime(4266):在java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-09 15:00:48.125:E / AndroidRuntime(4266):在com.google.gson.internal.ConstructorConstructor $ 3.construct(ConstructorConstructor.java:104)
10-09 15:00:48.125:E / AndroidRuntime(4266):27 ...更多
辛格尔顿code做的JSON写:
公共无效storeGlobalData(上下文的背景下){
米preFS = context.getShared preferences(com.nanospark.cnc,Context.MODE_PRIVATE);
preferenceEditor = M prefs.edit();
GSON GSON =新GSON();
//变换的ArrayList成JSON数据。
串machineProfileListJSON = gson.toJson(getMachineProfileList());
串contactInfoListJSON = gson.toJson(getContactInfoList());
串eventInfoListJSON = gson.toJson(getEventInfoList());
preferenceEditor.putString(machineProfileListJSONData,machineProfileListJSON);
preferenceEditor.putString(contactInfoListJSONData,contactInfoListJSON);
preferenceEditor.putString(eventInfoListJSONData,eventInfoListJSON);
//提交更改。
preferenceEditor.commit();
}
公共无效retrieveGlobalDataFromStorage(上下文的背景下){
米preFS = context.getShared preferences(com.nanospark.cnc,Context.MODE_PRIVATE);
如果(M prefs.contains(machineProfileListJSONData)){
GSON GSON =新GSON();
字符串machineProfileListJSON = M prefs.getString(machineProfileListJSONData,);
字符串contactInfoListJSON = M prefs.getString(contactInfoListJSONData,);
字符串eventInfoListJSON = M prefs.getString(eventInfoListJSONData,);
键入machineProfileListType =新TypeToken&其中; ArrayList的&其中; MachineProfile>>(){} .getType();
machineProfileList =(gson.fromJson(machineProfileListJSON,machineProfileListType));
键入contactInfoListType =新TypeToken&其中; ArrayList的&其中; CONTACTINFO>>(){} .getType();
contactInfoList = gson.fromJson(contactInfoListJSON,contactInfoListType);
键入eventListType =新TypeToken< ArrayList的<事件>>(){} .getType();
eventInfoList = gson.fromJson(eventInfoListJSON,eventListType);
}
}
报告事件类正试图这样写:
包com.nanospark.cnc;
进口的java.util.ArrayList;
进口org.joda.time.LocalDate;
进口org.joda.time.LocalTime;
公共类ReportEvent扩展事件{
公共ReportEvent(字符串eventTitle,字符串eventDescription,INT assignedPin,串trackingType,
LocalDate的startDate,LocalDate结束日期,布尔watchAllDay,
本地时间的startTime,本地时间endTime的,ArrayList的<字符串> selectedDays,
ArrayList的< CONTACTINFO> selectedContact,串formatChoice,
ReportTimeSpec reportTimingConfig){
超(eventTitle,eventDescription,assignedPin,trackingType,的startDate,结束日期,watchAllDay,
的startTime,endTime的,selectedDays,selectedContact,formatChoice,
reportTimingConfig);
// TODO自动生成构造函数存根
}
}
一般事件类报告事件延伸:
包com.nanospark.cnc;
进口的java.util.ArrayList;
进口的java.util.Calendar;
进口org.joda.time.LocalDate;
进口org.joda.time.LocalTime;
公共类事件{
字符串eventTitle;
字符串eventDescription;
INT assignedPin;
字符串trackingType;
LocalDate的startDate;
LocalDate结束日期;
布尔watchAllDay;
本地时间的startTime;
本地时间endTime的;
ArrayList的<字符串> selectedDays =新的ArrayList<字符串>();
ArrayList的< CONTACTINFO> selectedContact =新的ArrayList< CONTACTINFO>();
字符串formatChoice;
ReportTimeSpec reportTimingConfigs;
公共事件(){
}
公共事件(字符串eventTitle,字符串eventDescription,INT assignedPin,串trackingType,
LocalDate的startDate,LocalDate结束日期,布尔watchAllDay,
本地时间的startTime,本地时间endTime的,ArrayList的<字符串> selectedDays,
ArrayList的< CONTACTINFO> selectedContact,串formatChoice,ReportTimeSpec reportTimingConfigs){
this.eventTitle = eventTitle;
this.eventDescription = eventDescription;
this.assignedPin = assignedPin;
this.trackingType = trackingType;
this.startDate =的startDate;
this.endDate =结束日期;
this.watchAllDay = watchAllDay;
this.startTime = startTime时;
this.endTime = endTime的;
this.selectedDays = selectedDays;
this.selectedContact = selectedContact;
this.formatChoice = formatChoice;
this.reportTimingConfigs = reportTimingConfigs;
}
公共字符串getEventTitle(){
返回eventTitle;
}
公共无效setEventTitle(字符串eventTitle){
this.eventTitle = eventTitle;
}
公共字符串getEventDescription(){
返回eventDescription;
}
公共无效setEventDescription(字符串eventDescription){
this.eventDescription = eventDescription;
}
公共ReportTimeSpec getReportTimingConfigs(){
返回reportTimingConfigs;
}
公共无效setReportTimingConfigs(ReportTimeSpec reportTimingConfigs){
this.reportTimingConfigs = reportTimingConfigs;
}
公众诠释getAssignedPin(){
返回assignedPin;
}
公共无效setAssignedPin(INT assignedPin){
this.assignedPin = assignedPin;
}
公共字符串getTrackingType(){
返回trackingType;
}
公共无效setTrackingType(字符串trackingType){
this.trackingType = trackingType;
}
公共LocalDate getStartDate(){
返回的startDate;
}
公共无效setStartDate(LocalDate的startDate){
this.startDate =的startDate;
}
公共LocalDate getEndDate(){
返回结束日期;
}
公共无效setEndDate(LocalDate结束日期){
this.endDate =结束日期;
}
公共布尔isWatchAllDay(){
返回watchAllDay;
}
公共无效setWatchAllDay(布尔watchAllDay){
this.watchAllDay = watchAllDay;
}
公共本地时间getStartTime(){
返回的startTime;
}
公共无效setStartTime(本地时间的startTime){
this.startTime = startTime时;
}
公共本地时间getEndTime(){
返回endTime的;
}
公共无效setEndTime(本地时间endTime的){
this.endTime = endTime的;
}
公众的ArrayList<字符串> getSelectedDays(){
返回selectedDays;
}
公共无效setSelectedDays(ArrayList中<字符串> selectedDays){
this.selectedDays = selectedDays;
}
公众的ArrayList< CONTACTINFO> getSelectedContact(){
返回selectedContact;
}
公共无效setSelectedContact(ArrayList中< CONTACTINFO> selectedContact){
this.selectedContact = selectedContact;
}
公共字符串getFormatChoice(){
返回formatChoice;
}
公共无效setFormatChoice(字符串formatChoice){
this.formatChoice = formatChoice;
}
公共ReportTimeSpec getReportTimeSpec(){
返回reportTimingConfigs;
}
公共无效setReportTimeSpec(ReportTimeSpec reportTimingConfigs){
this.reportTimingConfigs = reportTimingConfigs;
}
@覆盖
公共字符串的toString(){
返回this.eventTitle;
}
}
从https://github.com/gkopff/gson-javatime-serialisers对于 java.time 从Java8类,如: <一href="http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html">java.time.LocalDate.
但你需要转换器 org.joda.time 从第三部分库类。你可以为它创建自定义的转换器,如:
转换为 org.joda.time.LocalDate
公共类LocalDateSerializer实现JsonDeserializer&LT; LocalDate&gt;中JsonSerializer&LT; LocalDate&GT;
{
私有静态最后DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.date();
@覆盖
公共LocalDate反序列化(最终JsonElement济,最终类型的类型,
最后JsonDeserializationContext JDC)抛出JsonParseException
{
最后弦乐dateAsString = je.getAsString();
如果(dateAsString.length()== 0)
{
返回null;
}
其他
{
返回DATE_FORMAT.parseLocalDate(dateAsString);
}
}
@覆盖
公共JsonElement连载(最终LocalDate SRC,最终类型typeOfSrc,
最后JsonSerializationContext上下文)
{
字符串retVal的;
如果(SRC == NULL)
{
retVal的=;
}
其他
{
retVal的= DATE_FORMAT.print(SRC);
}
返回新JsonPrimitive(retVal的);
}
}
转换为 org.joda.time.LocalTime
公共类LocalTimeSerializer实现JsonDeserializer&LT;本地时间&gt;中JsonSerializer&LT;本地时间&GT;
{
私有静态最后DateTimeFormatter TIME_FORMAT = ISODateTimeFormat.timeNoMillis();
@覆盖
公共本地时间反序列化(最终JsonElement济,最终类型的类型,
最后JsonDeserializationContext JDC)抛出JsonParseException
{
最后弦乐dateAsString = je.getAsString();
如果(dateAsString.length()== 0)
{
返回null;
}
其他
{
返回TIME_FORMAT.parseLocalTime(dateAsString);
}
}
@覆盖
公共JsonElement连载(最终本地时间SRC,最终类型typeOfSrc,
最后JsonSerializationContext上下文)
{
字符串retVal的;
如果(SRC == NULL)
{
retVal的=;
}
其他
{
retVal的= TIME_FORMAT.print(SRC);
}
返回新JsonPrimitive(retVal的);
}
}
用法
公共无效retrieveGlobalDataFromStorage(上下文的背景下)
{
// ...
最后GsonBuilder建设者=新GsonBuilder()
.registerTypeAdapter(LocalDate.class,新LocalDateSerializer())
.registerTypeAdapter(LocalTime.class,新LocalTimeSerializer());
最后GSON GSON = builder.create();
// ...
After running a second activity that creates an object and stores it into an ArrayList
inside of a Singleton
I get this error when returning to the first Activity
from the creation Activity
.
Logcat:
10-09 15:00:48.125: E/AndroidRuntime(4266): FATAL EXCEPTION: main
10-09 15:00:48.125: E/AndroidRuntime(4266): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanospark.cnc/com.nanospark.cnc.ContactCreateActivity}: java.lang.RuntimeException: Failed to invoke public org.joda.time.Chronology() with no args
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.os.Looper.loop(Looper.java:137)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-09 15:00:48.125: E/AndroidRuntime(4266): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 15:00:48.125: E/AndroidRuntime(4266): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 15:00:48.125: E/AndroidRuntime(4266): at dalvik.system.NativeStart.main(Native Method)
10-09 15:00:48.125: E/AndroidRuntime(4266): Caused by: java.lang.RuntimeException: Failed to invoke public org.joda.time.Chronology() with no args
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:107)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:173)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.Gson.fromJson(Gson.java:805)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.Gson.fromJson(Gson.java:770)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.Gson.fromJson(Gson.java:719)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.nanospark.cnc.GlobalData.retrieveGlobalDataFromStorage(GlobalData.java:92)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.nanospark.cnc.ContactCreateActivity.onCreate(ContactCreateActivity.java:38)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.Activity.performCreate(Activity.java:5133)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-09 15:00:48.125: E/AndroidRuntime(4266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-09 15:00:48.125: E/AndroidRuntime(4266): ... 11 more
10-09 15:00:48.125: E/AndroidRuntime(4266): Caused by: java.lang.InstantiationException: can't instantiate class org.joda.time.Chronology
10-09 15:00:48.125: E/AndroidRuntime(4266): at java.lang.reflect.Constructor.constructNative(Native Method)
10-09 15:00:48.125: E/AndroidRuntime(4266): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-09 15:00:48.125: E/AndroidRuntime(4266): at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:104)
10-09 15:00:48.125: E/AndroidRuntime(4266): ... 27 more
Singleton Code doing the JSON writing:
public void storeGlobalData(Context context){
mPrefs = context.getSharedPreferences("com.nanospark.cnc", Context.MODE_PRIVATE);
preferenceEditor = mPrefs.edit();
Gson gson = new Gson();
//Transform the ArrayLists into JSON Data.
String machineProfileListJSON = gson.toJson(getMachineProfileList());
String contactInfoListJSON = gson.toJson(getContactInfoList());
String eventInfoListJSON = gson.toJson(getEventInfoList());
preferenceEditor.putString("machineProfileListJSONData", machineProfileListJSON);
preferenceEditor.putString("contactInfoListJSONData", contactInfoListJSON);
preferenceEditor.putString("eventInfoListJSONData", eventInfoListJSON);
//Commit the changes.
preferenceEditor.commit();
}
public void retrieveGlobalDataFromStorage(Context context){
mPrefs = context.getSharedPreferences("com.nanospark.cnc", Context.MODE_PRIVATE);
if(mPrefs.contains("machineProfileListJSONData")){
Gson gson = new Gson();
String machineProfileListJSON = mPrefs.getString("machineProfileListJSONData", "");
String contactInfoListJSON = mPrefs.getString("contactInfoListJSONData", "");
String eventInfoListJSON = mPrefs.getString("eventInfoListJSONData", "");
Type machineProfileListType = new TypeToken<ArrayList<MachineProfile>>() {}.getType();
machineProfileList = (gson.fromJson(machineProfileListJSON, machineProfileListType));
Type contactInfoListType = new TypeToken<ArrayList<ContactInfo>>() {}.getType();
contactInfoList = gson.fromJson(contactInfoListJSON, contactInfoListType );
Type eventListType = new TypeToken<ArrayList<Event>>() {}.getType();
eventInfoList = gson.fromJson(eventInfoListJSON, eventListType);
}
}
Report Event Class that is trying to be written:
package com.nanospark.cnc;
import java.util.ArrayList;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
public class ReportEvent extends Event {
public ReportEvent(String eventTitle,String eventDescription,int assignedPin, String trackingType,
LocalDate startDate, LocalDate endDate, boolean watchAllDay,
LocalTime startTime, LocalTime endTime, ArrayList<String> selectedDays,
ArrayList<ContactInfo> selectedContact, String formatChoice,
ReportTimeSpec reportTimingConfig) {
super(eventTitle,eventDescription, assignedPin, trackingType, startDate, endDate, watchAllDay,
startTime, endTime, selectedDays, selectedContact, formatChoice,
reportTimingConfig);
// TODO Auto-generated constructor stub
}
}
Generic Event Class that Report Event extends:
package com.nanospark.cnc;
import java.util.ArrayList;
import java.util.Calendar;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
public class Event {
String eventTitle;
String eventDescription;
int assignedPin;
String trackingType;
LocalDate startDate;
LocalDate endDate;
boolean watchAllDay;
LocalTime startTime;
LocalTime endTime;
ArrayList<String> selectedDays = new ArrayList<String>();
ArrayList<ContactInfo> selectedContact = new ArrayList<ContactInfo>();
String formatChoice;
ReportTimeSpec reportTimingConfigs;
public Event(){
}
public Event(String eventTitle, String eventDescription, int assignedPin, String trackingType,
LocalDate startDate, LocalDate endDate, boolean watchAllDay,
LocalTime startTime, LocalTime endTime, ArrayList<String> selectedDays,
ArrayList<ContactInfo> selectedContact, String formatChoice, ReportTimeSpec reportTimingConfigs) {
this.eventTitle = eventTitle;
this.eventDescription = eventDescription;
this.assignedPin = assignedPin;
this.trackingType = trackingType;
this.startDate = startDate;
this.endDate = endDate;
this.watchAllDay = watchAllDay;
this.startTime = startTime;
this.endTime = endTime;
this.selectedDays = selectedDays;
this.selectedContact = selectedContact;
this.formatChoice = formatChoice;
this.reportTimingConfigs = reportTimingConfigs;
}
public String getEventTitle() {
return eventTitle;
}
public void setEventTitle(String eventTitle) {
this.eventTitle = eventTitle;
}
public String getEventDescription() {
return eventDescription;
}
public void setEventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
public ReportTimeSpec getReportTimingConfigs() {
return reportTimingConfigs;
}
public void setReportTimingConfigs(ReportTimeSpec reportTimingConfigs) {
this.reportTimingConfigs = reportTimingConfigs;
}
public int getAssignedPin() {
return assignedPin;
}
public void setAssignedPin(int assignedPin) {
this.assignedPin = assignedPin;
}
public String getTrackingType() {
return trackingType;
}
public void setTrackingType(String trackingType) {
this.trackingType = trackingType;
}
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
public LocalDate getEndDate() {
return endDate;
}
public void setEndDate(LocalDate endDate) {
this.endDate = endDate;
}
public boolean isWatchAllDay() {
return watchAllDay;
}
public void setWatchAllDay(boolean watchAllDay) {
this.watchAllDay = watchAllDay;
}
public LocalTime getStartTime() {
return startTime;
}
public void setStartTime(LocalTime startTime) {
this.startTime = startTime;
}
public LocalTime getEndTime() {
return endTime;
}
public void setEndTime(LocalTime endTime) {
this.endTime = endTime;
}
public ArrayList<String> getSelectedDays() {
return selectedDays;
}
public void setSelectedDays(ArrayList<String> selectedDays) {
this.selectedDays = selectedDays;
}
public ArrayList<ContactInfo> getSelectedContact() {
return selectedContact;
}
public void setSelectedContact(ArrayList<ContactInfo> selectedContact) {
this.selectedContact = selectedContact;
}
public String getFormatChoice() {
return formatChoice;
}
public void setFormatChoice(String formatChoice) {
this.formatChoice = formatChoice;
}
public ReportTimeSpec getReportTimeSpec() {
return reportTimingConfigs;
}
public void setReportTimeSpec(ReportTimeSpec reportTimingConfigs) {
this.reportTimingConfigs = reportTimingConfigs;
}
@Override
public String toString(){
return this.eventTitle;
}
}
Converters from https://github.com/gkopff/gson-javatime-serialisers are for java.time classes from Java8, e.g. java.time.LocalDate.
But you need converters for org.joda.time classes from 3rd part library. You can create custom converters for it, like:
Converter for org.joda.time.LocalDate
public class LocalDateSerializer implements JsonDeserializer<LocalDate>, JsonSerializer<LocalDate>
{
private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.date();
@Override
public LocalDate deserialize(final JsonElement je, final Type type,
final JsonDeserializationContext jdc) throws JsonParseException
{
final String dateAsString = je.getAsString();
if (dateAsString.length() == 0)
{
return null;
}
else
{
return DATE_FORMAT.parseLocalDate(dateAsString);
}
}
@Override
public JsonElement serialize(final LocalDate src, final Type typeOfSrc,
final JsonSerializationContext context)
{
String retVal;
if (src == null)
{
retVal = "";
}
else
{
retVal = DATE_FORMAT.print(src);
}
return new JsonPrimitive(retVal);
}
}
Converter for org.joda.time.LocalTime
public class LocalTimeSerializer implements JsonDeserializer<LocalTime>, JsonSerializer<LocalTime>
{
private static final DateTimeFormatter TIME_FORMAT = ISODateTimeFormat.timeNoMillis();
@Override
public LocalTime deserialize(final JsonElement je, final Type type,
final JsonDeserializationContext jdc) throws JsonParseException
{
final String dateAsString = je.getAsString();
if (dateAsString.length() == 0)
{
return null;
}
else
{
return TIME_FORMAT.parseLocalTime(dateAsString);
}
}
@Override
public JsonElement serialize(final LocalTime src, final Type typeOfSrc,
final JsonSerializationContext context)
{
String retVal;
if (src == null)
{
retVal = "";
}
else
{
retVal = TIME_FORMAT.print(src);
}
return new JsonPrimitive(retVal);
}
}
Usage
public void retrieveGlobalDataFromStorage(Context context)
{
// ...
final GsonBuilder builder = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateSerializer())
.registerTypeAdapter(LocalTime.class, new LocalTimeSerializer());
final Gson gson = builder.create();
// ...
这篇关于包含JodaTime对象为JSON序列化的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!