Practical Mapping Examples
Basic Object Serialization and Deserialization
public class User {
private String name;
private int age;
// Constructors, getters, setters
}
public class JsonMappingDemo {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Serialization
User user = new User("Alice", 30);
String jsonString = mapper.writeValueAsString(user);
System.out.println("Serialized JSON: " + jsonString);
// Deserialization
User parsedUser = mapper.readValue(jsonString, User.class);
System.out.println("Deserialized User: " + parsedUser.getName());
}
}
Handling Complex Objects
graph TD
A[Complex Object] --> B[Nested Objects]
A --> C[Collections]
A --> D[Polymorphic Types]
Nested Object Mapping
public class Address {
private String street;
private String city;
}
public class Employee {
private String name;
private Address address;
private List<String> skills;
}
public class NestedObjectExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Employee employee = new Employee();
employee.setName("John Doe");
Address address = new Address();
address.setStreet("123 Main St");
address.setCity("Techville");
employee.setAddress(address);
employee.setSkills(Arrays.asList("Java", "Python"));
String jsonString = mapper.writeValueAsString(employee);
System.out.println("Nested Object JSON: " + jsonString);
}
}
Handling Date and Time
Java Type |
JSON Representation |
LocalDate |
"2023-06-15" |
LocalDateTime |
"2023-06-15T10:30:00" |
ZonedDateTime |
"2023-06-15T10:30:00+01:00" |
public class DateMappingExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
LocalDate date = LocalDate.now();
String jsonDate = mapper.writeValueAsString(date);
System.out.println("Formatted Date: " + jsonDate);
}
}
Conditional Serialization
public class ConditionalSerializationExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Exclude null values
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Ignore unknown properties during deserialization
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
}
Error Handling Strategies
public class ErrorHandlingExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
String invalidJson = "{\"name\": \"John\", \"age\": \"invalid\"}";
User user = mapper.readValue(invalidJson, User.class);
} catch (JsonMappingException e) {
System.err.println("Mapping error: " + e.getMessage());
} catch (JsonProcessingException e) {
System.err.println("Processing error: " + e.getMessage());
}
}
}
LabEx Pro Tip
When working with complex JSON mappings, always validate your object models and use appropriate configuration to handle various scenarios efficiently.
- Reuse
ObjectMapper
instances
- Use appropriate inclusion strategies
- Minimize runtime configuration changes