Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore #29

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paperdb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
androidTestCompile 'com.orhanobut:hawk:1.14'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
androidTestCompile 'joda-time:joda-time:2.9.1'
}

apply from: '../publish.gradle'
13 changes: 12 additions & 1 deletion paperdb/src/androidTest/java/io/paperdb/PaperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import android.support.test.runner.AndroidJUnit4;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import de.javakaffee.kryoserializers.jodatime.JodaDateTimeSerializer;
import io.paperdb.testdata.TestDataGenerator;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -167,5 +170,13 @@ public void testGetAllKeys() {
assertThat(allKeys.contains("city1")).isTrue();
assertThat(allKeys.contains("city2")).isTrue();
}


@Test
public void testCustomSerializer() {
Paper.addSerializer(DateTime.class, new JodaDateTimeSerializer());

DateTime now = DateTime.now();
Paper.book().write("joda-datetime", now);
assertEquals(Paper.book().read("joda-datetime"), now);
}
}
6 changes: 4 additions & 2 deletions paperdb/src/main/java/io/paperdb/Book.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.paperdb;

import android.content.Context;
import com.esotericsoftware.kryo.Serializer;

import java.util.HashMap;
import java.util.List;

public class Book {

private final Storage mStorage;

protected Book(Context context, String dbName) {
mStorage = new DbStoragePlainFile(context.getApplicationContext(), dbName);
protected Book(Context context, String dbName, HashMap<Class, Serializer> serializers) {
mStorage = new DbStoragePlainFile(context.getApplicationContext(), dbName, serializers);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions paperdb/src/main/java/io/paperdb/DbStoragePlainFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
Expand All @@ -16,12 +17,14 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import de.javakaffee.kryoserializers.ArraysAsListSerializer;
import de.javakaffee.kryoserializers.SynchronizedCollectionsSerializer;
import de.javakaffee.kryoserializers.UUIDSerializer;
import de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer;
import io.paperdb.serializer.NoArgCollectionSerializer;

Expand All @@ -31,6 +34,7 @@ public class DbStoragePlainFile implements Storage {

private final Context mContext;
private final String mDbName;
private final HashMap<Class, Serializer> mCustomSerializers;
private String mFilesDir;
private boolean mPaperDirIsCreated;

Expand Down Expand Up @@ -65,12 +69,22 @@ private Kryo createKryoInstance() {
new NoArgCollectionSerializer());
// To keep backward compatibility don't change the order of serializers above

// UUID support
kryo.register(UUID.class, new UUIDSerializer());

for (Class<?> clazz : mCustomSerializers.keySet())
kryo.register(clazz, mCustomSerializers.get(clazz));

return kryo;
}

public DbStoragePlainFile(Context context, String dbName) {
public DbStoragePlainFile(
Context context,
String dbName,
HashMap<Class, Serializer> serializers) {
mContext = context;
mDbName = dbName;
mCustomSerializers = serializers;
}

@Override
Expand Down
19 changes: 18 additions & 1 deletion paperdb/src/main/java/io/paperdb/Paper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.content.Context;
import android.os.Bundle;

import com.esotericsoftware.kryo.Serializer;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -26,6 +29,7 @@ public class Paper {
private static Context mContext;

private static final ConcurrentHashMap<String, Book> mBookMap = new ConcurrentHashMap<>();
private static final HashMap<Class, Serializer> mCustomSerializers = new HashMap<>();

/**
* Lightweight method to init Paper instance. Should be executed in {@link Application#onCreate()}
Expand Down Expand Up @@ -66,7 +70,7 @@ private static Book getBook(String name) {
synchronized (mBookMap) {
Book book = mBookMap.get(name);
if (book == null) {
book = new Book(mContext, name);
book = new Book(mContext, name, mCustomSerializers);
mBookMap.put(name, book);
}
return book;
Expand Down Expand Up @@ -116,4 +120,17 @@ public static void clear(Context context) {
init(context);
book().destroy();
}

/**
* Add a custom serializer for a class without no-arg constructor.
* When used, must be called right after Paper.init()
*
* @param clazz type of the custom serializer
* @param serializer the serializer instance
* @param <T> type of the serializer
*/
public static <T> void addSerializer(Class<T> clazz, Serializer<T> serializer) {
if (!mCustomSerializers.containsKey(clazz))
mCustomSerializers.put(clazz, serializer);
}
}