Browse Source

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

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
6b047c4ebb
  1. 7
      app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java
  2. 17
      app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java
  3. 52
      app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.java
  4. 44
      app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java
  5. 10
      app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java

7
app/src/main/java/awais/instagrabber/customviews/emoji/Emoji.java

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

17
app/src/main/java/awais/instagrabber/customviews/emoji/EmojiCategory.java

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

52
app/src/main/java/awais/instagrabber/utils/emoji/EmojiCategoryDeserializer.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);
}
}

44
app/src/main/java/awais/instagrabber/utils/emoji/EmojiDeserializer.java

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

10
app/src/main/java/awais/instagrabber/utils/emoji/EmojiParser.java

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

Loading…
Cancel
Save