0

So I'm working on a portable application for my capstone project and I've kind of hit a dead end. My project is a reminder application that is supposed to push a message to the user on an inputted date and time.

So far I've been able to save TO a json but I have issues with getting the information back out again. So my question is, how do I format the LocalDateTime when it goes into the Json file so that I can get it back out again?

Here's the exception I get when I try to get from the json I just wrote to.

2 Exception Occured while converting Json into Java --> Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"eventName":"Test","repeatEvent":false,"repeatValue":0,"repeatType":"Min","message":"Test". {"nano":0,"year":2020,"monthValue":12,"dayOfMonth":25,"hour":21,"minute":30,"second":0,"dayOfWeek":"FRIDAY","dayOfYear":360,"month":"DECEMBER","chronology":{"id":"ISO","calendarType":"iso8601"}}}"; line: 1, column: 129] (through reference chain: bean.Event["reminderDateTime"])

Here's my Util class in case it's useful. I tried the @JsonFormat but it doesn't seem to work.

package util;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    private static ObjectMapper mapper = new ObjectMapper();
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")

    public static String convertJavaToJson(Object object)
    {
        String jsonResult = "";
        try {
            jsonResult = mapper.writeValueAsString(object);
        } 
        catch (JsonGenerationException e) {
            System.out.println("1 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        catch (JsonMappingException e) {
            System.out.println("2 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        catch (IOException e) {
            System.out.println("3 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        catch (Exception e) {
            System.out.println("4 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        return jsonResult;
    }

    public static <T> T convertJsonToJava(String jsonString, Class<T> cls) {
        T result = null;
        try {
            result = mapper.readValue(jsonString, cls);
        } 
        catch (JsonParseException e){
            System.out.println("1 Exception Occured while converting Json into Java --> " + e.getMessage());
        } 
        catch (JsonMappingException e) {
            System.out.println("2 Exception Occured while converting Json into Java --> " + e.getMessage());
        } 
        catch (IOException e) {
            System.out.println("3 Exception Occured while converting Json into Java --> " + e.getMessage());
        } 
        catch (Exception e) {
            System.out.println("4 Exception Occured while converting Json into Java --> " + e.getMessage());
        }
        return result;
    }
}

EDIT: Here's the main class that calls the JsonToJava function. It included the calling of my custom class Event. the ent1.toString() errors out because of some null pointer Exception

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

import bean.Event;
import util.JsonUtil;

public class main {

    public static void main(String[] args) {

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM dd YYYY HH:mm a");

        String Test = "Test";
        LocalDateTime Time = LocalDateTime.of(2020, Month.DECEMBER, 25, 21, 30);

        Event testEvent3 = new Event (Test, false, 0, Time, Test);
        System.out.println(testEvent3.toString());
        System.out.println();

        String JsonTime = JsonUtil.convertJavaToJson(testEvent3);
        System.out.println(JsonTime);

        System.out.println("----------------------------");

        Event ent1 = JsonUtil.convertJsonToJava(JsonTime, Event.class);
        System.out.println(ent1.toString());
    }

}

0 Answers0