0

I have been trying to desalinize a JSON string containing Date but I am getting the following exception-

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '/Date(1458672480000)/': not a valid representation (error: Unparseable date: "/Date(1458672480000)/" (at offset 0))
 at [Source: java.io.StringReader@32e26583; line: 1, column: 199] 

Code details is as below-

DataModel-

@JsonIgnoreProperties(ignoreUnknown = true)
    public class DataModel {
        public Integer Capacity;
        public Long Id;
        public String Name;
        public Date StartDate;
        public Date EndDate;
        public String Message;
        public Integer LocationId;
        public Boolean IsValid;
        public Integer[] NickNames = new Integer[0];

    }

JSON string-

{"d":[{"__type":"my.package.name.className","Id":1,"Name":"xxx","PlaceId":2,"Message":"","IsValid":false,"NickNames":[],"StartDate":"\/Date(1458672480000)\/","EndDate":"\/Date(1458689400000)\/","Size":0,"StringStartDate":"2016-03-22T14:48:00-04:00","StringEndDate":"2016-03-22T19:30:00-04:00"}]}

De serialization code-

ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
mapper.setDateFormat(dateFormat);
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
TypeReference<HashMap<String,DataModel[]>> typeRef= new TypeReference<HashMap<String,DataModel[]>>(){};
HashMap<String,DataModel[]> newSessions = mapper.readValue(data, typeRef);

Is there any problem in the JSON string? If not, what is the correct way to deserialize it?

2
  • 1458672480000 instead of Date(1458672480000)? Commented Apr 4, 2016 at 11:18
  • You have a field called StringStartDate in your json that contains the data exactly how you expect it. That field does not occur in your DataModel though. The StartDate field in your json contains invalid content and thus cannot be deserialized into a java.util.Date. Commented Apr 4, 2016 at 11:19

1 Answer 1

1

The constructor for a new Date object is Date(long millis). But you are passing this String into it - "Date(1458672480000)".

Get the long value from this String and then create the Date object. Suppose your startDate is "Date(1458672480000)" then -

Date d = new Date(Long.parseLong(startDate.substring(5, 18)));

Here we are extracting the numerical part of the String and converting it to long type.

Ideally it would be better if your server sends you the long value directly so that you don't have to parse it. You code would've worked perfect if the long date value was coming directly in json like this - "startDate": 1458672480000.

Sign up to request clarification or add additional context in comments.

4 Comments

The last part is the correct approach. I have never seen a date serialized as Date(1458672480000) before.
Thanks for the answer. The exception throws at this line- HashMap<String,SessionModel[]> newSessions = mapper.readValue(data, typeRef);, where data is the JSON string. Now, where do I write this line - Date d = new Date(Long.parseLong(startDate.substring(5, 18))); ?
I don't think you can do it this way. Your only option this way it to make StartDate and EndDate String types. And then convert them into date whenever you want to use them using the code in my answer. OR take the advice in my last line of answer
Thanks a lot. I will try both the approaches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.