Simon-Pierre Simon-Pierre - 2 months ago 56
JSON Question

REST Assured - Generic List deserialization

Let's say I have a Java

Person
class:

class Person {
String name;
String email;
}


With REST Assured, you can deserialize this JSON object

{"name":"Bob", "email":"bob@email.com"}


to a Java
Person
instance using

Person bob = given().when().get("person/Bob/").as(Person.class);


How does one use REST Assured to deserialize this JSON array

[{"name":"Bob", "email":"bob@email.com"},
{"name":"Alice", "email":"alice@email.com"},
{"name":"Jay", "email":"jay@email.com"}]


into a
List<Person>
? For example, this would be handy:

List<Person> persons = given().when().get("person/").as(...);

Answer

I found a way to achieve what I wanted:

List<Person> persons = given().when().get("person/").as(Person[].class);

UPDATE: Using Rest-Assured 1.8.1, looks like cast to List is not supported anymore. You need to declare and object array like this:

Person[] persons = given().when().get("person/").as(Person[].class);
Comments