FasterXML / jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Apache License 2.0
398 stars 116 forks source link

Add javaTimeModule.addDeserializer(String.class,...) Error parsing generated later #313

Closed k631583871 closed 2 months ago

k631583871 commented 2 months ago

Search before asking

Describe the bug

public static void main(String[] args) throws IOException {
        String json = "{\"list\":[{\"A1\":\"123456\",\"A2\":\"530000\",\"op\":[{\"id\":1}]}]}";
        ObjectMapper mapper = new ObjectMapper();

        JavaTimeModule javaTimeModule = new JavaTimeModule();

        {
            //在下边这个代码之后,就会产生错误的解析,  list 从 1 个变成了 2个
            //After the following code, an error parsing will occur, and the list will change from 1 to 2
            javaTimeModule.addDeserializer(String.class, new JsonDeserializer<String>() {
                @Override
                public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
                    return p.getText();
                }
            });
        }

        mapper.registerModule(javaTimeModule);

        Bean bean = mapper.readValue(json, Bean.class);
        System.out.println(mapper.writeValueAsString(bean));
    }

Version Information

2.17.1

Reproduction

<-- Any of the following

  1. Brief code sample/snippet: include here in preformatted/code section
  2. Longer example stored somewhere else (diff repo, snippet), add a link
  3. Textual explanation: include here -->
    // Your code here

Expected behavior

Cannot deserialize value of type java.lang.String from Array value (token JsonToken.START_ARRAY)

Additional context

No response

cowtowncoder commented 2 months ago

Example seems to be missing definition of Bean class? That is needed to understand issue.

(note: moved from jackson-databind to this repository since it is related to JavaTimeModule)

k631583871 commented 2 months ago
    public static class Bean {
        List<Map<String, String>> list;

        public List<Map<String, String>> getList() {
            return list;
        }

        public void setList(List<Map<String, String>> list) {
            this.list = list;
        }
    }
JooHyukKim commented 2 months ago

Not a bug, the error must be caused by \"op\":[{\"id\":1}] is inserted in Map<String,String> same level as "A1", "A2"

cowtowncoder commented 2 months ago

Also looks like custom deserializer is blindly calling parser.getText() without verifying actual JsonToken. Which is asking for trouble -- esp. if parser is pointing to Array or Object, because if so, the whole value (multiple tokens) must be consumed. And the exception suggests that in fact this is the case I here.