dev-resources.site
for different kinds of informations.
๐พ Java Records ๐ฟ with Jackson 2.12
Published at
3/4/2021
Categories
jackson
java14
records
serialization
Author
cchacin
Author
7 person written this
cchacin
open
In the previous article about Java 14 Records, we saw how to start creating Records to avoid writing much boilerplate code that the compiler would generate for us.
Now the next steps are to see how we can serialize records to JSON and deserialize JSON to records to be able to use them as a request/response representation for microservices.
In this case, we would use the Jackson 2.12+.
Continuing with the same example that we used in the previous article, we would need to add Jackson Dependencies to our existing pom.xml file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
๐พ Now letโs see our example Record:
record Person(
@JsonProperty("first_name") String firstName,
@JsonProperty("last_name") String lastName,
String address,
Date birthday,
List<String> achievements) {
}
๐ก Java 14+ compiler would generate all of the following:
$ javap -p Person.class
final class Person extends java.lang.Record {
private final java.lang.String firstName;
private final java.lang.String lastName;
private final java.lang.String address;
private final java.util.Date birthday;
private final java.util.List<java.lang.String> achievements;
public Person(
java.lang.String,
java.lang.String,
java.lang.String,
java.util.Date,
java.util.List<java.lang.String>);
public final java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public java.lang.String firstName();
public java.lang.String lastName();
public java.lang.String address();
public java.util.Date birthday();
public java.util.List<java.lang.String> achievements();
}
๐จ Our test setup:
final ObjectMapper mapper = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT);
To serialize/deserialize a record like this:
var person = new Person(
"John",
"Doe",
"USA",
new Date(981291289182L),
List.of("Speaker")
);
To/From a json string like this:
{
"first_name" : "John",
"last_name" : "Doe",
"address" : "USA",
"birthday" : 981291289182,
"achievements" : ["Speaker"]
}
๐ Testing Serialization
@Test
void serializeRecord() throws Exception {
// Given
var person = new Person(
"John",
"Doe",
"USA",
new Date(981291289182L),
List.of("Speaker")
);
var json = """
{
"first_name" : "John",
"last_name" : "Doe",
"address" : "USA",
"birthday" : 981291289182,
"achievements" : ["Speaker"]
}""";
// When
var serialized = mapper.writeValueAsString(person);
// Then
assertThat(serialized).isEqualTo(json);
}
๐ Testing Deserialization
Letโs use the same record to try the deserialization using also the same configuration:
@Test
void deserializeRecord() throws Exception {
// Given
var person = new Person(
"John",
"Doe",
"USA",
new Date(981291289182L),
List.of("Speaker")
);
var json = """
{
"first_name" : "John",
"last_name" : "Doe",
"address" : "USA",
"birthday" : 981291289182,
"achievements" : ["Speaker"]
}""";
// When
var deserialized = mapper.readValue(json, Person.class);
// Then
assertThat(deserialized).isEqualTo(person);
}
๐ Conclusions
jackson Article's
30 articles in total
Why Do We Still Need Jackson or Gson in Java?
read article
A simple GeoJSON serializer for Jackson
read article
[Java Spring Boot] Como Criar Serializador Personalizado para seus Responses ou Json de saรญda
read article
[Java Spring Boot] How to implement a Custom Serializer for your Responses or Json
read article
[Java SpringBoot] Como Criar Deserializador Personalizado para seus Requests
read article
[Java SpringBoot] How to implement a Custom Deserializer for your Requests
read article
Java Jackson JSON: How to Handle Custom Keys?
read article
Create a custom Jackson JsonSerializer und JsonDeserializer for mapping values
read article
Anotaciรณn @JsonUnwrapped
read article
A tale of fixing a tiny OpenAPI bug
read article
Kotlin Springboot -- Part 21 ไปปๆใฎ key value ใฎ json ใ POST ใใ API E2E ใๆธใ
read article
Formatting json Date/LocalDateTime/LocalDate in Spring Boot
read article
Jackson's @JsonView with SpringBoot Tutorial
read article
Jackson JSON parsing top-level map into records
read article
Using Jackson Subtypes to Write Better Code
read article
Java โ Convert Excel File to/from JSON (String/File) โ using Apache Poi + Jackson
read article
How to resolve Json Infinite Recursion problem when working with Jackson
read article
Java โ Convert Excel File to/from JSON (String/File) โ using Apache Poi + Jackson
read article
Practical Java 16 - Using Jackson to serialize Records
read article
Kotlin โ Convert Object to/from JSON with Jackson 2.x
read article
๐พ Java Records ๐ฟ with Jackson 2.12
currently reading
Jackson, JSON and the Proper Handling of Unknown Fields in APIs
read article
Polymorphic deserialization with Jackson and no annotations
read article
Playing around with Kotlin Sealed Classes
read article
Moonwlker: JSON without annotation
read article
Jackson Readonly properties and swagger UI
read article
Registering Jackson sub-types at runtime in Kotlin
read article
Parsing JSON in Spring Boot, part 1
read article
Customize how Jackson does LocalDate Parsing
read article
Painless JSON with Kotlin and jackson
read article
Featured ones: