1

I've got the following reader configured:

@Configuration
public class ReaderConfig {
    @Bean
    public JsonItemReader<String> jsonItemReader(Resource resource) {
        return new JsonItemReaderBuilder<String>()
                .jsonObjectReader(new JacksonJsonObjectReader<>(String.class))
                .resource(resource)
                .name("jsonItemReader")
                .build();
    }
}

With this test:

@Test
public void jsonItemReaderTest() throws Exception {
    ReaderConfig config = new ReaderConfig();

    Resource sampleJsonResource = new ClassPathResource("sampleResponse.json");

    JsonItemReader<String> reader = config.jsonItemReader(sampleJsonResource);

    reader.open(new ExecutionContext());

    String item = null;
    List<String> results = new ArrayList<>();
    do {
        item = reader.read();
        System.err.println(item);
        if (!Objects.isNull(item)) {
            results.add(item);
        }
    } while (item != null);

    reader.close();

    assertThat(results).hasSize(7);
}

and the following JSON:

[
  "A",
  "B",
  "C",
  "D",
  "E",
  "F",
  "G"
]

However, the reader returns null. Trying to trace with the debugger, what I'm getting is that the parser returns back that it has a string at the beginning of the array and there's an expectation that there should instead be an opening brace. Any pointers on this? It seems like such an obvious use case that I'm surprised it isn't covered by the code.

Don Hosek
  • 981
  • 6
  • 23

1 Answers1

2

The JsonItemReader is designed to read an array of JSON objects (See its Javadoc). According to http://www.json.org, a JSON object is defined as follows:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

So according to this definition, your JSON input is not an array of JSON objects.

Hope this helps.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50