Created
June 12, 2018 08:48
-
-
Save Vanethos/011275eb66d754062f1a02af02770d05 to your computer and use it in GitHub Desktop.
Generic Gson Deserializer and raw data GSON parser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This class assumes that you use both RXJava and Dagger 2 in your project | |
* | |
*/ | |
public abstract class BaseLocalService<T> { | |
protected ResourceProvider mResourceProvider; | |
protected Gson mGson; | |
// ResourceProvider is a helper class that we use to retrieve resources, | |
// eliminating the need to use Context in this class | |
public BaseLocalService(ResourceProvider resourceProvider, Gson gson) { | |
mResourceProvider = resourceProvider; | |
mGson = gson; | |
} | |
// with this method | |
public abstract Class<T> objectClass(); | |
protected T getObjectFromJson(int id) throws IllegalAccessException, InstantiationException, NotFoundException, IOException { | |
String json = mResourceProvider.getRawJson(id); | |
return mGson.fromJson(json, objectClass()); | |
} | |
protected List<T> getLisOfObjectsFromJson(int id) throws IllegalAccessException, InstantiationException, NotFoundException, IOException { | |
String json = mResourceProvider.getRawJson(id); | |
return mGson.fromJson(json, TypeToken.getParameterized(ArrayList.class, objectClass()).getType()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This example class shows us how to use the generic GSON deserializer | |
*/ | |
public class ExampleService extends BaseLocalService<ExampleModel> { | |
@Inject | |
public AlertsLocalService(ResourceProvider resourceProvider, Gson gson) { | |
super(resourceProvider, gson); | |
} | |
// This method is needed to retrieve the object class | |
@Override | |
public Class<ExampleModel> objectClass() { | |
return ExampleModel.class; | |
} | |
public Single<List<ExampleModel>> getListOfAlerts() { | |
try { | |
return Single.just(getLisOfObjectsFromJson(R.raw.example)); | |
} catch (Exception | NotFoundException nfe ) { | |
return Single.error(nfe); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This helper class will help us retrieve the local raw json stored in a file. | |
*/ | |
public class ResourceProvider { | |
private final Context mContext; | |
public String getRawJson(@RawRes int id) throws Resources.NotFoundException, IOException { | |
BufferedReader r = new BufferedReader(new InputStreamReader(mContext.getResources().openRawResource(id))); | |
StringBuilder total = new StringBuilder(); | |
String line; | |
while ((line = r.readLine()) != null) { | |
total.append(line).append('\n'); | |
} | |
return total.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment