All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.lc.oss.commons.web.services.JsonService Maven / Gradle / Ivy

package io.github.lc.oss.commons.web.services;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import io.github.lc.oss.commons.serialization.Jsonable;

public class JsonService implements io.github.lc.oss.commons.api.services.JsonService {
    private static final ObjectWriter WRITER = new ObjectMapper().writer();
    private static final ObjectReader READER = new ObjectMapper().reader();

    public  T from(String json, Class clazz) {
        if (json == null || json.trim().equals("")) {
            return null;
        }

        try {
            return JsonService.READER.readValue(json, clazz);
        } catch (IOException ex) {
            throw new RuntimeException("Error deseralizing from JSON", ex);
        }
    }

    public String to(Jsonable object) {
        try {
            return JsonService.WRITER.writeValueAsString(object);
        } catch (JsonProcessingException ex) {
            throw new RuntimeException("Error seralizing to JSON", ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy