Assuming I need to parse a huge list of Item
s from a json asset file in Android with the format similar to below:
[
{
"id": 1,
"name: "Tom"
// other stuff to describe "Item"
}
]
For performance reason I want to avoid converting the whole file into a single String
and parse it using moshi
. I want instead to use stream and parse each item from json to Item
and construct the list of items myself. With gson
I think it can be done like this:
Gson gson = new GsonBuilder().create();
List<Item> items = new ArrayList<>();
// Read file in stream mode
try (JsonReader reader = new JsonReader(in)) {
reader.beginArray();
while (reader.hasNext()) {
// Read data into object model
Item item = gson.fromJson(reader, Item.class);
items.add(item);
}
} catch ...
}
I have been searching for a while but couldn't find Moshi's equivalent way for doing this. Any advice?
Thanks