AbstractJson

 package com.mridul.software.automation.rest.domain;  
 import com.fasterxml.jackson.annotation.JsonAutoDetect;  
 import com.fasterxml.jackson.annotation.PropertyAccessor;  
 import com.fasterxml.jackson.databind.ObjectMapper;  
 import com.fasterxml.jackson.databind.ObjectWriter;  
 import lombok.extern.slf4j.Slf4j;  
 @Slf4j  
 public abstract class AbstractJson {  
   private static final ObjectMapper MAPPER = new ObjectMapper()  
       .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);  
   private static final ObjectWriter WRITER = MAPPER.writerWithDefaultPrettyPrinter();  
   private String toJson() {  
     String json = null;  
     try {  
       json = WRITER.writeValueAsString(this);  
     } catch (final Exception e) {  
       throw new RuntimeException("Not able to serialize to json : " + e);  
     }  
     return json;  
   }  
   public final <T extends AbstractJson> T fromJson(String json, Class<T> clazz) {  
     T t = null;  
     try {  
       t = MAPPER.readValue(json, clazz);  
     } catch (final Exception e) {  
       throw new RuntimeException("Exception parsing json : " + e);  
     }  
     return t;  
   }  
   @Override  
   public String toString() {  
     return toJson();  
   }  
 }  

Comments

Popular posts from this blog

FileSystemUtils

TakeScreenshotAspect

GemfireConfig