Parse a JSON key, which is either an object or an array of an object

Can you create a type adapter in Moshi that will analyze both an object and a list of objects of the same type?

For example, sometimes JSON:

{
  "person": {...}
}

In other cases, these are:

{
  "person": [{...}, {...}]
}

Ideally, I want the Java object to look like this:

class PersonContainer {
  @PersonsList List<Person> persons; // @List(Person.class) would be even better
}
+4
source share
1 answer

I suggest you do only what you said adapter.
make a function (adapter) to check if it is the only object or array, probably based on '[' char and the adapter will direct input to the correct processing function.

, , .

-1

Source: https://habr.com/ru/post/1658099/


All Articles