How to convert json that contains LocalDate field to deserializable format?

I have an object stored in String. One of the fields of the object is LocalDate.

"from": {
        "year": 1000,
        "month": "JANUARY",
        "era": "CE",
        "dayOfMonth": 1,
        "dayOfWeek": "WEDNESDAY",
        "dayOfYear": 1,
        "leapYear": false,
        "monthValue": 1,
        "chronology": {
            "calendarType": "iso8601",
            "id": "ISO"
        }
    }

How do I convert this json to a format that can be used for deserialization?

Following code

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;


ObjectMapper om = new ObjectMapper();
om.registerModule(new JavaTimeModule());
MyCustomObject obj = om.readValue(json, MyCustomObject.class); //error

throws this exception:

com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string.

Here's the MyCustomObject class, which I use as MyCustomObject <LocalDate>

public class MyCustomObject<T> {
    private T from;
    private T to;

    public MyCustomObject() {
    }

    public T getFrom() {
        return this.from;
    }

    public void setFrom(T from) {
        this.from = from;
    }

    public T getTo() {
        return this.to;
    }

    public void setTo(T to) {
        this.to = to;
    }
}
+4
source share
2 answers

LocalDate stored incorrectly in JSON.

The correct way to fix the problem is to ensure the ObjectMappercreation of JSON JavaTimeModuleor Jdk8Module. This ensures that it is LocalDatecorrectly serialized to a JSON array [year, month, day].

, @Roy , LocalDate. - :

LocalDate date = LocalDate.now()
        .with(ChronoField.YEAR, year)
        .with(ChronoField.MONTH_OF_YEAR, Month.valueOf(month).getValue())
        .with(ChronoField.DAY_OF_MONTH, dayOfMonth);

, , , , .

0

MyCustomObject.class :

==================================
package ;
public class Chronology
{
    private String calendarType;

    private String id;

    public void setCalendarType(String calendarType){
        this.calendarType = calendarType;
    }
    public String getCalendarType(){
        return this.calendarType;
    }
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
}

==================================
package ;
public class From
{
    private int year;

    private String month;

    private String era;

    private int dayOfMonth;

    private String dayOfWeek;

    private int dayOfYear;

    private boolean leapYear;

    private int monthValue;

    private Chronology chronology;

    public void setYear(int year){
        this.year = year;
    }
    public int getYear(){
        return this.year;
    }
    public void setMonth(String month){
        this.month = month;
    }
    public String getMonth(){
        return this.month;
    }
    public void setEra(String era){
        this.era = era;
    }
    public String getEra(){
        return this.era;
    }
    public void setDayOfMonth(int dayOfMonth){
        this.dayOfMonth = dayOfMonth;
    }
    public int getDayOfMonth(){
        return this.dayOfMonth;
    }
    public void setDayOfWeek(String dayOfWeek){
        this.dayOfWeek = dayOfWeek;
    }
    public String getDayOfWeek(){
        return this.dayOfWeek;
    }
    public void setDayOfYear(int dayOfYear){
        this.dayOfYear = dayOfYear;
    }
    public int getDayOfYear(){
        return this.dayOfYear;
    }
    public void setLeapYear(boolean leapYear){
        this.leapYear = leapYear;
    }
    public boolean getLeapYear(){
        return this.leapYear;
    }
    public void setMonthValue(int monthValue){
        this.monthValue = monthValue;
    }
    public int getMonthValue(){
        return this.monthValue;
    }
    public void setChronology(Chronology chronology){
        this.chronology = chronology;
    }
    public Chronology getChronology(){
        return this.chronology;
    }
}

==================================
package ;
public class MyCustomObject
{
    private From from;

    public void setFrom(From from){
        this.from = from;
    }
    public From getFrom(){
        return this.from;
    }
}
+1

Source: https://habr.com/ru/post/1676757/


All Articles