Moshi: How to parse a list?

Created on 14 Sep 2015  路  5Comments  路  Source: square/moshi

I'm trying to parse a a list but I can't find docs on how to do that. I already made my custom adapter class, but when I do the following:

This doesn't work with lists

JsonAdapter<MyData> adapter = moshi.adapter(MyData.class)

This doesn't work

JsonAdapter<List> adapter = moshi.adapter(List.class)

How do I parse a List<MyData> ?

Most helpful comment

Type listMyData = Types.newParameterizedType(List.class, MyData.class);
JsonAdapter<List<MyData>> adapter = moshi.adapter(listMyData);

All 5 comments

Type listMyData = Types.newParameterizedType(List.class, MyData.class);
JsonAdapter<List<MyData>> adapter = moshi.adapter(listMyData);

This should definitely be in documentation - I just searched for this for like hours.

@janswist it is now :)

How do I do the reverse?

I already have an adapter generated for the class, but not sure how to use it to convert a list of objects to json.

This should definitely be in documentation - I just searched for this for like hours.

Was this page helpful?
0 / 5 - 0 ratings