com.liscs.server.utils.JacksonUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.liscs.server.utils.JacksonUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.liscs.server.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Just1689
 */
public class JacksonUtils<T> {

    public T stringToObject(String json, Class clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            T result = (T) objectMapper.readValue(json, clazz);
            return result;
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String objectToString(T t) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(t);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(JacksonUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}