[go: up one dir, main page]

DEV Community

Cover image for ๐Ÿ’พ Java Records ๐Ÿ’ฟ with Jackson 2.12
Carlos Chacin โ˜•๐Ÿ‘ฝ
Carlos Chacin โ˜•๐Ÿ‘ฝ

Posted on • Originally published at carloschac.in on

๐Ÿ’พ Java Records ๐Ÿ’ฟ with Jackson 2.12

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>

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’พ 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) {
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก 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();
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”จ Our test setup:

final ObjectMapper mapper = new ObjectMapper()
      .enable(SerializationFeature.INDENT_OUTPUT);

Enter fullscreen mode Exit fullscreen mode

To serialize/deserialize a record like this:

var person = new Person(
    "John",
    "Doe",
    "USA",
    new Date(981291289182L),
    List.of("Speaker")
);

Enter fullscreen mode Exit fullscreen mode

To/From a json string like this:

{
  "first_name" : "John",
  "last_name" : "Doe",
  "address" : "USA",
  "birthday" : 981291289182,
  "achievements" : ["Speaker"]
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’Š 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);
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’Š 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);
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”† Conclusions

  • โœ… We can start using Jackson >= 2.12.0 to serialize/deserialize Java Records.
  • ๐Ÿ‘ท This is a new feature, and there can be edge cases, try it out with caution.
  • ๐Ÿž Report issues here if you find any

Top comments (0)