From 894bb5692c6293cafe49b580960af5a0ad7a552b Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Wed, 5 Apr 2023 21:42:13 +0300 Subject: [PATCH] Fixed #160 --- README.md | 2 +- pom.xml | 2 +- .../deserializers/PayloadDeserializer.java | 17 +++- .../PayloadDeserializerTest.java | 80 +++++++++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/test/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializerTest.java diff --git a/README.md b/README.md index 34ae9d44..d1cfb00c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ This library uses the next third-party dependencies: com.github.yvasyliev java-vk-bots-longpoll-api - 4.1.0 + 4.1.1 ``` 4. Extend `LongPollBot` class and override necessary methods: diff --git a/pom.xml b/pom.xml index e7535c35..7b112945 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.yvasyliev java-vk-bots-longpoll-api jar - 4.1.0 + 4.1.1 Java VK Bots Long Poll API A Java library to create VK bots using Bots Long Poll API https://github.com/yvasyliev/java-vk-bots-long-poll-api diff --git a/src/main/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializer.java b/src/main/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializer.java index 46bf2d36..4b5fe4fc 100644 --- a/src/main/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializer.java +++ b/src/main/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializer.java @@ -5,6 +5,7 @@ import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; +import com.google.gson.JsonSyntaxException; import java.lang.reflect.Type; @@ -20,7 +21,21 @@ public class PayloadDeserializer implements JsonDeserializer { @Override public JsonElement deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { return jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString() - ? GSON.fromJson(jsonElement.getAsString(), JsonElement.class) + ? parseString(jsonElement) : jsonElement; } + + /** + * Parses string. + * + * @param jsonElement string value. + * @return JSON if provided string is a valid JSON, same string otherwise. + */ + private JsonElement parseString(JsonElement jsonElement) { + try { + return GSON.fromJson(jsonElement.getAsString(), JsonElement.class); + } catch (JsonSyntaxException e) { + return jsonElement; + } + } } diff --git a/src/test/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializerTest.java b/src/test/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializerTest.java new file mode 100644 index 00000000..0a38d452 --- /dev/null +++ b/src/test/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializerTest.java @@ -0,0 +1,80 @@ +package api.longpoll.bots.adapters.deserializers; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.google.gson.annotations.JsonAdapter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PayloadDeserializerTest { + Gson gson = new Gson(); + + @Test + void parseBooleanTest() { + String json = "{\"payload\":true}"; + + JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; + assertTrue(payload.isJsonPrimitive()); + + JsonPrimitive jsonPrimitive = payload.getAsJsonPrimitive(); + assertTrue(jsonPrimitive.isBoolean()); + assertTrue(jsonPrimitive.getAsBoolean()); + } + + @Test + void parseStringTest() { + String json = "{\"payload\":\"Hello World!\"}"; + + JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; + assertTrue(payload.isJsonPrimitive()); + + JsonPrimitive jsonPrimitive = payload.getAsJsonPrimitive(); + assertTrue(jsonPrimitive.isString()); + assertEquals("Hello World!", jsonPrimitive.getAsString()); + } + + @Test + void parseJsonTest() { + String json = "{\"payload\":{\"foo\":\"bar\"}}"; + + JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; + assertTrue(payload.isJsonObject()); + + JsonObject jsonObject = payload.getAsJsonObject(); + assertTrue(jsonObject.has("foo")); + + JsonElement foo = jsonObject.get("foo"); + assertTrue(foo.isJsonPrimitive()); + + JsonPrimitive jsonPrimitive = foo.getAsJsonPrimitive(); + assertTrue(jsonPrimitive.isString()); + assertEquals("bar", jsonPrimitive.getAsString()); + } + + @Test + void parseStringJsonTest() { + String json = "{\"payload\":\"{\\\"foo\\\":\\\"bar\\\"}\"}"; + + JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; + assertTrue(payload.isJsonObject()); + + JsonObject jsonObject = payload.getAsJsonObject(); + assertTrue(jsonObject.has("foo")); + + JsonElement foo = jsonObject.get("foo"); + assertTrue(foo.isJsonPrimitive()); + + JsonPrimitive jsonPrimitive = foo.getAsJsonPrimitive(); + assertTrue(jsonPrimitive.isString()); + assertEquals("bar", jsonPrimitive.getAsString()); + } + + static class PayloadHolder { + @JsonAdapter(PayloadDeserializer.class) + JsonElement payload; + } +} \ No newline at end of file