Java Code Examples for com.google.gson.JsonObject#isJsonNull()

The following examples show how to use com.google.gson.JsonObject#isJsonNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientCredentials.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
public ClientCredentials createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new ClientCredentials.Builder()
    .setAccessToken(
      hasAndNotNull(jsonObject, "access_token")
        ? jsonObject.get("access_token").getAsString()
        : null)
    .setTokenType(
      hasAndNotNull(jsonObject, "token_type")
        ? jsonObject.get("token_type").getAsString()
        : null)
    .setExpiresIn(
      hasAndNotNull(jsonObject, "expires_in")
        ? jsonObject.get("expires_in").getAsInt()
        : null)
    .build();
}
 
Example 2
Source File: Disallows.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
@Override
public Disallows createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  EnumSet<Action> disallowedActions = EnumSet.noneOf(Action.class);
  for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
    if (entry.getValue().getAsJsonPrimitive().getAsBoolean()) {
      disallowedActions.add(
        Action.keyOf(entry.getKey().toLowerCase()));
    }
  }

  return new Builder()
    .setDisallowedActions(
      disallowedActions)
    .build();
}
 
Example 3
Source File: FeaturedPlaylists.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
public FeaturedPlaylists createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new FeaturedPlaylists.Builder()
    .setMessage(
      hasAndNotNull(jsonObject, "message")
        ? jsonObject.get("message").getAsString()
        : null)
    .setPlaylists(
      hasAndNotNull(jsonObject, "playlists")
        ? new PlaylistSimplified.JsonUtil().createModelObjectPaging(
        jsonObject.getAsJsonObject("playlists"))
        : null)
    .build();
}
 
Example 4
Source File: Recommendations.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
public Recommendations createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new Recommendations.Builder()
    .setSeeds(
      hasAndNotNull(jsonObject, "seeds")
        ? new RecommendationsSeed.JsonUtil().createModelObjectArray(
        jsonObject.getAsJsonArray("seeds"))
        : null)
    .setTracks(
      hasAndNotNull(jsonObject, "tracks")
        ? new TrackSimplified.JsonUtil().createModelObjectArray(
        jsonObject.getAsJsonArray("tracks"))
        : null)
    .build();
}
 
Example 5
Source File: ResumePoint.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
@Override
public ResumePoint createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new Builder()
    .setFullyPlayed(
      hasAndNotNull(jsonObject, "fully_played")
        ? jsonObject.get("fully_played").getAsBoolean()
        : null)
    .setResumePositionMs(
      hasAndNotNull(jsonObject, "resume_position_ms")
        ? jsonObject.get("resume_position_ms").getAsInt()
        : null)
    .build();
}
 
Example 6
Source File: SavedTrack.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
public SavedTrack createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  try {
    return new Builder()
      .setAddedAt(
        hasAndNotNull(jsonObject, "added_at")
          ? SpotifyApi.parseDefaultDate(jsonObject.get("added_at").getAsString())
          : null)
      .setTrack(
        hasAndNotNull(jsonObject, "track")
          ? new Track.JsonUtil().createModelObject(
          jsonObject.getAsJsonObject("track"))
          : null)
      .build();
  } catch (ParseException e) {
    SpotifyApi.LOGGER.log(Level.SEVERE, e.getMessage());
    return null;
  }
}
 
Example 7
Source File: GsonInterfaceAdapter.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private <S> S readValue(JsonObject jsonObject, TypeToken<S> defaultTypeToken) throws IOException {
  try {
    TypeToken<S> actualTypeToken;
    if (jsonObject.isJsonNull()) {
      return null;
    } else if (jsonObject.has(OBJECT_TYPE)) {
      String className = jsonObject.get(OBJECT_TYPE).getAsString();
      Class<S> klazz = (Class<S>) Class.forName(className);
      actualTypeToken = TypeToken.get(klazz);
    } else if (defaultTypeToken != null) {
      actualTypeToken = defaultTypeToken;
    } else {
      throw new IOException("Could not determine TypeToken.");
    }
    TypeAdapter<S> delegate = this.gson.getDelegateAdapter(this.factory, actualTypeToken);
    S value = delegate.fromJsonTree(jsonObject.get(OBJECT_DATA));
    return value;
  } catch (ClassNotFoundException cnfe) {
    throw new IOException(cnfe);
  }
}
 
Example 8
Source File: ExternalUrl.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public ExternalUrl createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  Map<String, String> map = new Gson().fromJson(jsonObject, Map.class);

  return new ExternalUrl.Builder()
    .setExternalUrls(map)
    .build();
}
 
Example 9
Source File: PagingCursorbased.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public PagingCursorbased<X> createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new Builder<X>()
    .setHref(
      hasAndNotNull(jsonObject, "href")
        ? jsonObject.get("href").getAsString()
        : null)
    .setItems(
      hasAndNotNull(jsonObject, "items")
        ? createModelObjectArray(
        jsonObject.getAsJsonArray("items"), (Class<X>) ((ParameterizedType) getClass()
          .getGenericSuperclass()).getActualTypeArguments()[0])
        : null)
    .setLimit(
      hasAndNotNull(jsonObject, "limit")
        ? jsonObject.get("limit").getAsInt()
        : null)
    .setNext(
      hasAndNotNull(jsonObject, "next")
        ? jsonObject.get("next").getAsString()
        : null)
    .setCursors(
      hasAndNotNull(jsonObject, "cursors")
        ? new Cursor.JsonUtil().createModelObjectArray(
        jsonObject.getAsJsonArray("cursors"))
        : null)
    .setTotal(
      hasAndNotNull(jsonObject, "total")
        ? jsonObject.get("total").getAsInt()
        : null)
    .build();
}
 
Example 10
Source File: AuthorizationCodeCredentials.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public AuthorizationCodeCredentials createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new AuthorizationCodeCredentials.Builder()
    .setAccessToken(
      hasAndNotNull(jsonObject, "access_token")
        ? jsonObject.get("access_token").getAsString()
        : null)
    .setTokenType(
      hasAndNotNull(jsonObject, "token_type")
        ? jsonObject.get("token_type").getAsString()
        : null)
    .setScope(
      hasAndNotNull(jsonObject, "scope")
        ? jsonObject.get("scope").getAsString()
        : null)
    .setExpiresIn(
      hasAndNotNull(jsonObject, "expires_in")
        ? jsonObject.get("expires_in").getAsInt()
        : null)
    .setRefreshToken(
      hasAndNotNull(jsonObject, "refresh_token")
        ? jsonObject.get("refresh_token").getAsString()
        : null)
    .build();
}
 
Example 11
Source File: TrackLink.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public TrackLink createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new TrackLink.Builder()
    .setExternalUrls(
      hasAndNotNull(jsonObject, "external_urls")
        ? new ExternalUrl.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("external_urls"))
        : null)
    .setHref(
      hasAndNotNull(jsonObject, "href")
        ? jsonObject.get("href").getAsString()
        : null)
    .setId(
      hasAndNotNull(jsonObject, "id")
        ? jsonObject.get("id").getAsString()
        : null)
    .setType(
      hasAndNotNull(jsonObject, "type")
        ? ModelObjectType.keyOf(
        jsonObject.get("type").getAsString().toLowerCase())
        : null)
    .setUri(
      hasAndNotNull(jsonObject, "uri")
        ? jsonObject.get("uri").getAsString()
        : null)
    .build();
}
 
