Skip to content

Commit 0f78926

Browse files
committed
Fix: added BackgroundFillAdapter and BackgroundTypeAdapter for deserialization (api v7.3)
1 parent 0c4bcc2 commit 0f78926

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import com.google.gson.Gson;
55
import com.google.gson.GsonBuilder;
66
import com.pengrad.telegrambot.model.Update;
7+
import com.pengrad.telegrambot.model.chatbackground.BackgroundFill;
8+
import com.pengrad.telegrambot.model.chatbackground.BackgroundType;
79
import com.pengrad.telegrambot.model.chatboost.source.ChatBoostSource;
810
import com.pengrad.telegrambot.model.message.MaybeInaccessibleMessage;
911
import com.pengrad.telegrambot.model.message.origin.MessageOrigin;
1012
import com.pengrad.telegrambot.model.reaction.ReactionType;
13+
import com.pengrad.telegrambot.utility.gson.BackgroundFillAdapter;
14+
import com.pengrad.telegrambot.utility.gson.BackgroundTypeAdapter;
1115
import com.pengrad.telegrambot.utility.gson.ChatBoostSourceTypeAdapter;
1216
import com.pengrad.telegrambot.utility.gson.MaybeInaccessibleMessageTypeAdapter;
1317
import com.pengrad.telegrambot.utility.gson.MessageOriginTypeAdapter;
@@ -31,6 +35,8 @@ private BotUtils() {}
3135
.registerTypeAdapter(MessageOrigin.class, new MessageOriginTypeAdapter())
3236
.registerTypeAdapter(ChatBoostSource.class, new ChatBoostSourceTypeAdapter())
3337
.registerTypeAdapter(MaybeInaccessibleMessage.class, new MaybeInaccessibleMessageTypeAdapter())
38+
.registerTypeAdapter(BackgroundType.class, new BackgroundTypeAdapter())
39+
.registerTypeAdapter(BackgroundFill.class, new BackgroundFillAdapter())
3440
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
3541
.create();
3642

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pengrad.telegrambot.utility.gson;
2+
3+
import com.google.gson.*;
4+
import com.pengrad.telegrambot.model.chatbackground.BackgroundFill;
5+
import com.pengrad.telegrambot.model.chatbackground.BackgroundFillFreeformGradient;
6+
import com.pengrad.telegrambot.model.chatbackground.BackgroundFillGradient;
7+
import com.pengrad.telegrambot.model.chatbackground.BackgroundFillSolid;
8+
9+
import java.lang.reflect.Type;
10+
11+
public class BackgroundFillAdapter implements JsonDeserializer<BackgroundFill> {
12+
13+
@Override
14+
public BackgroundFill deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
15+
JsonObject object = element.getAsJsonObject();
16+
String discriminator = object.getAsJsonPrimitive("type").getAsString();
17+
18+
if (BackgroundFillSolid.TYPE.equals(discriminator)) {
19+
return context.deserialize(object, BackgroundFillSolid.class);
20+
} else if (BackgroundFillGradient.TYPE.equals(discriminator)) {
21+
return context.deserialize(object, BackgroundFillGradient.class);
22+
} else if (BackgroundFillFreeformGradient.TYPE.equals(discriminator)) {
23+
return context.deserialize(object, BackgroundFillFreeformGradient.class);
24+
}
25+
26+
return context.deserialize(object, BackgroundFill.class);
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.pengrad.telegrambot.utility.gson;
2+
3+
import com.google.gson.*;
4+
import com.pengrad.telegrambot.model.chatbackground.BackgroundType;
5+
import com.pengrad.telegrambot.model.chatbackground.BackgroundTypeChatTheme;
6+
import com.pengrad.telegrambot.model.chatbackground.BackgroundTypeFill;
7+
import com.pengrad.telegrambot.model.chatbackground.BackgroundTypePattern;
8+
import com.pengrad.telegrambot.model.chatbackground.BackgroundTypeWallpaper;
9+
10+
import java.lang.reflect.Type;
11+
12+
public class BackgroundTypeAdapter implements JsonDeserializer<BackgroundType> {
13+
14+
@Override
15+
public BackgroundType deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
16+
JsonObject object = element.getAsJsonObject();
17+
String discriminator = object.getAsJsonPrimitive("type").getAsString();
18+
19+
if (BackgroundTypeFill.TYPE.equals(discriminator)) {
20+
return context.deserialize(object, BackgroundTypeFill.class);
21+
} else if (BackgroundTypeWallpaper.TYPE.equals(discriminator)) {
22+
return context.deserialize(object, BackgroundTypeWallpaper.class);
23+
} else if (BackgroundTypePattern.TYPE.equals(discriminator)) {
24+
return context.deserialize(object, BackgroundTypePattern.class);
25+
} else if (BackgroundTypeChatTheme.TYPE.equals(discriminator)) {
26+
return context.deserialize(object, BackgroundTypeChatTheme.class);
27+
}
28+
29+
return context.deserialize(object, BackgroundType.class);
30+
}
31+
}

0 commit comments

Comments
 (0)