Json response parser for Array or Object

前端 未结 3 1745
北恋
北恋 2020-12-21 12:01

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

3条回答
  •  伪装坚强ぢ
    2020-12-21 12:28

    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.

提交回复
热议问题