Gson and other JSON libraries
28 questions in 10 subtopics.
Gson basics
How does Gson convert a Java object to JSON and back withnew Gson().toJson(obj) and fromJson(json, Order.class)? What does it use to read fields, and how are nulls handled by default?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.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?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
Configure a Gson instance that pretty-prints, includes nulls, uses snake_case field names, does not escape HTML characters, and formats aLocalDate as yyyy-MM-dd.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
Write a GsonTypeAdapter 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?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?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
List the ways to exclude a field from Gson serialization and say when you would use each.A security review found that a User object serialized with Gson in API responses includespasswordHash and internal flags. How do you fix this and prevent it from recurring?Field naming and versioning
The JSON uses first_name while the Java field isfirstName, and older clients send fname. Show how to handle both with Gson.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
This code throwsClassCastException:List<Order> orders = gson.fromJson(json, List.class);
orders.get(0).getId();
<T> List<T> parse(String json) {
return gson.fromJson(json, new TypeToken<List<T>>(){}.getType());
}
JSON tree and streaming parsing
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.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?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
Compare Gson and Jackson for a new Spring Boot service. Which would you choose and why?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.Your company has Gson, Jackson 2, Jackson 3, JSON-B andorg.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
What are Jakarta JSON Processing (JSON-P) and Jakarta JSON Binding (JSON-B), and how do they relate to Jackson and toorg.json?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.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.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
Using JaywayJsonPath, write expressions for this document:{"store":{"book":[{"title":"A","price":8},{"title":"B","price":12}]}}
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?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?No question matches these choices.