0

I have a JSON with Joda DateTime field. It has some sample values. But whenever I convert it to Object it automatically takes the current DateTime instead of the DateTime present in the JSON.

PFB the sample JSON

[{
"pas": "CSP",
"policyNumber": "ZU131874",
"schemeName": "PepsiCo employee scheme20",
"policyStatus": "ACTIVE",
"productCode": "GPP",
"totalSavings": 100000,
"investmentReturn": 55000,
"effectiveDate": {
    "startDate": {
        "dayOfYear": 2,
        "year": 2014,
        "dayOfMonth": 2,
        "dayOfWeek": 4,
        "era": 1,
        "weekOfWeekyear": 1,
        "millisOfSecond": 0,
        "secondOfMinute": 0,
        "minuteOfDay": 0,
        "centuryOfEra": 20,
        "yearOfCentury": 14,
        "hourOfDay": 0,
        "monthOfYear": 1,
        "weekyear": 2014,
        "minuteOfHour": 0,
        "yearOfEra": 2014,
        "secondOfDay": 0,
        "millisOfDay": 0,
        "millis": 1388601000000
    },
    "endDate": null
}
}, {
"pas": "CSP",
"policyNumber": "ZU146271",
"schemeName": "PepsiCo employee scheme7",
"policyStatus": "ACTIVE",
"productCode": "GPP",
"totalSavings": 100000,
"investmentReturn": 55000,
"effectiveDate": {
    "startDate": {
        "dayOfYear": 156,
        "year": 2015,
        "dayOfMonth": 5,
        "dayOfWeek": 5,
        "era": 1,
        "weekOfWeekyear": 23,
        "millisOfSecond": 0,
        "secondOfMinute": 0,
        "minuteOfDay": 0,
        "centuryOfEra": 20,
        "yearOfCentury": 15,
        "hourOfDay": 0,
        "monthOfYear": 6,
        "weekyear": 2015,
        "minuteOfHour": 0,
        "yearOfEra": 2015,
        "secondOfDay": 0,
        "millisOfDay": 0,
        "millis": 1433442600000
    },
    "endDate": null
}
}]

I am using following code to convert list of JSON objects to a list Of Java objects.

policies = new ArrayList<Policy>();

JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader("./src/test/resources/" + "sample-zurich-pensions.json"));
Type listType = new TypeToken<List<Policy>>(){}.getType();
List<Policy> policyList = new Gson().fromJson(jsonElement, listType);
policies.addAll(policyList);

In the jsonElement I am getting the exact value, but in the policyList the DateTime is set to the current date.

PFB the classes Policy.java

private String pas;
private String policyNumber;
private String schemeName;  
private String policyStatus;
private String productCode;
private BigDecimal totalSavings;
private BigDecimal investmentReturn;
private EffectiveDate effectiveDate;

EffectiveDate.java

private DateTime startDate;
private DateTime endDate;
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Asif Billa
  • 821
  • 2
  • 15
  • 28
  • 1
    [related, I think](http://stackoverflow.com/questions/14996663/is-there-a-standard-implementation-for-a-gson-joda-time-serialiser); also, do you really need to serialize the whole of the date-time object in the json? won't a simple year-month-day-hour-minute-second format do? – aasu Feb 16 '16 at 07:18

1 Answers1

3

During deserialization from JSON, Gson is creating a new DateTime() (which is equal to current system DateTime). The fields present in your JSON are based on getters in DateTime, but there are no setters for them present, so the object cannot be adjusted to the timestamp represented by the JSON. You are much better off using a standard date-time representation like ISO 8601. Then, implement a JsonSerializer and JsonDeserializer for DateTime as suggested on the Gson site:

class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {

    @Override
    public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) {
        return new JsonPrimitive(src.toString());
    }

    @Override
    public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
            throws JsonParseException {
        return new DateTime(json.getAsString());
    }
}

or use one of the solutions provided in this post (linked also by @user2762451). You can register the serializer/deserializer like this:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter());
Gson gson = gsonBuilder.create();
Community
  • 1
  • 1
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102