Skip to content

Commit

Permalink
Merge pull request #24 from yvasyliev/TrackDataDeserializer_inifinite…
Browse files Browse the repository at this point in the history
…_recursion_fix

fixed infinite deserialization recursion
  • Loading branch information
yvasyliev authored Feb 24, 2023
2 parents 62c26a2 + b5f6d97 commit aae3615
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ A Java implementation of [Deezer API](https://developers.deezer.com/api).
<dependency>
<groupId>com.github.yvasyliev</groupId>
<artifactId>deezer-api</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.yvasyliev</groupId>
<artifactId>deezer-api</artifactId>
<packaging>jar</packaging>
<version>2.0.0</version>
<version>2.0.1</version>
<name>Deezer API Java Library</name>
<description>A Java implementation of Deezer API.</description>
<url>https://github.com/yvasyliev/deezer-api</url>
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/api/deezer/deserializers/TrackDataDeserializer.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package api.deezer.deserializers;

import api.deezer.objects.Track;
import api.deezer.objects.data.TrackData;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

public class TrackDataDeserializer implements JsonDeserializer<TrackData> {
@Override
public TrackData deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
boolean isFalse = jsonElement.isJsonPrimitive()
&& jsonElement.getAsJsonPrimitive().isBoolean()
&& jsonElement.getAsBoolean();
return isFalse ? null : jsonDeserializationContext.deserialize(jsonElement, type);
if (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isBoolean() && !jsonElement.getAsBoolean()) {
return null;
}

JsonObject jsonObject = jsonElement.getAsJsonObject();
TrackData trackData = new TrackData();
trackData.setNext(jsonObject.get("next").getAsString());
trackData.setTotal(jsonObject.get("total").getAsInt());
trackData.setData(jsonDeserializationContext.deserialize(jsonObject.get("data"), TypeToken.getParameterized(List.class, Track.class).getType()));
return trackData;
}
}

0 comments on commit aae3615

Please sign in to comment.