Skip to content

Instantly share code, notes, and snippets.

@mataanin
Last active August 29, 2015 14:19
Show Gist options
  • Save mataanin/00e1ddb8301e96bbc59b to your computer and use it in GitHub Desktop.
Save mataanin/00e1ddb8301e96bbc59b to your computer and use it in GitHub Desktop.
Save a pojo to file
package com.instacart.library.persistence;
import android.content.Context;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.util.List;
public class ILJsonCollectionFile<PAYLOAD> extends ILJsonFile<List<PAYLOAD>> {
private final String mTag;
public ILJsonCollectionFile(Context context, String filename, String tag) {
super(context, filename, tag);
mTag = tag;
}
public void store(List<PAYLOAD> data) throws IOException {
super.store(data);
}
public List<PAYLOAD> read() throws IOException {
TypeReference<List<PAYLOAD>> typeReference = new TypeReference<List<PAYLOAD>>() { };
return read(typeReference);
}
}
package com.instacart.library.persistence;
import android.content.Context;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.instacart.library.ILLog;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
public class ILJsonFile<PAYLOAD> {
private final Context mContext;
private final String mFilename;
private Class<PAYLOAD> mPayloadClass;
private final String mTag;
public ILJsonFile(Context context, String filename, String tag) {
mContext = context;
mFilename = filename;
mTag = tag;
}
private ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
public ILJsonFile(Context context, String filename, String tag, Class<PAYLOAD> clazz) {
this(context, filename, tag);
mPayloadClass = clazz;
}
public void store(PAYLOAD data) throws IOException {
ObjectMapper mapper = getObjectMapper();
FileWriter writer = new FileWriter(getFile(), false);
mapper.writeValue(writer, data);
writer.close();
}
public PAYLOAD read() throws IOException {
return read(new TypeReference<PAYLOAD>() {
@Override public Type getType() {
return mPayloadClass;
}
});
}
public PAYLOAD read(TypeReference<PAYLOAD> payloadClass) throws IOException {
File file = getFile();
try {
ObjectMapper mapper = getObjectMapper();
return mapper.readValue(file, payloadClass);
} catch (JsonProcessingException jme) {
ILLog.d(mTag, "failed reading application data", jme);
} catch (FileNotFoundException fnf) {
;
} catch (IOException io) {
// if data is corrupt, get rid of it.
file.delete();
throw io;
}
return null;
}
public File getFile() {
File cacheDir = mContext.getApplicationContext().getCacheDir();
return new File(cacheDir, mFilename);
}
public String getTag() {
return mTag;
}
public void delete() {
getFile().delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment