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

com.octo.android.robospice.persistence.springandroid.json.jackson.JacksonObjectPersister Maven / Gradle / Ivy

package com.octo.android.robospice.persistence.springandroid.json.jackson;

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 org.codehaus.jackson.map.ObjectMapper;

import android.app.Application;

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 JacksonObjectPersister extends SpringAndroidObjectPersister {

    // ============================================================================================
    // ATTRIBUTES
    // ============================================================================================

    private final ObjectMapper mJsonMapper;

    // ============================================================================================
    // CONSTRUCTOR
    // ============================================================================================

    public JacksonObjectPersister(Application application, Class clazz, File cacheFolder)
        throws CacheCreationException {
        super(application, clazz, cacheFolder);
        this.mJsonMapper = new ObjectMapper();
    }

    public JacksonObjectPersister(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);

        // 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