What is JSON serialization in the context of Jackson?
JSON serialization is the process of converting Java objects into JSON format using Jackson. It allows data to be easily shared or stored in a readable text format.
Click to reveal answer
beginner
Which Jackson annotation is used to ignore a field during JSON serialization?
The @JsonIgnore annotation is used to tell Jackson to skip a field when converting an object to JSON.
Click to reveal answer
intermediate
How do you customize the JSON property name of a Java field using Jackson?
Use the @JsonProperty("name") annotation on the field or getter method to specify a custom name in the JSON output.
Click to reveal answer
beginner
What is the role of ObjectMapper in Jackson?
ObjectMapper is the main class in Jackson used to convert Java objects to JSON and JSON back to Java objects. It handles the serialization and deserialization process.
Click to reveal answer
intermediate
How can you format dates during JSON serialization with Jackson?
You can use the @JsonFormat annotation on date fields to specify the date pattern, like @JsonFormat(pattern = "yyyy-MM-dd") to control how dates appear in JSON.
Click to reveal answer
Which Jackson annotation prevents a field from appearing in JSON output?
A@JsonFormat
B@JsonProperty
C@JsonInclude
D@JsonIgnore
✗ Incorrect
@JsonIgnore tells Jackson to skip the field during serialization.
What does ObjectMapper do in Jackson?
AHandles HTTP requests
BManages database connections
CConverts Java objects to JSON and vice versa
DCompiles Java code
✗ Incorrect
ObjectMapper is the core class for JSON serialization and deserialization.
How do you change the JSON key name of a Java field?
AUse @JsonIgnore
BUse @JsonProperty("newName")
CUse @JsonFormat
DUse @JsonInclude
✗ Incorrect
@JsonProperty allows you to rename the JSON key for a field.
Which annotation helps format date fields in JSON?
A@JsonFormat
B@JsonIgnore
C@JsonProperty
D@JsonInclude
✗ Incorrect
@JsonFormat lets you specify date patterns for JSON output.
What format does Jackson produce when serializing objects?
AJSON
BXML
CYAML
DCSV
✗ Incorrect
Jackson serializes Java objects into JSON format.
Explain how Jackson serializes a Java object to JSON and mention key annotations that control this process.
Think about how you convert objects to text and how to hide or rename fields.
You got /4 concepts.
Describe how to customize the JSON output for date fields using Jackson.
Consider how dates might look different in JSON and how to control that.
You got /3 concepts.
Practice
(1/5)
1. What is the primary purpose of Jackson in a Spring Boot application?
easy
A. To convert Java objects to JSON and vice versa
B. To manage database connections
C. To handle HTTP requests
D. To style HTML pages
Solution
Step 1: Understand Jackson's role
Jackson is a library used to convert Java objects into JSON format and back.
Step 2: Compare options with Jackson's function
Only To convert Java objects to JSON and vice versa describes this conversion role; others relate to different tasks.
Final Answer:
To convert Java objects to JSON and vice versa -> Option A
Quick Check:
Jackson = Java-JSON conversion [OK]
Hint: Jackson is for JSON conversion, not database or UI tasks [OK]
Common Mistakes:
Confusing Jackson with database or HTTP handling
Thinking Jackson styles HTML
Assuming Jackson manages server requests
2. Which of the following is the correct way to serialize a Java object to a JSON string using Jackson's ObjectMapper?
easy
A. objectMapper.readValue(object, String.class);
B. objectMapper.writeValueAsString(object);
C. objectMapper.toJson(object);
D. objectMapper.serialize(object);
Solution
Step 1: Identify the serialization method in ObjectMapper
The method to convert an object to JSON string is writeValueAsString.
Step 2: Check other options for correctness
readValue is for deserialization, toJson and serialize are not valid ObjectMapper methods.
Final Answer:
objectMapper.writeValueAsString(object); -> Option B
Quick Check:
Serialize with writeValueAsString() [OK]
Hint: Use writeValueAsString() to convert object to JSON string [OK]
Common Mistakes:
Using readValue() instead of writeValueAsString()
Assuming toJson() or serialize() exist in ObjectMapper
Mixing serialization and deserialization methods
3. Given the following code snippet, what will be the output?
ObjectMapper mapper = new ObjectMapper();
User user = new User("Alice", 30);
String json = mapper.writeValueAsString(user);
System.out.println(json);
Assuming User class has fields 'name' and 'age' with getters.
medium
A. {"Alice":30}
B. {"userName":"Alice","userAge":30}
C. {"name":"Alice","age":30}
D. Error: Cannot serialize User object
Solution
Step 1: Understand default serialization of User object
Jackson serializes fields with their names and values, so 'name' and 'age' become JSON keys and values.
Step 2: Match output with expected JSON format
{"name":"Alice","age":30} matches the expected JSON string with keys 'name' and 'age' and their values.
Final Answer:
{"name":"Alice","age":30} -> Option C
Quick Check:
Default serialization = field names as keys [OK]
Hint: Default JSON keys match Java field names with getters [OK]
Common Mistakes:
Expecting custom key names without annotations
Assuming serialization fails without extra config
Confusing field names with variable names
4. What is wrong with this code snippet that tries to pretty-print JSON?
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.PRETTY_PRINT);
String json = mapper.writeValueAsString(user);
medium
A. SerializationFeature.PRETTY_PRINT does not exist; should use INDENT_OUTPUT
B. writeValueAsString cannot produce pretty JSON
C. ObjectMapper must be configured with a custom serializer
D. Pretty print requires calling writeValue() instead
Solution
Step 1: Check correct SerializationFeature for pretty print
The correct feature name is INDENT_OUTPUT, not PRETTY_PRINT.
Step 2: Verify usage of writeValueAsString
writeValueAsString supports pretty printing if INDENT_OUTPUT is enabled.
Final Answer:
SerializationFeature.PRETTY_PRINT does not exist; should use INDENT_OUTPUT -> Option A
Quick Check:
Use INDENT_OUTPUT for pretty print [OK]
Hint: Use SerializationFeature.INDENT_OUTPUT for pretty JSON [OK]
Common Mistakes:
Using wrong feature name PRETTY_PRINT
Thinking writeValueAsString can't pretty print
Believing custom serializers are always needed
5. You want to serialize a Java object to JSON but exclude null fields from the output. Which Jackson configuration achieves this?
hard
A. mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
B. mapper.disable(SerializationFeature.WRITE_NULLS);
C. mapper.enable(SerializationFeature.EXCLUDE_NULLS);
D. mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Solution
Step 1: Identify correct method to exclude null fields
Jackson uses setSerializationInclusion with JsonInclude.Include.NON_NULL to skip nulls.
Step 2: Check other options for validity
WRITE_NULLS and EXCLUDE_NULLS are not valid features; ALWAYS includes all fields.
Final Answer:
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); -> Option D
Quick Check:
Exclude nulls with NON_NULL inclusion [OK]
Hint: Use NON_NULL inclusion to skip null fields [OK]
Common Mistakes:
Using non-existent SerializationFeature flags
Confusing ALWAYS with excluding nulls
Trying to disable null writing with wrong methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.