0

I get this error when attempting to deserialize but I feel there is a deserializing interface i need to configure.

The error:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.sql.Timestamp` from String "2022-01-13 23:57:58": not a valid representation (error: Failed to parse Date value '2022-01-13 23:57:58': Cannot parse date "2022-01-13 23:57:58": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))
 at [Source: (String)"{"endTime": "2022-01-13 23:57:58", "startTime": "2022-01-13 23:57:56", "numResults": 6, "numFailures": 0, "sampleResults": {"0": {"id": 0, "enabled": false, "resultsMap": {"tst": "FAIL", "tst2": "PASS"}, "containerId": null, "criteriaMap": {}}, "1": {"id": 1, "enabled": false, "resultsMap": {"tst": "FAIL", "tst2": "PASS"}, "containerId": null, "criteriaMap": {}}, "2": {"id": 2, "enabled": true, "resultsMap": {}, "containerId": null, "criteriaMap": {"ICCI_VE_OSW_B_Z01": "N_A", "ICCI_VE_OSW_B_Z02""[truncated 464 chars]; line: 1, column: 13] (through reference chain: filtec.test.TestRun["endTime"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1991)
    at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1219)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:1261)
    ...
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4675)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3630)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3598)
    at filtec.db.TestRunEntity.getData(TestRunEntity.java:79)

the calling method:

public TestRun getData() {
    TestRun data = null;
    try {
        data = MAPPER.readValue(jsonDoc, TestRun.class);
    } catch (JsonProcessingException e) {
        ApplicationManager.logStackTrace(e);
    }
    return data;
}

my serializer:

public class JsonTimestampSerializer extends JsonSerializer<Timestamp> {
    @Override
    public void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = sdf.format(value);
            gen.writeString(s);
        } catch (DateTimeParseException e) {
            System.err.println(e);
            gen.writeString("");
        }
    }
}

and my TestRun POJO fields:

public class TestRun {
    //...
    /** Internal state of a test run. */
    enum State {
        CREATED,
        IN_PROGRESS,
        COMPLETE
    }
    
    /** State of the test session. */
    State state = State.CREATED;
    
    /** The start time of the run. */
    @JsonSerialize(using = JsonTimestampSerializer.class)
    Timestamp startTime;
    
    /** The end time of the test run. */
    @JsonSerialize(using = JsonTimestampSerializer.class)
    Timestamp endTime;
3
  • Where's your de-serializer? Also, sdf.format(value) can't throw DateTimeParseException... have you confused serialization and de-serialization? Commented Jan 18, 2022 at 1:05
  • Do I need to add a deserializer with a class such as: fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/… ? Commented Jan 18, 2022 at 1:07
  • Basic principle: If you serialized something yourself, unless you duplicated an existing built-in serializer (why would you do that), you will have to provide the corresponding de-serializer. DateTimeParserException, cannot occur here, but could occur in a deserializer. Commented Jan 18, 2022 at 1:19

1 Answer 1

2

Looks like a solution was to add a deserializer but an even more ideal solution per Jim's suggestion is to use a built-in serializer which works for the string format I am using.

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
Timestamp startTime;
Sign up to request clarification or add additional context in comments.

Comments

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.