4

I am using Jackson inside my Spring Boot project for generator JSON. In my Java class I am using private LocalDateTime timestamp; Jackson converts it to JSON. When timestamp contains for example: 2022-04-26T05:59:54:124530the JSON Object will receive 2022-04-26T05:59:54:12453

This causes an error in my receiving system which cannnot work with this date format. Unfortunantelly I am not able to change the receiving system.

Is there a way I can Jackson tell not to remove the zeros at the end?

Daniel Pomrehn
  • 791
  • 3
  • 8
  • 23
  • see if this helps https://stackoverflow.com/questions/53314552/java-instant-to-localdatetime-trailing-zero if that doesnt help you might need to use custom serializer using `@JsonGetter` – Kaus2b Apr 26 '22 at 05:23
  • That’s a pity. Your JSON string lives up to [the ISO 8601 standard](https://en.wikipedia.org/wiki/ISO_8601) nicely no matter if the trailing 0 is there or not, so any decent receiving system ought to accept it. – Ole V.V. Apr 26 '22 at 08:09
  • 1
    Did you mean "54.12453" rather than "54:12453"? (FULL STOP versus COLON) – Basil Bourque Apr 27 '22 at 02:50

2 Answers2

0

LocaleDateTime class ha no static string format. It includes few options:

  • uuuu-MM-dd'T'HH:mm
  • uuuu-MM-dd'T'HH:mm:ss
  • uuuu-MM-dd'T'HH:mm:ss.SSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

Custom Serialization will help you to achieve static output. In example below I use a format with 6 milliseconds digits just like in your case.

@JsonDeserialize(using = BodyDeserializer.class)
@JsonSerialize(using = BodySerializer.class)
public class Body {

    public static final DATE_FORMAT = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss:SSSSSS");
    
    private LocalDateTime myDate;

    public Body(LocalDateTime myDate) {
        this.myDate = myDate;
    }

    public StrLocalDateTimeing getMyDate() { return myDate; }
}

public class BodyDeserializer extends JsonDeserializer {

    @Override
    public User deserialize(JsonParser jsonParser,
            DeserializationContext deserializationContext) throws IOException {
        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);
        String date = node.get("myDate");
        LocalDateTime myDate = LocalDateTime.from(Body.DATE_FORMAT.parse(date))
        return new Body(myDate),
    }
}

public class BodySerializer extends JsonSerializer {

    @Override
    public void serialize(Body body, JsonGenerator jsonGenerator,
            SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        
        jsonGenerator.writeNumberField("myDate", Body.DATE_FORMAT.format(body.getMyDate()));
        jsonGenerator.writeEndObject();
    }
}
Alex
  • 141
  • 9
0

You can use @JsonFormat to suppress the removal of trailing zeros.

public class Transaction {

    import com.fasterxml.jackson.annotation.JsonFormat;
    //...

    @JsonFormat(
        shape = JsonFormat.Shape.STRING,
        pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
    public LocalDateTime dateTime;

}

My use case of the above code is for json kafka serializer.

Boyi
  • 76
  • 2
  • 3