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

com.jslsolucoes.jax.rs.provider.se.ObjectMapper Maven / Gradle / Ivy

There is a newer version: 1.0.32
Show newest version
package com.jslsolucoes.jax.rs.provider.se;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;

import javax.enterprise.inject.Vetoed;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

@Vetoed
public class ObjectMapper {

    public com.fasterxml.jackson.databind.ObjectMapper objectMapper;

    public ObjectMapper() {

    }

    public ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) {
	this.objectMapper = objectMapper;
    }
    
    
    public void serialize(OutputStream outputStream,Object object, Map, Set> exclusions) {
	try {
	    objectMapper.writer(filterProvider(exclusions)).writeValue(outputStream,object);
	} catch (IOException e) {
	    throw new RuntimeException(e);
	}
    }

    public  T deserialize(String json, Class clazz) throws JsonParseException, JsonMappingException, IOException {
	   return objectMapper.readValue(json, clazz);
    }
    
    public String serialize(Object object) throws JsonProcessingException {
	return serialize(object,null);
    }

    public String serialize(Object object, Map, Set> exclusions) throws JsonProcessingException {
	 return objectMapper.writer(filterProvider(exclusions)).writeValueAsString(object);
    }

    private FilterProvider filterProvider(Map, Set> exclusions) {
	if (exclusions != null) {
	    return hibernateFilterProvider(exclusions);
	} else {
	    return defaultFilterProvider();
	}
    }

    private HibernateExclusionFilterProvider hibernateFilterProvider(Map, Set> exclusions) {
	return new HibernateExclusionFilterProvider(exclusions);
    }

    private FilterProvider defaultFilterProvider() {
	return new SimpleFilterProvider().addFilter(JsonExcludeMixIn.KEY, SimpleBeanPropertyFilter.serializeAll());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy