You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-1Lines changed: 54 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -581,7 +581,60 @@ This will deserialize JSON fields with `known_as`, as well as `identifer` and `f
581
581
Note: to use the `@JsonAlias` annotation, a `@JsonProperty` annotation must also be used.
582
582
583
583
Overall, Jackson library is very powerful in deserializing objects using builder pattern.
584
-
584
+
585
+
## Tutorial: Collecting multiple errors (3.1+)
586
+
587
+
One recently introduced feature is the ability to collect multiple deserialization errors instead of failing fast on the first one. This can be really handy for validation use cases.
588
+
589
+
By default, if Jackson encounters a problem during deserialization -- say, string `"xyz"` for an `int` property -- it will immediately throw an exception and stop. But sometimes you want to see ALL the problems in one go.
590
+
591
+
Consider a case where you have a couple of fields with bad data:
// Can also access problem.getRawValue() to see what the bad input was
617
+
}
618
+
}
619
+
```
620
+
621
+
This will report all 3 problems at once. Much better.
622
+
623
+
By default, Jackson will collect up to 100 problems before giving up (to prevent DoS-style attacks with huge bad payloads). You can configure this:
624
+
625
+
```java
626
+
ObjectReader reader = mapper.readerFor(Order.class).problemCollectingReader(10); // limit to 10
627
+
```
628
+
629
+
Few things to keep in mind:
630
+
631
+
1. This is best-effort: not all problems can be collected. Malformed JSON (like missing closing brace) or other structural problems will still fail immediately. But type conversion errors, unknown properties (if you enable that check), and such will be collected.
632
+
2. Error paths use JSON Pointer notation (RFC 6901): so `"/items/0/price"` means first item in `items` array, `price` field. Special characters get escaped (`~` becomes `~0`, `/` becomes `~1`).
633
+
3. Each call to `readValueCollectingProblems()` gets its own problem bucket, so it's thread-safe to reuse the same `ObjectReader`.
634
+
4. Fields that fail to deserialize get default values (0 for primitives, null for objects) during the attempt, but if any problems are collected, only the problems are reported in the `DeferredBindingException` - the partial result is not returned.
635
+
636
+
This is particularly useful for things like REST API validation (return all validation errors to client), or batch processing (log errors but keep going), or development tooling.
637
+
585
638
# Contribute!
586
639
587
640
We would love to get your contribution, whether it's in form of bug reports, Requests for Enhancement (RFE), documentation, or code patches.
0 commit comments