-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from goblint/messages
Use a TypeAdapter to deserialize Multipiece from json and cleanup
- Loading branch information
Showing
4 changed files
with
111 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/api/json/GoblintMultiPieceInterfaceAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package api.json; | ||
|
||
import api.messages.GoblintMessagesResult; | ||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* The Class GoblintMultiPieceInterfaceAdapter. | ||
* <p> | ||
* Implements the JsonDeserializer to deserialize json to GoblintResult objects. | ||
* In particular to differentiate between the Group and Piece (Single) classes. | ||
* | ||
* @author Karoliine Holter | ||
* @since 0.0.4 | ||
*/ | ||
|
||
|
||
public class GoblintMultiPieceInterfaceAdapter implements JsonDeserializer<Object> { | ||
@Override | ||
public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
if (jsonObject.has("group_text")) | ||
return jsonDeserializationContext.deserialize(jsonObject, GoblintMessagesResult.Group.class); | ||
if (jsonObject.has("text")) | ||
return jsonDeserializationContext.deserialize(jsonObject, GoblintMessagesResult.Piece.class); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters