JSON Parser Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓JSON Parser runs in O(n) time and O(n) space, in Java as in every other language.
- ✓Nothing. Java SE has no JSON parser: the javax.json and jakarta.json APIs belong to Jakarta EE, not to the JDK.
- ✓Use a Jackson ObjectMapper configured deliberately rather than by default, target records or immutable classes, and use the streaming JsonParser for documents large enough that materialising a tree is a problem.
- ✓The Java trap: Jackson polymorphic deserialisation has been the source of a long series of remote-code-execution vulnerabilities, because a payload that is allowed to name its own target class can name a class whose construction has side effects.
- ✓The streaming JsonParser reads tokens without building a tree and is dramatically faster and lighter than readTree for large documents.
What JSON Parser actually is
JSON Parser in Java: what you actually reach for
Java implementation
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class Json {
// Expensive to build, thread-safe once configured: share ONE instance.
private static final ObjectMapper MAPPER = new ObjectMapper()
// A config typo should be an error, not a silently ignored field.
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
public record User(String id, String email, int age) {}
public static User readUser(String body) throws Exception {
return MAPPER.readValue(body, User.class);
}
// Polymorphism the SAFE way: the permitted subtypes are named here,
// in code. Never activateDefaultTyping on untrusted input, letting a
// payload choose its own target class is the root of Jackson's
// remote-code-execution CVE history.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
@JsonSubTypes({
@JsonSubTypes.Type(value = Card.class, name = "card"),
@JsonSubTypes.Type(value = Bank.class, name = "bank"),
})
public sealed interface Method permits Card, Bank {}
public record Card(String last4) implements Method {}
public record Bank(String iban) implements Method {}
public static Method readMethod(String body) throws Exception {
return MAPPER.readValue(body, Method.class);
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for JSON Parser?
Nothing. Java SE has no JSON parser: the javax.json and jakarta.json APIs belong to Jakarta EE, not to the JDK. Jackson and Gson are the ecosystem standards, and Jackson is the one most frameworks embed.
What is the most common Java mistake when implementing JSON Parser?
Jackson polymorphic deserialisation has been the source of a long series of remote-code-execution vulnerabilities, because a payload that is allowed to name its own target class can name a class whose construction has side effects. The dangerous switch was enableDefaultTyping, and Jackson 2.10 deprecated it in favour of activateDefaultTyping, which requires a PolymorphicTypeValidator and therefore an explicit allow-list. The rule that follows is simple and absolute: never enable default typing on input you do not control, and if you need polymorphism, name the permitted subtypes explicitly with JsonSubTypes. This is not a historical curiosity, it is the reason the API changed shape.
How does JSON Parser actually perform in Java?
The streaming JsonParser reads tokens without building a tree and is dramatically faster and lighter than readTree for large documents. ObjectMapper instances are expensive to construct and are thread-safe once configured, so they should be shared, not created per request, which is one of the most common Jackson performance mistakes. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Replit free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.