Example 12
Source File: AudioAnalysisMeta.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public AudioAnalysisMeta createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new AudioAnalysisMeta.Builder()
    .setAnalysisTime(
      hasAndNotNull(jsonObject, "analysis_time")
        ? jsonObject.get("analysis_time").getAsFloat()
        : null)
    .setAnalyzerVersion(
      hasAndNotNull(jsonObject, "analyzer_version")
        ? jsonObject.get("analyzer_version").getAsString()
        : null)
    .setDetailedStatus(
      hasAndNotNull(jsonObject, "detailed_status")
        ? jsonObject.get("detailed_status").getAsString()
        : null)
    .setInputProcess(
      hasAndNotNull(jsonObject, "input_process")
        ? jsonObject.get("input_process").getAsString()
        : null)
    .setPlatform(
      hasAndNotNull(jsonObject, "platform")
        ? jsonObject.get("platform").getAsString()
        : null)
    .setStatusCode(
      hasAndNotNull(jsonObject, "status_code")
        ? jsonObject.get("status_code").getAsInt()
        : null)
    .setTimestamp(
      hasAndNotNull(jsonObject, "timestamp")
        ? jsonObject.get("timestamp").getAsLong()
        : null)
    .build();
}
 
Example 13
Source File: PlayHistory.java    From spotify-web-api-java with MIT License 5 votes vote down vote up
public PlayHistory createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  try {
    return new Builder()
      .setTrack(
        hasAndNotNull(jsonObject, "track")
          ? new TrackSimplified.JsonUtil().createModelObject(
          jsonObject.getAsJsonObject("track"))
          : null)
      .setPlayedAt(
        hasAndNotNull(jsonObject, "played_at")
          ? SpotifyApi.parseDefaultDate(jsonObject.get("played_at").getAsString())
          : null)
      .setContext(
        hasAndNotNull(jsonObject, "context")
          ? new Context.JsonUtil().createModelObject(
          jsonObject.getAsJsonObject("context"))
          : null)
      .build();
  } catch (ParseException e) {
    SpotifyApi.LOGGER.log(Level.SEVERE, e.getMessage());
    return null;
  }
}
 
Example 14
Source File: ContextsDeserializerAdapter.java    From sentry-android with MIT License 5 votes vote down vote up
private @Nullable <T> T parseObject(
    final @NotNull JsonDeserializationContext context,
    final @NotNull JsonObject jsonObject,
    final @NotNull String key,
    final @NotNull Class<T> clazz)
    throws JsonParseException {
  final JsonObject object = jsonObject.getAsJsonObject(key);
  if (object != null && !object.isJsonNull()) {
    return context.deserialize(object, clazz);
  }
  return null;
}
 
Example 15
Source File: TrackSimplified.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
public TrackSimplified createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new Builder()
    .setArtists(
      hasAndNotNull(jsonObject, "artists")
        ? new ArtistSimplified.JsonUtil().createModelObjectArray(
        jsonObject.getAsJsonArray("artists"))
        : null)
    .setAvailableMarkets(
      hasAndNotNull(jsonObject, "available_markets")
        ? new Gson().fromJson(jsonObject.getAsJsonArray(
        "available_markets"), CountryCode[].class)
        : null)
    .setDiscNumber(
      hasAndNotNull(jsonObject, "disc_number")
        ? jsonObject.get("disc_number").getAsInt()
        : null)
    .setDurationMs(
      hasAndNotNull(jsonObject, "duration_ms")
        ? jsonObject.get("duration_ms").getAsInt()
        : null)
    .setExplicit(
      hasAndNotNull(jsonObject, "explicit")
        ? jsonObject.get("explicit").getAsBoolean()
        : null)
    .setExternalUrls(
      hasAndNotNull(jsonObject, "external_urls")
        ? new ExternalUrl.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("external_urls"))
        : null)
    .setHref(
      hasAndNotNull(jsonObject, "href")
        ? jsonObject.get("href").getAsString()
        : null)
    .setId(
      hasAndNotNull(jsonObject, "id")
        ? jsonObject.get("id").getAsString()
        : null)
    .setIsPlayable(
      hasAndNotNull(jsonObject, "is_playable")
        ? jsonObject.get("is_playable").getAsBoolean()
        : null)
    .setLinkedFrom(
      hasAndNotNull(jsonObject, "linked_from")
        ? new TrackLink.JsonUtil().createModelObject(
        jsonObject.get("linked_from").getAsJsonObject())
        : null)
    .setName(
      hasAndNotNull(jsonObject, "name")
        ? jsonObject.get("name").getAsString()
        : null)
    .setPreviewUrl(
      hasAndNotNull(jsonObject, "preview_url")
        ? jsonObject.get("preview_url").getAsString()
        : null)
    .setTrackNumber(
      hasAndNotNull(jsonObject, "track_number")
        ? jsonObject.get("track_number").getAsInt()
        : null)
    .setType(
      hasAndNotNull(jsonObject, "type")
        ? ModelObjectType.keyOf(
        jsonObject.get("type").getAsString().toLowerCase())
        : null)
    .setUri(
      hasAndNotNull(jsonObject, "uri")
        ? jsonObject.get("uri").getAsString()
        : null)
    .build();
}
 
Example 16
Source File: PlaylistTrack.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
public PlaylistTrack createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  try {
    IPlaylistItem track = null;

    if (hasAndNotNull(jsonObject, "track")) {
      final JsonObject trackObj = jsonObject.getAsJsonObject("track");

      if (hasAndNotNull(trackObj, "type")) {
        String type = trackObj.get("type").getAsString().toLowerCase();

        if (type.equals("track")) {
          track = new Track.JsonUtil().createModelObject(trackObj);
        } else if (type.equals("episode")) {
          track = new Episode.JsonUtil().createModelObject(trackObj);
        }
      }
    }

    return new Builder()
      .setAddedAt(
        hasAndNotNull(jsonObject, "added_at")
          ? SpotifyApi.parseDefaultDate(jsonObject.get("added_at").getAsString())
          : null)
      .setAddedBy(
        hasAndNotNull(jsonObject, "added_by")
          ? new User.JsonUtil().createModelObject(
          jsonObject.get("added_by").getAsJsonObject())
          : null)
      .setIsLocal(
        hasAndNotNull(jsonObject, "is_local")
          ? jsonObject.get("is_local").getAsBoolean()
          : null)
      .setTrack(track)
      .build();
  } catch (ParseException e) {
    SpotifyApi.LOGGER.log(Level.SEVERE, e.getMessage());
    return null;
  }
}
 
Example 17
Source File: CurrentlyPlayingContext.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
public CurrentlyPlayingContext createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new Builder()
    .setDevice(
      hasAndNotNull(jsonObject, "device")
        ? new Device.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("device"))
        : null)
    .setRepeat_state(
      hasAndNotNull(jsonObject, "repeat_state")
        ? jsonObject.get("repeat_state").getAsString()
        : null)
    .setShuffle_state(
      hasAndNotNull(jsonObject, "shuffle_state")
        ? jsonObject.get("shuffle_state").getAsBoolean()
        : null)
    .setContext(
      hasAndNotNull(jsonObject, "context")
        ? new Context.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("context"))
        : null)
    .setTimestamp(
      hasAndNotNull(jsonObject, "timestamp")
        ? jsonObject.get("timestamp").getAsLong()
        : null)
    .setProgress_ms(
      hasAndNotNull(jsonObject, "progress_ms")
        ? jsonObject.get("progress_ms").getAsInt()
        : null)
    .setIs_playing(
      hasAndNotNull(jsonObject, "is_playing")
        ? jsonObject.get("is_playing").getAsBoolean()
        : null)
    .setItem(
      hasAndNotNull(jsonObject, "item") && hasAndNotNull(jsonObject, "currently_playing_type")
        ? (jsonObject.get("currently_playing_type").getAsString().equals("track")
        ? new Track.JsonUtil().createModelObject(jsonObject.getAsJsonObject("item"))
        : jsonObject.get("currently_playing_type").getAsString().equals("episode")
        ? new Episode.JsonUtil().createModelObject(jsonObject.getAsJsonObject("item"))
        : null)
        : null)
    .setCurrentlyPlayingType(
      hasAndNotNull(jsonObject, "currently_playing_type")
        ? CurrentlyPlayingType.keyOf(
        jsonObject.get("currently_playing_type").getAsString().toLowerCase())
        : null)
    .setActions(
      hasAndNotNull(jsonObject, "actions")
        ? new Actions.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("actions"))
        : null)
    .build();
}
 
Example 18
Source File: AudioFeatures.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
public AudioFeatures createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new AudioFeatures.Builder()
    .setAcousticness(
      hasAndNotNull(jsonObject, "acousticness")
        ? jsonObject.get("acousticness").getAsFloat()
        : null)
    .setAnalysisUrl(
      hasAndNotNull(jsonObject, "analysis_url")
        ? jsonObject.get("analysis_url").getAsString()
        : null)
    .setDanceability(
      hasAndNotNull(jsonObject, "danceability")
        ? jsonObject.get("danceability").getAsFloat()
        : null)
    .setDurationMs(
      hasAndNotNull(jsonObject, "duration_ms")
        ? jsonObject.get("duration_ms").getAsInt()
        : null)
    .setEnergy(
      hasAndNotNull(jsonObject, "energy")
        ? jsonObject.get("energy").getAsFloat()
        : null)
    .setId(
      hasAndNotNull(jsonObject, "id")
        ? jsonObject.get("id").getAsString()
        : null)
    .setInstrumentalness(
      hasAndNotNull(jsonObject, "instrumentalness")
        ? jsonObject.get("instrumentalness").getAsFloat()
        : null)
    .setKey(
      hasAndNotNull(jsonObject, "key")
        ? jsonObject.get("key").getAsInt()
        : null)
    .setLiveness(
      hasAndNotNull(jsonObject, "liveness")
        ? jsonObject.get("liveness").getAsFloat()
        : null)
    .setLoudness(
      hasAndNotNull(jsonObject, "loudness")
        ? jsonObject.get("loudness").getAsFloat()
        : null)
    .setMode(
      hasAndNotNull(jsonObject, "mode")
        ? Modality.keyOf(
        jsonObject.get("mode").getAsInt())
        : null)
    .setSpeechiness(
      hasAndNotNull(jsonObject, "speechiness")
        ? jsonObject.get("speechiness").getAsFloat()
        : null)
    .setTempo(
      hasAndNotNull(jsonObject, "tempo")
        ? jsonObject.get("tempo").getAsFloat()
        : null)
    .setTimeSignature(
      hasAndNotNull(jsonObject, "time_signature")
        ? jsonObject.get("time_signature").getAsInt()
        : null)
    .setTrackHref(
      hasAndNotNull(jsonObject, "track_href")
        ? jsonObject.get("track_href").getAsString()
        : null)
    .setType(
      hasAndNotNull(jsonObject, "type")
        ? ModelObjectType.keyOf(
        jsonObject.get("type").getAsString().toLowerCase())
        : null)
    .setUri(
      hasAndNotNull(jsonObject, "uri")
        ? jsonObject.get("uri").getAsString()
        : null)
    .setValence(
      hasAndNotNull(jsonObject, "valence")
        ? jsonObject.get("valence").getAsFloat()
        : null)
    .build();
}
 
