用JSONFORMAT模式编写正则表达式


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

是否可以用图案编写正则表达式?我不能

staters =" yyyy-mm-dd'hh:mm:ss.sss(z?("

我想将z作为可选

有任何链接建议?

我最终基于LocalDateDeserializer.INSTANCE创建自定义求职者,然后将正则态度移动到那里。

注册求职者后,对象映射器作为自定义模块不再需要@JsonFormat注释:

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

并在CustomTimeModule

中定义了避难所
    class CustomTimeModule extends SimpleModule {
        public CustomTimeModule() {
            super();
            addDeserializer(LocalDate.class, CustomLocalDateDeserializer.INSTANCE);
        }
    }

最终是正则零件,就我而言,我有时会在日期之后获得可选的非标准时区,但很容易扩展以匹配您的案件:

        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.");
            }

最新更新