在使用rxcache的过程中,发现缓存到本地的数据,rxcache重新读取解析的时候,没有按照我的数据结构规则解析,所以从网上找了这个类替换了 GsonSpeaker
。 使用如下:
cacheProviders = RxCache.Builder()
.persistence(context.filesDir,GsonTSpeaker())
.using(CacheProviders::class.java
现在把 ` GsonTSpeaker `的Java和kotlin的源码贴出来:
java源码:
package com.example.puppy.datalayer.data.cache;
/**
* Created by Stefan on 2018/5/31.
*/
import com.google.gson.Gson;
import io.victoralbertos.jolyglot.JolyglotGenerics;
import io.victoralbertos.jolyglot.Types;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GsonTSpeaker implements JolyglotGenerics {
private final Gson gson;
public GsonTSpeaker(Gson gson) {
this.gson = gson;
}
public GsonTSpeaker() {
this.gson = new Gson();
}
/**
* {@inheritDoc}
*/
@Override
public String toJson(Object src) {
return gson.toJson(src);
}
/**
* {@inheritDoc}
*/
@Override
public String toJson(Object src, Type typeOfSrc) {
return gson.toJson(src, typeOfSrc);
}
/**
* {@inheritDoc}
*/
@Override
public <T> T fromJson(String json, Class<T> classOfT) throws RuntimeException {
Type genType = classOfT.getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
return gson.fromJson(json, params[0]);
}
/**
* {@inheritDoc}
*/
@Override
public <T> T fromJson(String json, Type typeOfT) throws RuntimeException {
Type[] params = ((ParameterizedType) typeOfT).getActualTypeArguments();
return gson.fromJson(json, params[0]);
}
/**
* {@inheritDoc}
*/
@Override
public <T> T fromJson(File file, Class<T> classOfT) throws RuntimeException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file.getAbsoluteFile()));
Type genType = classOfT.getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
T object = gson.fromJson(reader, params[0]);
reader.close();
return object;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException i) {
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
public <T> T fromJson(File file, Type typeOfT) throws RuntimeException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file.getAbsoluteFile()));
Type[] params = ((ParameterizedType) typeOfT).getActualTypeArguments();
return gson.fromJson(reader, params[0]);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException i) {
}
}
}
}
@Override
public GenericArrayType arrayOf(Type componentType) {
return Types.arrayOf(componentType);
}
/**
* {@inheritDoc}
*/
@Override
public ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) {
return Types.newParameterizedType(rawType, typeArguments);
}
}
kotlin源码 :
package com.example.puppy.datalayer.data.net
import com.google.gson.Gson
import io.victoralbertos.jolyglot.JolyglotGenerics
import io.victoralbertos.jolyglot.Types
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.IOException
import java.lang.reflect.GenericArrayType
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
/**
* created by yujianbin on 2018/6/5
*
*/
class GsonTSpeaker : JolyglotGenerics {
private val gson: Gson
constructor(){
this.gson = Gson()
}
constructor(gson: Gson){
this.gson = gson
}
/**
* {@inheritDoc}
*/
override fun toJson(src: Any): String {
return gson.toJson(src)
}
/**
* {@inheritDoc}
*/
override fun toJson(src: Any, typeOfSrc: Type): String {
return gson.toJson(src, typeOfSrc)
}
/**
* {@inheritDoc}
*/
@Throws(RuntimeException::class)
override fun <T> fromJson(json: String, classOfT: Class<T>): T {
val genType = classOfT.genericSuperclass
val params = (genType as ParameterizedType).getActualTypeArguments()
return gson.fromJson(json, params[0])
}
/**
* {@inheritDoc}
*/
@Throws(RuntimeException::class)
override fun <T> fromJson(json: String, typeOfT: Type): T {
val params = (typeOfT as ParameterizedType).actualTypeArguments
return gson.fromJson(json, params[0])
}
/**
* {@inheritDoc}
*/
@Throws(RuntimeException::class)
override fun <T> fromJson(file: File, classOfT: Class<T>): T {
var reader: BufferedReader? = null
try {
reader = BufferedReader(FileReader(file.getAbsoluteFile()))
val genType = classOfT.genericSuperclass
val params = (genType as ParameterizedType).actualTypeArguments
val `object` = gson.fromJson<T>(reader, params[0])
reader!!.close()
return `object`
} catch (e: IOException) {
throw RuntimeException(e)
} finally {
if (reader != null) {
try {
reader!!.close()
} catch (i: IOException) {
}
}
}
}
/**
* {@inheritDoc}
*/
@Throws(RuntimeException::class)
override fun <T> fromJson(file: File, typeOfT: Type): T {
var reader: BufferedReader? = null
try {
reader = BufferedReader(FileReader(file.absoluteFile))
val params = (typeOfT as ParameterizedType).actualTypeArguments
return gson.fromJson(reader, params[0])
} catch (e: IOException) {
throw RuntimeException(e)
} finally {
if (reader != null) {
try {
reader.close()
} catch (i: IOException) {
}
}
}
}
override fun arrayOf(componentType: Type): GenericArrayType {
return Types.arrayOf(componentType)
}
/**
* {@inheritDoc}
*/
override fun newParameterizedType(rawType: Type, vararg typeArguments: Type): ParameterizedType {
return Types.newParameterizedType(rawType, *typeArguments)
}
}