0
0
Spring Bootframework~20 mins

JSON serialization with Jackson in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Jackson JSON Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the JSON output of this Spring Boot controller method?

Consider a Spring Boot REST controller method that returns a simple POJO. What JSON will be sent to the client?

Spring Boot
public record User(String name, int age) {}

@GetMapping("/user")
public User getUser() {
    return new User("Alice", 30);
}
A{"user":{"name":"Alice","age":30}}
B{"name":"Alice","age":"30"}
C{"Name":"Alice","Age":30}
D{"name":"Alice","age":30}
Attempts:
2 left
💡 Hint

Jackson uses field names as JSON keys by default and serializes numbers as numbers.

📝 Syntax
intermediate
2:00remaining
Which option correctly configures Jackson to ignore null fields in JSON output?

You want to configure Jackson in Spring Boot to skip null fields when serializing objects. Which code snippet achieves this?

A
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}
B
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
C
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.ALWAYS);
}
D
@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper().enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}
Attempts:
2 left
💡 Hint

Jackson's setSerializationInclusion controls which fields are included.

🔧 Debug
advanced
2:00remaining
Why does this Jackson serialization produce an empty JSON object?

Given this class and controller, why does the JSON output show {} instead of the expected fields?

Spring Boot
public class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() { return name; }
    public double getPrice() { return price; }
}

@GetMapping("/product")
public Product getProduct() {
    return new Product("Book", 12.99);
}
ABecause the fields are private and there are no getters, Jackson cannot access them.
BBecause the class is missing the @JsonSerializable annotation.
CBecause the price field is a primitive type and cannot be serialized.
DBecause the controller method does not return ResponseEntity.
Attempts:
2 left
💡 Hint

Jackson needs a way to read field values, usually via getters or public fields.

🧠 Conceptual
advanced
2:00remaining
What is the effect of using @JsonIgnoreProperties on a class?

In Jackson, what does adding @JsonIgnoreProperties({"field1", "field2"}) to a class do?

AIt renames the specified fields in the JSON output.
BIt marks the specified fields as required during deserialization.
CIt prevents the specified fields from being included in JSON serialization and deserialization.
DIt makes the specified fields transient and skips them only during serialization.
Attempts:
2 left
💡 Hint

Think about ignoring fields in both directions: reading and writing JSON.

state_output
expert
3:00remaining
What JSON output results from this custom serializer in Spring Boot?

Given this custom serializer registered for a class, what JSON will be produced?

Spring Boot
public class Color {
    private int red, green, blue;
    public Color(int r, int g, int b) { red = r; green = g; blue = b; }
    public int getRed() { return red; }
    public int getGreen() { return green; }
    public int getBlue() { return blue; }
}

public class ColorSerializer extends StdSerializer<Color> {
    public ColorSerializer() { super(Color.class); }
    @Override
    public void serialize(Color value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        String hex = String.format("#%02X%02X%02X", value.getRed(), value.getGreen(), value.getBlue());
        gen.writeString(hex);
    }
}

// Registered in ObjectMapper

@GetMapping("/color")
public Color getColor() {
    return new Color(255, 165, 0);
}
A"#FFA500"
B{"red":255,"green":165,"blue":0}
C"rgb(255,165,0)"
D{"color":"#FFA500"}
Attempts:
2 left
💡 Hint

The custom serializer writes a string with the hex color code.