Scrivio Interview library

Gson and other JSON libraries

28 questions in 10 subtopics.

Gson basics

ConceptFoundations6 min read
How does Gson convert a Java object to JSON and back with new Gson().toJson(obj) and fromJson(json, Order.class)? What does it use to read fields, and how are nulls handled by default?
DebuggingFoundations6 min read
A class has private int retries = 3; and no no-arg constructor. After new Gson().fromJson("{}", Settings.class), retries is 0, not 3. Explain why and how to fix it.
DebuggingIntermediate6 min read
A service upgraded to Java 17 and now Gson throws InaccessibleObjectException when serializing an object that contains a java.time.Instant field. Why, and what is the right fix?
LeadershipPrincipal6 min read
Gson's own project page describes it as being in maintenance mode, yet dozens of your services and libraries use it. What does this mean for your long-term choice, and what would you do?

GsonBuilder configuration

CodingFoundations5 min read
Configure a Gson instance that pretty-prints, includes nulls, uses snake_case field names, does not escape HTML characters, and formats a LocalDate as yyyy-MM-dd.
ScenarioIntermediate5 min read
Each team in your service creates its own Gson with different builder settings, and the same Order class is serialized with different field names and null handling depending on the code path. How do you fix this?

Custom serializers and type adapters

CodingFoundations6 min read
Write a Gson TypeAdapter that writes a Money(BigDecimal amount, String currency) as the string "12.50 EUR" and reads it back. When would you use JsonSerializer and JsonDeserializer instead?
DesignIntermediate7 min read
You need polymorphic deserialization in Gson: a JSON array of payment methods where each object has a type field of card or bank. Gson has no built-in annotation for this. How would you implement it safely?
ScenarioAdvanced6 min read
A shared client library uses Gson with 30 custom type adapters, and you are migrating it to Jackson. How would you plan and verify the migration?

Excluding fields

ConceptFoundations6 min read
List the ways to exclude a field from Gson serialization and say when you would use each.
ScenarioIntermediate6 min read
A security review found that a User object serialized with Gson in API responses includes passwordHash and internal flags. How do you fix this and prevent it from recurring?

Field naming and versioning

CodingFoundations5 min read
The JSON uses first_name while the Java field is firstName, and older clients send fname. Show how to handle both with Gson.
DesignIntermediate6 min read
How do Gson's @Since and @Until annotations with GsonBuilder.setVersion work, and would you use them to version a public API?

Generic collections with TypeToken

DebuggingFoundations6 min read
This code throws ClassCastException:
List<Order> orders = gson.fromJson(json, List.class);
orders.get(0).getId();
Explain and fix it.
InternalsIntermediate6 min read
A generic method
<T> List<T> parse(String json) {
return gson.fromJson(json, new TypeToken<List<T>>(){}.getType());
}
returns maps instead of T objects. Why does this not work, and how do you fix it?

JSON tree and streaming parsing

CodingFoundations5 min read
Without creating a Java class, read the JSON {"order":{"id":7,"items":[{"sku":"A"},{"sku":"B"}]}} with Gson's tree API and print every sku.
ScenarioIntermediate6 min read
You must process a 3 GB JSON array of transactions in a service with a 512 MB heap. How would you do it with Gson, and what errors would you watch for?
DesignAdvanced6 min read
Several services receive large JSON files from partners and each builds its own parsing code, some with tree models that run out of memory. What standard would you define?

Gson vs Jackson

ConceptFoundations6 min read
Compare Gson and Jackson for a new Spring Boot service. Which would you choose and why?
DebuggingIntermediate7 min read
After switching a client from Gson to Jackson, a downstream service fails because fields that were absent are now sent as null, and a field computed by a getter appears in the JSON. Explain the behaviour difference and fix it.
LeadershipPrincipal7 min read
Your company has Gson, Jackson 2, Jackson 3, JSON-B and org.json across 200 services and Android apps. Would you standardise on one JSON library? Explain the cost, risk and how you would govern it.

JSON-P, JSON-B and org.json

ConceptFoundations6 min read
What are Jakarta JSON Processing (JSON-P) and Jakarta JSON Binding (JSON-B), and how do they relate to Jackson and to org.json?
DebuggingIntermediate7 min read
Code using org.json builds a response with new JSONObject().put("amount", new BigDecimal("10.10")).put("tags", list) and clients complain that the order of keys changes and some numbers look different. Explain what org.json does and what you would use instead.
DesignAdvanced7 min read
You maintain a Jakarta EE application server based product and a Spring Boot product. Should shared libraries depend on JSON-B, Jackson, or neither? Explain the design.
LeadershipPrincipal6 min read
Jakarta EE standardised JSON-P and JSON-B, while most of the Java ecosystem chose Jackson. What lessons does this give you about standards versus de facto libraries when setting technology policy?

JsonPath queries

CodingFoundations6 min read
Using Jayway JsonPath, write expressions for this document:
{"store":{"book":[{"title":"A","price":8},{"title":"B","price":12}]}}
Get all titles, titles of books cheaper than 10, and the number of books.
ScenarioIntermediate7 min read
Your MockMvc tests use jsonPath("$.items[0].price").value(10.5) and fail intermittently after a change that returns items in a different order, and some paths that no longer exist pass silently in custom assertions. How do you make these tests robust?
LeadershipPrincipal7 min read
JSONPath had many incompatible implementations for years and was standardised as RFC 9535 in 2024. Your platform exposes JSONPath in alerting rules, API gateway policies and data pipelines. What do you do about it?