I am trying to serialize a class that has two Date
fields defined:
import com.google.gson.annotations.Expose;
import java.util.Date;
public class DateRange {
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Expose
public Date startDate;
@Expose
public Date endDate;
public DateRange(Date startDate, Date endDate) {
this.startDate = startDate;
this.endDate = endDate;
}
public DateRange(String startDate, String endDate ) throws ParseException{
this.startDate = dateFormat.parse(startDate);
this.endDate = dateFormat.parse(endDate);
}
}
but using gson.toJson
an exception is thrown where multiple
import com.google.gson.Gson
Gson gson = new Gson()
gson.toJson(new DateRange("2011-11-11", "2012-11-11"))
results in
java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields named maximumIntegerDigits
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
The problem is exacerbated where I have a class ManyDates
which has a field with DateRange
as well as another field with an array of DateRange
. I have attempted adding the field as a private transient
but no luck (also tried with the field as a String
type)
private transient java.text.DecimalFormat maximumIntegerDigits;
but the field is still causing issues with the serialization. I'm not sure where that field is even coming from but I suspect a simple solution to this must be just out of reach that I'm just not seeing.