Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write regex in JsonFormat pattern

Tags:

json

spring

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSS")

is it possible to write regex in pattern? I could not

pattern ="yyyy-MM-dd'T'HH:mm:ss.SSS(Z?)"

I want to make Z as optional

any links suggestions?

like image 298
roki Avatar asked Mar 27 '26 14:03

roki


1 Answers

I ended up creating custom deserializer based on LocalDateDeserializer.INSTANCE and moved the regex there.

After registering the deserializer the object mapper as a custom module the @JsonFormat annotation is no longer required:

    @Bean
    public ObjectMapper createObjectMapper() {
        return new ObjectMapper()
              .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
              .registerModule(new JavaTimeModule())
              .registerModule(new CustomTimeModule());
    }

and defined the deserializer in the CustomTimeModule

    class CustomTimeModule extends SimpleModule {
        public CustomTimeModule() {
            super();
            addDeserializer(LocalDate.class, CustomLocalDateDeserializer.INSTANCE);
        }
    }

and finally the regex part, in my case was cutting of the optional non-standard time zone that i was sometimes getting after the date, but could be easily extended to match your case:

        public class CustomLocalDateDeserializer extends JSR310DateTimeDeserializerBase<LocalDate> {
            private static final long serialVersionUID = 1L;

            private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;

            public static final CustomLocalDateDeserializer INSTANCE = new CustomLocalDateDeserializer();

            private CustomLocalDateDeserializer() {
                this(DEFAULT_FORMATTER);
            }

            public CustomLocalDateDeserializer(DateTimeFormatter dtf) {
                super(LocalDate.class, dtf);
            }

            @Override
            protected JsonDeserializer<LocalDate> withDateFormat(DateTimeFormatter dtf) {
                return new CustomLocalDateDeserializer(dtf);
            }

            @Override
            public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException
            {
                if (parser.hasToken(JsonToken.VALUE_STRING)) {
                    String string = parser.getText().trim();
                    if (string.length() == 0) {
                        return null;
                    }



                    // >>>>>>> regex part comes here <<<<<<<
                    string = parser.getText().trim().substring(0, 10);
                    // >>>>>>> regex part comes here <<<<<<<



                    // as per [datatype-jsr310#37], only check for optional (and, incorrect...) time marker 'T'
                    // if we are using default formatter
                    try {
                        return LocalDate.parse(string, _formatter);
                    } catch (DateTimeException e) {
                        return _handleDateTimeException(context, e, string);
                    }
                }
                if (parser.isExpectedStartArrayToken()) {
                    JsonToken t = parser.nextToken();
                    if (t == JsonToken.END_ARRAY) {
                        return null;
                    }
                    if (context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
                            && (t == JsonToken.VALUE_STRING || t==JsonToken.VALUE_EMBEDDED_OBJECT)) {
                        final LocalDate parsed = deserialize(parser, context);
                        if (parser.nextToken() != JsonToken.END_ARRAY) {
                            handleMissingEndArrayForSingle(parser, context);
                        }
                        return parsed;
                    }
                    if (t == JsonToken.VALUE_NUMBER_INT) {
                        int year = parser.getIntValue();
                        int month = parser.nextIntValue(-1);
                        int day = parser.nextIntValue(-1);

                        if (parser.nextToken() != JsonToken.END_ARRAY) {
                            throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY,
                                    "Expected array to end");
                        }
                        return LocalDate.of(year, month, day);
                    }
                    context.reportInputMismatch(handledType(),
                            "Unexpected token (%s) within Array, expected VALUE_NUMBER_INT",
                            t);
                }
                if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
                    return (LocalDate) parser.getEmbeddedObject();
                }
                // 06-Jan-2018, tatu: Is this actually safe? Do users expect such coercion?
                if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
                    return LocalDate.ofEpochDay(parser.getLongValue());
                }
                return _handleUnexpectedToken(context, parser, "Expected array or string.");
            }

like image 199
Aleksandar Golubovski Avatar answered Mar 30 '26 04:03

Aleksandar Golubovski



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!