Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JsonIgnore instead of @JsonProperty will hide the field from JSON.
Confusing @JsonInclude with @JsonProperty.
✗ Incorrect
The @JsonProperty annotation is used to specify the JSON property name for a Java field or method.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @JsonProperty instead of @JsonIgnore will not hide the field.
Forgetting to import the correct annotation.
✗ Incorrect
The @JsonIgnore annotation tells Jackson to skip the annotated field during serialization and deserialization.
3fill in blank
hardFix 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'
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.
✗ Incorrect
Using Include.NON_NULL tells Jackson to serialize only fields that are not null.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using shape=NUMBER will serialize date as timestamp.
Using wrong date pattern will cause parsing errors.
✗ Incorrect
Using shape=STRING and pattern="yyyy-MM-dd" formats the date as a string in the specified pattern.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names instead of class names in the annotation.
Mismatching class name between annotation and class declaration.
✗ Incorrect
The custom serializer class is named PasswordSerializer and is used in the @JsonSerialize annotation and class declaration.