mrog
mrog

Reputation: 2124

How can I tell Jackson to skip invalid records during deserialization?

I have a JSON file containing millions of records. I'd like to use a Jackson iterator to read the records one at a time and perform an action for each one. Here's the code so far.

MappingIterator<MyClass> iterator = new ObjectMapper()
        .readerFor(MyClass.class)
        .readValues(file);

while (iterator.hasNext()) {
    MyClass object = iterator.next();
    ...
}

The problem is that a few of the records are invalid due to missing quotes or illegal characters. This causes Jackson to throw an exception and quit. How can I tell Jackson to skip these records and continue to parse the remaining valid records?

Upvotes: 1

Views: 1970

Answers (1)

Rony Nguyen
Rony Nguyen

Reputation: 1144

try @JsonIgnoreProperties(ignoreUnknown = true) or you may need JsonFilter or customize serialization

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(using = UserDeserializer.class)
public class User {
    private Long id;
    private String name;
    private User() {}
    constructor, setter, getter 
}
public class UserDeserializer extends JsonDeserializer<User> {
    @Override
    public User deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
        try {
            ObjectCodec oc = jsonParser.getCodec();
            JsonNode node = oc.readTree(jsonParser);
            final Long id = node.get("id").asLong();
            final String name = node.get("name").asText();
            return new User(id, name);
        } catch (JsonParseException ex) {
        } catch (Exception e) {}
        return null;
    }
}


    public static void main(String[] args) throws IOException {
    String input = "[{\"id\": 1, \"name\": \"valid\"}," +
            " {\"id\": 2, \"name\": invalid}," +
            " {\"id\": 3, \"name\": \"valid\"}]";

    ObjectMapper objectMapper = new ObjectMapper();
    List<User> users = objectMapper.readValue(input, objectMapper.getTypeFactory().constructCollectionType(List.class, User.class));
    users.forEach(System.out::println);
}

Output

1 valid
null
null
3 valid

So you just ignore/filter null in collection

Upvotes: 1

Related Questions