Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Jackson ObjectMapper ReadValue - Error in converting date

I am seeing the error listed below when trying to convert the json string to an object. I don't have the source code for Data class & it is part of a jar file. Is there a way I could use mixins to get this fixed?

CODE

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(request, Data.class);

ERROR

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of javax.xml.datatype.XMLGregorianCalendar from String value '10:00:00': not a valid representation (error: Can not parse date "10:00:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

JSON REQUEST

 "timeSlot":{  
       "date":"2015-10-21",
       "endTime":"10:00:00",
       "startTime":"08:00:00",   
 }

EDIT

This issue is similar to this , however this is occuring while deserializing. The other solution helped in appropriately serializing the request.

I have pasted the sample code which is not working below -

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class TestJSONDeserialize {

    public static void main(String[] args){

        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixIn(Data.class, MyMixin.class);

        String jsonString = "{\"date\":\"2014-02-10\",\"time\":\"16:15:00\"}";
        try {
            mapper.readValue(jsonString, Data.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

Data class (I cannot make changes to this file)

import javax.xml.datatype.XMLGregorianCalendar;

public class Data {

    private XMLGregorianCalendar date;
    private XMLGregorianCalendar time;

    public XMLGregorianCalendar getDate() {
        return date;
    }
    public void setDate(XMLGregorianCalendar date) {
        this.date = date;
    }
    public XMLGregorianCalendar getTime() {
        return time;
    }
    public void setTime(XMLGregorianCalendar time) {
        this.time = time;
    }



}

I see the below error when I run this :

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of javax.xml.datatype.XMLGregorianCalendar from String value '16:15:00': not a valid representation (error: Failed to parse Date value '16:15:00': Can not parse date "16:15:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: {"date":"2014-02-10","time":"16:15:00"}; line: 1, column: 21] (through reference chain: com.comcast.json.test.Data["time"])

like image 753
Punter Vicky Avatar asked Mar 14 '26 07:03

Punter Vicky


2 Answers

See here. Stick a date formatter on your field:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="HH:mm:ss", timezone="CET")
private Date startTime;
like image 161
sh0rug0ru Avatar answered Mar 16 '26 21:03

sh0rug0ru


Already answered in this old post, but hope this will be helpful for someone in future.

Specifying DateFormat in ObjectMapper solved my issue.

objectMapper.setDateFormat(new SimpleDateFormat(MY_FORMAT));
like image 38
RahulArackal Avatar answered Mar 16 '26 20:03

RahulArackal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!