I am writing a library to consume a Json API and I am facing a design problem when using Gson as the parsing library.
One of the endpoints returns an array
In your error-free case, since the top-level element is an array rather than an object, you have to use custom deserializers. You cannot escape from that. (I assume you cannot change the response formats.)
The best attempt to make the code cleaner, as far as I can see, is to create an abstract top-level deserializer class and check for error
here. If there is no error, delegate parsing fields to some abstract method which will be implemented in custom serializers that you have written for each class.