Creating own parse and stringify method for JSON using Jackson?



parse () method

This method converts a JSON string to a Java object. 
In the argument (Class dto, String json),  pass the Java object class and the JSON string you want to convert. 
parse (Java object, JSON string) will convert JSON string to Java object and Java object will be returned.

stringify () method

This method simply generates a JSON string from a Java object by passing a Java object as an argument.


package com.fasterxml.jackson.core.jackson_core;import java.io.IOException;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;public class JsonUtil {     /**     * Convert JSON string to DTO class     * @param dto     * @param json     * @return DTOobject     * @throws IOException     */     public static <T> T parse(Class<T> dto, String json){          ObjectMapper mapper = new ObjectMapper();          try{               return (T) mapper.readValue(json, dto);          }catch(IOException e){               return null;          }     }     /**     * Convert instance of DTO class to JSON string     * @param dto     * @return String     * @throws JsonProcessingException     */     public static String stringify(Object dto){          ObjectMapper mapper = new ObjectMapper();          try{               String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dto);               return json;          }catch(JsonProcessingException e){               return null;          }     }}