pl.vane.factory.JSONFactory.java Source code

Java tutorial

Introduction

Here is the source code for pl.vane.factory.JSONFactory.java

Source

/*
    
 Copyright (c) 2015-2015, Michal Szczepanski
 All rights reserved.
    
 This source code is licensed under the BSD-style license found in the
 LICENSE file in the root directory of this source tree.
    
 */

package pl.vane.factory;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import pl.vane.log.Log;

import java.io.IOException;

/**
 * Author: Michal Szczepanski
 * Date: 29/03/15
 * Time: 17:32
 */
public class JSONFactory {

    private static final Log log = Log.getLogger(JSONFactory.class.getSimpleName());

    private static final ObjectMapper mapper = new ObjectMapper();

    public static String toJSON(Object value) {
        String out = null;
        try {
            out = mapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            log.error(e);
        }
        return out;
    }

    public static Object toObject(String json, Class clazz) {
        Object out = null;
        try {
            out = mapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e);
        }
        return out;
    }
}