Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Switch to Jackson to correct JSON issue. (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
tracyboehrer authored Feb 22, 2021
1 parent f441624 commit 35608c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
7 changes: 1 addition & 6 deletions samples/50.teams-messaging-extensions-search/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20201115</version>
</dependency>
<dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package com.microsoft.bot.sample.teamssearch;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.bot.builder.TurnContext;
import com.microsoft.bot.builder.teams.TeamsActivityHandler;
import com.microsoft.bot.schema.*;
Expand All @@ -12,8 +15,6 @@
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import java.io.IOException;
Expand Down Expand Up @@ -44,11 +45,14 @@ protected CompletableFuture<MessagingExtensionResponse> onTeamsMessagingExtensio
.thenApply(packages -> {
List<MessagingExtensionAttachment> attachments = new ArrayList<>();
for (String[] item : packages) {
ObjectNode data = Serialization.createObjectNode();
data.set("data", Serialization.objectToTree(item));

ThumbnailCard previewCard = new ThumbnailCard() {{
setTitle(item[0]);
setTap(new CardAction() {{
setType(ActionTypes.INVOKE);
setValue(new JSONObject().put("data", item).toString());
setValue(Serialization.toStringSilent(data));
}});
}};

Expand Down Expand Up @@ -129,23 +133,22 @@ private CompletableFuture<List<String[]>> findPackages(String text) {
))
.build();

List<String[]> filteredItems = new ArrayList<String[]>();
List<String[]> filteredItems = new ArrayList<>();
try {
Response response = client.newCall(request).execute();
JSONObject obj = new JSONObject(response.body().string());
JSONArray dataArray = (JSONArray) obj.get("data");

dataArray.forEach(i -> {
JSONObject item = (JSONObject) i;
filteredItems.add(new String[]{
item.getString("id"),
item.getString("version"),
item.getString("description"),
item.has("projectUrl") ? item.getString("projectUrl") : "",
item.has("iconUrl") ? item.getString("iconUrl") : ""
JsonNode obj = Serialization.jsonToTree(response.body().string());
ArrayNode dataArray = (ArrayNode) obj.get("data");

for (int i = 0; i < dataArray.size(); i++) {
JsonNode item = dataArray.get(i);
filteredItems.add(new String[] {
item.get("id").asText(),
item.get("version").asText(),
item.get("description").asText(),
item.has("projectUrl") ? item.get("projectUrl").asText() : "",
item.has("iconUrl") ? item.get("iconUrl").asText() : ""
});
});

}
} catch (IOException e) {
LoggerFactory.getLogger(TeamsMessagingExtensionsSearchBot.class)
.error("findPackages", e);
Expand Down

0 comments on commit 35608c1

Please sign in to comment.