Difference between revisions of "Java Coding Practises"

(Created page with "=ObjectMapper= <syntaxhighlight lang="java"> import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JS...")
 
(No difference)

Latest revision as of 08:32, 26 May 2020

ObjectMapper

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSON {

    private static final ObjectMapper mapper=new ObjectMapper();

    public static <V extends Object> V parse(String str, Class<V> type) {
        V value = null;
        if (str != null) {
            try {
                value = mapper.readValue(str, type);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return value;
    }


    public static String stringify(Object type) {
        String json = null;
        try {
            json = mapper.writeValueAsString(type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}