Class ModelObjects

java.lang.Object
se.michaelthelin.spotify.model_objects.ModelObjects

public final class ModelObjects extends Object
Reads model objects from JSON in the shape the Spotify Web API sends them.

Requests already return model objects, so this is only needed for JSON that reached the caller some other way, for example a response that was cached or stored. It reads exactly as the requests do, including the wire formats of enums and dates and the polymorphic playlist items.

Track track = ModelObjects.fromJson(json, Track.class);
Paging<SavedTrack> savedTracks = ModelObjects.pagingFromJson(json, SavedTrack.class);
Malformed JSON, or JSON of a different shape than requested (e.g. an array where an object is expected), throws an unchecked exception.
  • Method Details

    • fromJson

      public static <T extends IModelObject> T fromJson(String json, Class<T> type)
      Reads a model object from a JSON object.
      Type Parameters:
      T - The model object type.
      Parameters:
      json - The JSON object, or null.
      type - The model object class, for example Track.class.
      Returns:
      The model object, or null if json is null.
    • arrayFromJson

      public static <T extends IModelObject> T[] arrayFromJson(String json, Class<T> type)
      Reads model objects from a JSON array of objects.
      Type Parameters:
      T - The model object type.
      Parameters:
      json - The JSON array.
      type - The model object class of the elements, for example Artist.class.
      Returns:
      The model objects, with null for each JSON null element.
    • pagingFromJson

      public static <T extends IModelObject> Paging<T> pagingFromJson(String json, Class<T> type)
      Reads a Paging object, such as the response of a request that lists items page by page.
      Type Parameters:
      T - The model object type of the items.
      Parameters:
      json - The JSON paging object.
      type - The model object class of the items, for example SavedTrack.class.
      Returns:
      The paging object.
    • pagingCursorbasedFromJson

      public static <T extends IModelObject> PagingCursorbased<T> pagingCursorbasedFromJson(String json, Class<T> type)
      Reads a PagingCursorbased object, such as the response of a request that pages by cursor.
      Type Parameters:
      T - The model object type of the items.
      Parameters:
      json - The JSON cursor-based paging object.
      type - The model object class of the items, for example PlayHistory.class.
      Returns:
      The cursor-based paging object.