Skip to content

FilteringParserDelegate cannot deserialize empty list #708

Description

@xiazuojie

This is a follow up issue of #700 .

If using FilteringParserDelegate to filter out a part of json, it throws the following exception when the input json is an empty list ("[]").

com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: (byte[])"[]"; line: 1, column: 2]

	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
	at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4688)
	at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:4561)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2823)
	at com.alibaba.dapr.serialization.json.FilteringParserDelegateTest.testFilteringParserDelegateWithEmptyList(FilteringParserDelegateTest.java:36)

Test case:

package com.alibaba.dapr.serialization.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.filter.FilteringParserDelegate;
import com.fasterxml.jackson.core.filter.TokenFilter;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertTrue;

public class FilteringParserDelegateTest {
    @Test
    public void testFilteringParserDelegateWithEmptyList() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        final TokenFilter tokenFilter = new TokenFilter() {
            @Override
            public TokenFilter includeProperty(String name) {
                if ("@type".equals(name)) {
                    return null;
                }
                return INCLUDE_ALL;
            }
        };

        byte[] content = "[]".getBytes(StandardCharsets.UTF_8);

        JsonParser jp = mapper.createParser(content);
        JsonParser jsonParser = new FilteringParserDelegate(jp, tokenFilter, TokenFilter.Inclusion.INCLUDE_ALL_AND_PATH, true);

        List<Map<String, String>> a = mapper.readValue(jsonParser, new TypeReference<List<Map<String, String>>>() {});
        assertTrue(a.isEmpty());
    }
}

Jackson version: 2.12.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.21Issues planned (at earliest) for 2.21

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions