Class ModelObjects
java.lang.Object
se.michaelthelin.spotify.model_objects.ModelObjects
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 Summary
Modifier and TypeMethodDescriptionstatic <T extends IModelObject>
T[]arrayFromJson(String json, Class<T> type) Reads model objects from a JSON array of objects.static <T extends IModelObject>
TReads a model object from a JSON object.static <T extends IModelObject>
PagingCursorbased<T> pagingCursorbasedFromJson(String json, Class<T> type) Reads aPagingCursorbasedobject, such as the response of a request that pages by cursor.static <T extends IModelObject>
Paging<T> pagingFromJson(String json, Class<T> type) Reads aPagingobject, such as the response of a request that lists items page by page.
-
Method Details
-
fromJson
Reads a model object from a JSON object.- Type Parameters:
T- The model object type.- Parameters:
json- The JSON object, ornull.type- The model object class, for exampleTrack.class.- Returns:
- The model object, or
nullifjsonisnull.
-
arrayFromJson
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 exampleArtist.class.- Returns:
- The model objects, with
nullfor each JSONnullelement.
-
pagingFromJson
Reads aPagingobject, 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 exampleSavedTrack.class.- Returns:
- The paging object.
-
pagingCursorbasedFromJson
public static <T extends IModelObject> PagingCursorbased<T> pagingCursorbasedFromJson(String json, Class<T> type) Reads aPagingCursorbasedobject, 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 examplePlayHistory.class.- Returns:
- The cursor-based paging object.
-