Jackson Timestamp wrong deserialization

Ef Ge :

I have a custom objectmapper-class:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.codehaus.jackson.map.ObjectMapper;


public class CustomObjectMapper extends ObjectMapper {
public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

public CustomObjectMapper() {
    DateFormat df = new SimpleDateFormat(DATE_FORMAT);
    this.setDateFormat(df);
}

and a unit test:

@Test
public void testSerialization() throws JsonParseException, JsonMappingException, IOException  {

    String timestamp = "2019-02-12T07:53:11+0000";
    CustomObjectMapper customObjectMapper = new CustomObjectMapper();
    Timestamp result = customObjectMapper.readValue(timestamp, Timestamp.class);
    System.out.println(result.getTime());

}

The junit-test gives me "2019".

I tried to use a customTimestampDeserializer:

public class CustomJsonTimestampDeserializer extends
    JsonDeserializer<Timestamp> {

@Override
public Timestamp deserialize(JsonParser jsonparser,
        DeserializationContext deserializationcontext) throws IOException,
        JsonProcessingException {

    String date = jsonparser.getText(); //date is "2019"
    JsonToken token = jsonparser.getCurrentToken(); // is JsonToken.VALUE_NUMBER_INT
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
            CustomObjectMapper.DATE_FORMAT);
    try {
        return new Timestamp(simpleDateFormat.parse(date).getTime());
    } catch (ParseException e) {

        return null;
    }
}

}

What am I doint wrong? It seems like jackson thinks the timestamp-string is an integer ans stops parsing it after 2019.

Antot :

The are two problems with this approach.

First of all, there is a suspicious import statement:

import org.codehaus.jackson.map.ObjectMapper;

org.codehaus is a predecessor of current com.fasterxml. It's not clear if it was used intentionally, but ObjectMapper's import should be

import com.fasterxml.jackson.databind.ObjectMapper;

Second, a Timestamp cannot be read directly from a plain String like this

String timestamp = "2019-02-12T07:53:11+0000";

ObjectMapper expects a JSON string. So if it were

{ "timestamp": "2019-02-12T07:53:11+0000" }

and a wrapping class

class TimestampWrapper {

  private Timestamp timestamp;
  // getter + setter for timestamp
}

then the test sequence will execute properly:

String timestamp = "{ \"timestamp\": \"2019-02-12T07:53:11+0000\" }";
CustomObjectMapper customObjectMapper = new CustomObjectMapper();
TimestampWrapper result = customObjectMapper.readValue(timestamp, TimestampWrapper.class);
System.out.println(result.getTimestamp());

update:

Or, without using a dedicated wrapper class, it can be deserialized from an JSON array:

String timestamp = "[ \"2019-02-12T07:53:11+0000\" ]";
CustomObjectMapper customObjectMapper = new CustomObjectMapper();
Timestamp[] result = customObjectMapper.readValue(timestamp, Timestamp[].class);
System.out.println(result[0]);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152046&siteId=1