Moshi java.time.LocalDate requires explicit JsonAdapter to be registered

Moshi does not have built-in adapters to process types like Java 8 date time APIs java.time.* or JSR-310; we need to manually provide a custom adapter to handle the Java 8 date time type.

This article shows how to use Moshi to convert the Java 8 Localdate type to JSON data.

Table of contents:

P.S Tested with Moshi 1.15.1

1. Download Moshi

Declare moshi in the pom.xml.

pom.xml

    <dependency>
        <groupId>com.squareup.moshi</groupId>
        <artifactId>moshi</artifactId>
        <version>1.15.1</version>
    </dependency>

2. LocalDate requires explicit JsonAdapter

The following Book object contains the type Java 8 date API java.time.LocalDate.

Person.java

package com.mkyong.json.model;

import java.time.LocalDate;

public class Book {

    private Long id;
    private String title;
    private LocalDate publishDate; // java 8

    // getters, setters, constructors and etc.
}

If we use Moshi to serialize the object into JSON, Moshi will throw the error java.time.LocalDate requires explicit JsonAdapter to be registered.


    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Book> jsonAdapter = moshi.adapter(Book.class);
        
    Book book = createBook();

    String json = jsonAdapter.toJson(book); // throws error 

Caused by: java.lang.IllegalArgumentException: 
    Platform class java.time.LocalDate requires explicit JsonAdapter to be registered

	at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:76)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
	... 6 more

3. Make Moshi supports Java 8 Date Time APIs

We need to create and register a custom JsonAdapter to handle the serialization and deserialization of LocalDate objects.

3.1 Define LocalDate Adapter

Create logic to handle the LocalDate. The ISO-8601 format (uuuu-MM-dd) is commonly used and is the default format for LocalDate.toString().

BigDecimalJsonAdapter.java

package com.mkyong.json.moshi.adapter;

import com.squareup.moshi.FromJson;
import com.squareup.moshi.ToJson;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateJsonAdapter {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;

    // Serialize the LocalDate to String
    @ToJson
    String toJson(LocalDate date) {
        return date.format(FORMATTER); 
    }

    // Parse the String back to a LocalDate
    @FromJson
    LocalDate fromJson(String date) {
        return LocalDate.parse(date, FORMATTER); 
    }

}

3.2 Register the Adapter with Moshi

The following example will register a LocalDateJsonAdapter and convert the type LocalDate to JSON.

MoshiJava8DateTimeExample.java

package com.mkyong.json.moshi;

import com.mkyong.json.model.Book;
import com.mkyong.json.moshi.adapter.BigDecimalJsonAdapter;
import com.mkyong.json.moshi.adapter.LocalDateJsonAdapter;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

import java.math.BigDecimal;
import java.time.LocalDate;

public class MoshiJava8DateTimeExample {

    public static void main(String[] args) {

        Moshi moshi = new Moshi.Builder()
                .add(new LocalDateJsonAdapter()) // Register the LocalDate adapter
                .build();

        JsonAdapter<Book> jsonAdapter = moshi.adapter(Book.class);

        Book book =
                new Book(1L, "Book A", LocalDate.of(2023, 10, 1));

        try {

            String json = jsonAdapter.toJson(book);
            System.out.println(json);

        } catch (Exception e) {
            System.err.println("Error parsing JSON: " + e.getMessage());
        }

    }
}

Output


{"id":1,"publishDate":"2023-10-01","title":"Book A"}

4. Download Source Code

$ git clone https://github.com/mkyong/java-json

$ cd moshi

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments