Browse Source
Add custom deserializer for EmojiCategory and Emoji class, to fix parsing error
renovate/org.robolectric-robolectric-4.x
Add custom deserializer for EmojiCategory and Emoji class, to fix parsing error
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
5 changed files with 117 additions and 13 deletions
-
7app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java
-
17app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java
-
52app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.java
-
44app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java
-
10app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java
@ -0,0 +1,52 @@ |
|||
package awais.instagrabber.utils.emoji; |
|||
|
|||
import android.util.Log; |
|||
|
|||
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 java.lang.reflect.Type; |
|||
import java.util.LinkedHashMap; |
|||
import java.util.Map; |
|||
|
|||
import awais.instagrabber.customviews.emoji.Emoji; |
|||
import awais.instagrabber.customviews.emoji.EmojiCategory; |
|||
import awais.instagrabber.customviews.emoji.EmojiCategoryType; |
|||
|
|||
public class EmojiCategoryDeserializer implements JsonDeserializer<EmojiCategory> { |
|||
private static final String TAG = EmojiCategoryDeserializer.class.getSimpleName(); |
|||
|
|||
@Override |
|||
public EmojiCategory deserialize(final JsonElement json, |
|||
final Type typeOfT, |
|||
final JsonDeserializationContext context) throws JsonParseException { |
|||
final JsonObject jsonObject = json.getAsJsonObject(); |
|||
final JsonElement typeElement = jsonObject.get("type"); |
|||
final JsonObject emojisObject = jsonObject.getAsJsonObject("emojis"); |
|||
if (typeElement == null || emojisObject == null) { |
|||
throw new JsonParseException("Invalid json for EmojiCategory"); |
|||
} |
|||
final String typeString = typeElement.getAsString(); |
|||
EmojiCategoryType type; |
|||
try { |
|||
type = EmojiCategoryType.valueOf(typeString); |
|||
} catch (IllegalArgumentException e) { |
|||
Log.e(TAG, "deserialize: ", e); |
|||
type = EmojiCategoryType.OTHERS; |
|||
} |
|||
final Map<String, Emoji> emojis = new LinkedHashMap<>(); |
|||
for (final Map.Entry<String, JsonElement> emojiObjectEntry : emojisObject.entrySet()) { |
|||
final String unicode = emojiObjectEntry.getKey(); |
|||
final JsonElement value = emojiObjectEntry.getValue(); |
|||
if (unicode == null || value == null) { |
|||
throw new JsonParseException("Invalid json for EmojiCategory"); |
|||
} |
|||
final Emoji emoji = context.deserialize(value, Emoji.class); |
|||
emojis.put(unicode, emoji); |
|||
} |
|||
return new EmojiCategory(type, emojis); |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
package awais.instagrabber.utils.emoji; |
|||
|
|||
import com.google.gson.JsonArray; |
|||
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 java.lang.reflect.Type; |
|||
import java.util.LinkedList; |
|||
import java.util.List; |
|||
|
|||
import awais.instagrabber.customviews.emoji.Emoji; |
|||
|
|||
public class EmojiDeserializer implements JsonDeserializer<Emoji> { |
|||
@Override |
|||
public Emoji deserialize(final JsonElement json, |
|||
final Type typeOfT, |
|||
final JsonDeserializationContext context) throws JsonParseException { |
|||
final JsonObject jsonObject = json.getAsJsonObject(); |
|||
final JsonElement unicodeElement = jsonObject.get("unicode"); |
|||
final JsonElement nameElement = jsonObject.get("name"); |
|||
if (unicodeElement == null || nameElement == null) { |
|||
throw new JsonParseException("Invalid json for Emoji class"); |
|||
} |
|||
final JsonElement variantsElement = jsonObject.get("variants"); |
|||
final List<Emoji> variants = new LinkedList<>(); |
|||
if (variantsElement != null) { |
|||
final JsonArray variantsArray = variantsElement.getAsJsonArray(); |
|||
for (final JsonElement variantElement : variantsArray) { |
|||
final Emoji variant = context.deserialize(variantElement, Emoji.class); |
|||
if (variant != null) { |
|||
variants.add(variant); |
|||
} |
|||
} |
|||
} |
|||
return new Emoji( |
|||
unicodeElement.getAsString(), |
|||
nameElement.getAsString(), |
|||
variants |
|||
); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue