Java Coding Practises

Revision as of 07:32, 26 May 2020 by Rasimsen (talk | contribs) (Created page with "=ObjectMapper= <syntaxhighlight lang="java"> import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JS...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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;
    }
}