Skip to content

Commit 2bef04e

Browse files
committed
generic gson converter to convert RxSharedPreferences
1 parent c372c07 commit 2bef04e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.anotherdev.firebase.auth.data;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.anotherdev.firebase.auth.util.FarGson;
6+
import com.f2prateek.rx.preferences2.Preference;
7+
import com.google.gson.Gson;
8+
9+
class RxPreferenceGsonConverter<T> implements Preference.Converter<T> {
10+
11+
private final Gson GSON = FarGson.get();
12+
private final Class<T> valueType;
13+
14+
15+
RxPreferenceGsonConverter(Class<T> valueType) {
16+
this.valueType = valueType;
17+
}
18+
19+
@NonNull
20+
@Override
21+
public T deserialize(@NonNull String json) {
22+
return GSON.fromJson(json, valueType);
23+
}
24+
25+
@NonNull
26+
@Override
27+
public String serialize(@NonNull T value) {
28+
return GSON.toJson(value);
29+
}
30+
}

0 commit comments

Comments
 (0)