1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

Add custom deserializer for EmojiCategory and Emoji class, to fix parsing error

This commit is contained in:
Ammar Githam 2021-03-22 07:37:41 +09:00
parent 358beffa9d
commit 6b047c4ebb
5 changed files with 117 additions and 13 deletions

View File

@ -2,7 +2,6 @@ package awais.instagrabber.customviews.emoji;
import androidx.annotation.NonNull;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@ -12,10 +11,12 @@ public class Emoji {
private final List<Emoji> variants;
private GoogleCompatEmojiDrawable drawable;
public Emoji(final String unicode, final String name) {
public Emoji(final String unicode,
final String name,
final List<Emoji> variants) {
this.unicode = unicode;
this.name = name;
this.variants = new LinkedList<>();
this.variants = variants;
}
public String getUnicode() {

View File

@ -1,8 +1,8 @@
package awais.instagrabber.customviews.emoji;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
@ -10,16 +10,13 @@ import awais.instagrabber.R;
public class EmojiCategory {
private final EmojiCategoryType type;
private final Map<String, Emoji> emojis = new LinkedHashMap<>();
private final Map<String, Emoji> emojis;
@DrawableRes
private int drawableRes;
public EmojiCategory(final EmojiCategoryType type) {
public EmojiCategory(final EmojiCategoryType type, final Map<String, Emoji> emojis) {
this.type = type;
}
public EmojiCategory() {
this.type = null;
this.emojis = emojis;
}
public EmojiCategoryType getType() {
@ -78,8 +75,12 @@ public class EmojiCategory {
return Objects.hash(type);
}
@NonNull
@Override
public String toString() {
return "EmojiCategory {TYPE=" + type + ", EMOJIS=" + emojis + "}";
return "EmojiCategory{" +
"type=" + type +
", emojis=" + emojis +
'}';
}
}

View File

@ -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);
}
}

View File

@ -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
);
}
}

View File

@ -3,10 +3,11 @@ package awais.instagrabber.utils.emoji;
import android.util.Log;
import com.google.common.collect.ImmutableList;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Collection;
@ -52,7 +53,12 @@ public final class EmojiParser {
}
try (final InputStream in = classLoader.getResourceAsStream(file)) {
final String json = NetworkUtils.readFromInputStream(in);
final Gson gson = new Gson();
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(EmojiCategory.class, new EmojiCategoryDeserializer())
.registerTypeAdapter(Emoji.class, new EmojiDeserializer())
.setLenient()
.create();
final Type type = new TypeToken<Map<EmojiCategoryType, EmojiCategory>>() {}.getType();
categoryMap = gson.fromJson(json, type);
// Log.d(TAG, "EmojiParser: " + categoryMap);