0
0
Spring Bootframework~10 mins

JSON serialization with Jackson in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate a Java class for JSON serialization using Jackson.

Spring Boot
import com.fasterxml.jackson.annotation.[1];

public class User {
    private String name;
    private int age;

    @JsonProperty("user_name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
Drag options to blanks, or click blank then click option'
AJsonInclude
BJsonIgnore
CJsonProperty
DJsonFormat
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JsonIgnore instead of @JsonProperty will hide the field from JSON.
Confusing @JsonInclude with @JsonProperty.
2fill in blank
medium

Complete the code to ignore a field during JSON serialization.

Spring Boot
import com.fasterxml.jackson.annotation.[1];

public class Product {
    private String name;

    @JsonIgnore
    private double cost;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }
}
Drag options to blanks, or click blank then click option'
AJsonIgnore
BJsonProperty
CJsonInclude
DJsonFormat
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JsonProperty instead of @JsonIgnore will not hide the field.
Forgetting to import the correct annotation.
3fill in blank
hard

Fix the error in the code to include non-null fields only during JSON serialization.

Spring Boot
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.[1])
public class Order {
    private String id;
    private String description;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
ANON_NULL
BALWAYS
CNON_EMPTY
DNON_DEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Using ALWAYS will include all fields, even null ones.
Using NON_EMPTY excludes empty collections too, which may not be desired.
4fill in blank
hard

Fill both blanks to format a date field in JSON serialization.

Spring Boot
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;

public class Event {
    @JsonFormat(shape = JsonFormat.Shape.[1], pattern = "[2]")
    private Date eventDate;

    // getters and setters
}
Drag options to blanks, or click blank then click option'
ASTRING
Byyyy-MM-dd
Cyyyy/MM/dd
DNUMBER
Attempts:
3 left
💡 Hint
Common Mistakes
Using shape=NUMBER will serialize date as timestamp.
Using wrong date pattern will cause parsing errors.
5fill in blank
hard

Fill all three blanks to create a custom serializer for a field.

Spring Boot
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;

public class User {
    @JsonSerialize(using = [1].class)
    private String password;

    // getters and setters
}

class [2] extends StdSerializer<String> {
    public [3]() {
        super(String.class);
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeString("***hidden***");
    }
}
Drag options to blanks, or click blank then click option'
APasswordSerializer
Bserialize
CwriteString
DPasswordEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names instead of class names in the annotation.
Mismatching class name between annotation and class declaration.