com.octo.android.robospice.persistence.springandroid.json.jackson2.Jackson2ObjectPersister Maven / Gradle / Ivy
package com.octo.android.robospice.persistence.springandroid.json.jackson2;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import android.app.Application;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.octo.android.robospice.persistence.exception.CacheCreationException;
import com.octo.android.robospice.persistence.exception.CacheLoadingException;
import com.octo.android.robospice.persistence.exception.CacheSavingException;
import com.octo.android.robospice.persistence.springandroid.SpringAndroidObjectPersister;
public final class Jackson2ObjectPersister extends SpringAndroidObjectPersister {
// ============================================================================================
// ATTRIBUTES
// ============================================================================================
private final ObjectMapper mJsonMapper;
// ============================================================================================
// CONSTRUCTOR
// ============================================================================================
public Jackson2ObjectPersister(Application application, Class clazz, File cacheFolder) throws CacheCreationException {
super(application, clazz, cacheFolder);
this.mJsonMapper = new ObjectMapper();
}
public Jackson2ObjectPersister(Application application, Class clazz) throws CacheCreationException {
this(application, clazz, null);
}
// ============================================================================================
// METHODS
// ============================================================================================
@Override
protected T deserializeData(String json) throws CacheLoadingException {
try {
return mJsonMapper.readValue(json, getHandledClass());
} catch (Exception e) {
throw new CacheLoadingException(e);
}
}
@Override
protected void saveData(T data, Object cacheKey) throws IOException, CacheSavingException {
String resultJson;
// transform the content in json to store it in the cache
resultJson = mJsonMapper.writeValueAsString(data);
synchronized (getCacheFile(cacheKey).getAbsolutePath().intern()) {
// finally store the json in the cache
if (!StringUtils.isEmpty(resultJson)) {
FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson, CharEncoding.UTF_8);
} else {
throw new CacheSavingException("Data was null and could not be serialized in json");
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy