1
MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader(Map.class).with(bootstrap).readValue(file.getInputStream());

Above code throws this error:

java.util.LinkedHashMap cannot be cast to com.fasterxml.jackson.databind.MappingIterator
Gokul Nath KP
  • 14,745
  • 24
  • 85
  • 122
user972404
  • 11
  • 3

3 Answers3

2

That happens because the method to get MappingIterator is readValues instead of readValue.

The second method "readValue" (without 's') is used to read a single value from the schema.

Eduardo
  • 477
  • 5
  • 9
0

The code csvMapper.reader(Map.class).with(bootstrap).readValue(file.getInputStream()); returns LinkedHashMap. But MappingIterator cannot hold LinkedHashMap.

Not sure though, but try explicitly casting as shown below:

(MappingIterator)(csvMapper.reader(Map.class).with(bootstrap).readValue(file.getInputStream()));
Gokul Nath KP
  • 14,745
  • 24
  • 85
  • 122
0

I was having the same problem with an example I got from directly convert CSV file to JSON file using the Jackson library

Which version of the jackson databind and dataformat libraries are you using? There is a method readValues(InputStream) that returns a MappingIterator. But it seems there were a few changes in that area between the versions.

Community
  • 1
  • 1
Dennis Hunziker
  • 1,273
  • 9
  • 18