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

com.spotinst.sdkjava.client.rest.JsonMapper Maven / Gradle / Ivy

There is a newer version: 1.0.121
Show newest version
package com.spotinst.sdkjava.client.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by aharontwizer on 7/22/15.
 */
public class JsonMapper {

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonMapper.class);

    private static ObjectMapper jsonMapper = new ObjectMapper();

    static {

        PartialUpdateEntityPropertyFilter propertyFilter = new PartialUpdateEntityPropertyFilter();
        FilterProvider filters = new SimpleFilterProvider().addFilter("PartialUpdateEntityFilter", propertyFilter);

        // Set the filters.
        jsonMapper.setFilterProvider(filters);

    }

    public static  T fromJson(String content, Class contentClass) {
        T retVal = null;

        try {
            retVal = jsonMapper.readValue(content, contentClass);
        } catch (IOException e) {

            LOGGER.error("Error in parsing json to object", e);
        }

        return retVal;
    }

    public static  T fromJson(String content, TypeReference referenceClass) {
        T retVal = null;

        try {
            retVal = jsonMapper.readValue(content, referenceClass);
        } catch (IOException e) {
            LOGGER.error("Error in parsing json to object", e);
        }

        return retVal;
    }

    public static  String toJson(T objectToWrite) {
        String retVal = null;


        try {
            retVal = jsonMapper.writeValueAsString(objectToWrite);
        } catch (JsonProcessingException e) {
            LOGGER.error("Error in writing object to json", e);
        }

        return retVal;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy