Created
October 14, 2014 10:18
-
-
Save dsvoronin/117f367217cc0a42e133 to your computer and use it in GitHub Desktop.
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
import android.content.res.AssetManager; | |
import com.google.gson.Gson; | |
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import rx.Observable; | |
public class AssetsReader { | |
private final Gson gson; | |
private final AssetManager assetManager; | |
public AssetsReader(Gson gson, AssetManager assetManager) { | |
this.gson = gson; | |
this.assetManager = assetManager; | |
} | |
public <T> Observable<T> readJson(final String assetsFolderRelatedPath, Class<T> type) { | |
return Observable.create(subscriber -> { | |
try { | |
InputStream stream = assetManager.open(assetsFolderRelatedPath); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); | |
subscriber.onNext(gson.fromJson(reader, type)); | |
subscriber.onCompleted(); | |
} catch (Throwable e) { | |
subscriber.onError(e); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment