JSONmedium3-5 years

Jackson's `ObjectMapper.readValue(json, Order.class)` successfully returns an `Order` object even though the JSON was missing a field the code assumes is always present, and three methods later something throws a `NullPointerException`. Why didn't `readValue` reject the malformed JSON, and how does Jackson actually turn text into an object in the first place?

JSON has no schema built in, so a missing field isn't a parse error to Jackson at all — it just becomes null (or a primitive's default value) on the Java side, and nothing complains until code three methods downstream tries to use that field and dereferences a null. Under the hood, Jackson doesn't go straight from text to your object in one step: a low-level parser first turns the raw text into a stream of tokens (start object, field name, string value, end object...) with no idea what class it's populating, and a second layer — the data binder — walks that token stream and, using reflection over your class's fields, decides where each value goes. Neither layer consults a schema, because JSON doesn't have one to consult; 'this field must be present' is something you have to add yourself, with a validation annotation, not something Jackson checks for free.

The lesson behind it →
More on JSON