protected AbstractJacksonSerializer(ObjectMapper mapper) { this.mapper = mapper; init(); }
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 protected void init() { getMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); getMapper().setSerializationInclusion(Include.NON_EMPTY); getMapper().setSerializationInclusion(Include.NON_NULL); registerModules(getMapper()); }
public abstract <T> T deserialize(InputStream stream, Class<T> targetClass);
@SuppressWarnings("rawtypes") protected User retrieveProfile(Session session) { Object profile = session.getAttribute(PRINCIPAL); if (profile == null) { return null; } Client client = getClient(session); Class<UserProfile> type = Generics.getTypeParameter(client.getClass(), UserProfile.class); if (type.isAssignableFrom(profile.getClass())) { return new User((UserProfile) profile); } if (profile instanceof Map) { String buffer = Serializer.DEFAULT_JSON_SERIALIZER.serialize(profile); profile = Serializer.DEFAULT_JSON_SERIALIZER.deserialize(buffer, type); User user = new User((UserProfile) profile); session.addAttribute(PRINCIPAL, profile); return user; } // Can't come here return null; }
void encodeData() { String content = Serializer.DEFAULT_JSON_SERIALIZER.serialize(attributes); data = BaseEncoding.base64().encode(content.getBytes(Charsets.UTF_8)); }
@Override public void serialize(Object object, OutputStream stream) { try { stream.write(object.toString().getBytes(Charsets.UTF_8)); } catch (Exception e) { throw new SerializationException("Failed while serializing the object", e); } finally { closeStream(stream); } } }
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 abstract <T extends Collection<E>, E> T deserializeCollection(InputStream stream, Class<T> collectionType, Class<E> elementType);
public <T> T deserialize(InputStream stream, Class<T> targetClass) { if (! targetClass.equals(String.class)) { throw new IllegalArgumentException("Target class is not string"); } try { InputStreamReader reader = new InputStreamReader(stream, Charsets.UTF_8); return (T) CharStreams.toString(reader); } catch (Exception e) { throw new SerializationException("Failed while deserializing the buffer to type - " + targetClass, e); } }
@SuppressWarnings("unchecked") @Override public <T> T deserialize(InputStream stream, Class<T> targetClass) { if (! targetClass.isAssignableFrom(Map.class)) { throw new IllegalArgumentException("Target class is not a map"); } String data = super.deserialize(stream, String.class); return (T) Splitter.on("&").omitEmptyStrings().withKeyValueSeparator("=").split(data); }
void decodeData(String data) { byte[] bytes = BaseEncoding.base64().decode(data); setAttributes(Serializer.DEFAULT_JSON_SERIALIZER.deserialize(new String(bytes, Charsets.UTF_8), Map.class)); }
public abstract void serialize(Object object, OutputStream 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); } } }
public <T extends Configuration> T provide(Class<T> clazz, String path) { InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); try { return Serializer.DEFAULT_YAML_SERIALIZER.deserialize(is, clazz); } catch (Exception e) { logger.error("Failed while reading the config file - " + path, e); throw new MinnalException(e); } }
protected void serializeTo(File file, Object content, Serializer serializer) { FileOutputStream stream = null; try { stream = new FileOutputStream(file); serializer.serialize(content, stream); } catch (Exception e) { throw new MinnalGeneratorException("Failed while writing the file " + file.getAbsolutePath(), e); } finally { if (stream != null) { 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); } }
protected <T> T deserializeFrom(File file, Serializer serializer, Class<T> clazz) { if (!file.exists()) { return null; } InputStream stream = null; try { stream = new FileInputStream(file); return serializer.deserialize(stream, clazz); } catch (Exception e) { throw new MinnalGeneratorException("Failed while creating the file " + file.getAbsolutePath()); } finally { closeStream(stream); } }