Example 19
Source File: AlbumSimplified.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
public AlbumSimplified createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new AlbumSimplified.Builder()
    .setAlbumGroup(
      hasAndNotNull(jsonObject, "album_group")
        ? AlbumGroup.keyOf(
        jsonObject.get("album_group").getAsString().toLowerCase())
        : null)
    .setAlbumType(
      hasAndNotNull(jsonObject, "album_type")
        ? AlbumType.keyOf(
        jsonObject.get("album_type").getAsString().toLowerCase())
        : null)
    .setArtists(
      hasAndNotNull(jsonObject, "artists")
        ? new ArtistSimplified.JsonUtil().createModelObjectArray(
        jsonObject.getAsJsonArray("artists"))
        : null)
    .setAvailableMarkets(
      hasAndNotNull(jsonObject, "available_markets")
        ? new Gson().fromJson(
        jsonObject.get("available_markets"), CountryCode[].class)
        : null)
    .setExternalUrls(
      hasAndNotNull(jsonObject, "external_urls")
        ? new ExternalUrl.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("external_urls"))
        : null)
    .setHref(
      hasAndNotNull(jsonObject, "href")
        ? jsonObject.get("href").getAsString()
        : null)
    .setId(
      hasAndNotNull(jsonObject, "id")
        ? jsonObject.get("id").getAsString()
        : null)
    .setImages(
      hasAndNotNull(jsonObject, "images")
        ? new Image.JsonUtil().createModelObjectArray(
        jsonObject.getAsJsonArray("images"))
        : null)
    .setName(
      hasAndNotNull(jsonObject, "name")
        ? jsonObject.get("name").getAsString()
        : null)
    .setReleaseDate(
      hasAndNotNull(jsonObject, "release_date")
        ? jsonObject.get("release_date").getAsString()
        : null)
    .setReleaseDatePrecision(
      hasAndNotNull(jsonObject, "release_date_precision")
        ? ReleaseDatePrecision.keyOf(
        jsonObject.get("release_date_precision").getAsString().toLowerCase())
        : null)
    .setRestrictions(
      hasAndNotNull(jsonObject, "restrictions")
        ? new Restrictions.JsonUtil().createModelObject(
        jsonObject.getAsJsonObject("restrictions"))
        : null)
    .setType(
      hasAndNotNull(jsonObject, "type")
        ? ModelObjectType.keyOf(
        jsonObject.get("type").getAsString().toLowerCase())
        : null)
    .setUri(
      hasAndNotNull(jsonObject, "uri")
        ? jsonObject.get("uri").getAsString()
        : null)
    .build();
}
 
Example 20
Source File: AudioAnalysisSection.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
public AudioAnalysisSection createModelObject(JsonObject jsonObject) {
  if (jsonObject == null || jsonObject.isJsonNull()) {
    return null;
  }

  return new AudioAnalysisSection.Builder()
    .setKey(
      hasAndNotNull(jsonObject, "key")
        ? jsonObject.get("key").getAsInt()
        : null)
    .setKeyConfidence(
      hasAndNotNull(jsonObject, "key_confidence")
        ? jsonObject.get("key_confidence").getAsFloat()
        : null)
    .setLoudness(
      hasAndNotNull(jsonObject, "loudness")
        ? jsonObject.get("loudness").getAsFloat()
        : null)
    .setMeasure(
      new AudioAnalysisMeasure.JsonUtil().createModelObject(jsonObject))
    .setMode(
      hasAndNotNull(jsonObject, "type")
        ? Modality.keyOf(
        jsonObject.get("mode").getAsInt())
        : null)
    .setModeConfidence(
      hasAndNotNull(jsonObject, "mode_confidence")
        ? jsonObject.get("mode_confidence").getAsFloat()
        : null)
    .setTempo(
      hasAndNotNull(jsonObject, "tempo")
        ? jsonObject.get("tempo").getAsFloat()
        : null)
    .setTempoConfidence(
      hasAndNotNull(jsonObject, "tempo_confidence")
        ? jsonObject.get("tempo_confidence").getAsFloat()
        : null)
    .setTimeSignature(
      hasAndNotNull(jsonObject, "time_signature")
        ? jsonObject.get("time_signature").getAsInt()
        : null)
    .setTimeSignatureConfidence(
      hasAndNotNull(jsonObject, "time_signature_confidence")
        ? jsonObject.get("time_signature_confidence").getAsFloat()
        : null)
    .build();
}