protected AbstractJacksonSerializer(ObjectMapper mapper) { this.mapper = mapper; init(); }
protected void init() { mapper.addMixInAnnotations(Object.class, PropertyFilterMixIn.class); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE); mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.SETTER, Visibility.PROTECTED_AND_PUBLIC); registerModules(mapper); mapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true); mapper.setPropertyNamingStrategy(getPropertyNamingStrategy()); SimpleFilterProvider provider = new SimpleFilterProvider(); provider.addFilter("property_filter", SimpleBeanPropertyFilter.serializeAllExcept(Sets.<String>newHashSet())); mapper.setFilters(provider); }
public <T> T deserialize(InputStream stream, Class<T> targetClass) { try { return mapper.readValue(stream, targetClass); } catch (Exception e) { throw new SerializationException("Failed while deserializing the buffer to type - " + targetClass, e); } finally { closeStream(stream); } }
@Override public void serialize(Object object, OutputStream stream) { try { mapper.writeValue(stream, object); } catch (Exception e) { logger.error("Unable to serialize object", e); throw new SerializationException("Failed while serializing the object", e); } finally { closeStream(stream); } } }
@Override public <T extends Collection<E>, E> T deserializeCollection(InputStream stream, Class<T> collectionType, Class<E> elementType) { JavaType javaType = mapper.getTypeFactory().constructCollectionType(collectionType, elementType); try { return mapper.readValue(stream, javaType); } catch (Exception e) { throw new SerializationException("Failed while deserializing the buffer to type - " + javaType, e); } finally { closeStream(stream); } }