Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 1d0d76e

Browse files
author
folomeev
committed
Improved ResponseChatBubble w/single item serialization
1 parent 9a679a2 commit 1d0d76e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

libai/src/main/java/ai/api/GsonFactory.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,16 @@ public ResponseChatBubble deserialize(JsonElement json, Type typeOfT,
196196

197197
@Override
198198
public JsonElement serialize(ResponseChatBubble src, Type typeOfSrc, JsonSerializationContext context) {
199-
return SIMPLIFIED_GSON.toJsonTree(src, ResponseMessage.class);
199+
JsonObject result = (JsonObject)SIMPLIFIED_GSON.toJsonTree(src, ResponseMessage.class);
200+
JsonArray items = result.getAsJsonArray("items");
201+
if ((items != null) && (items.size() == 1)) {
202+
JsonObject item = (JsonObject)items.get(0);
203+
result.add("textToSpeech", item.get("textToSpeech"));
204+
result.add("ssml", item.get("ssml"));
205+
result.add("displayText", item.get("displayText"));
206+
result.remove("items");
207+
}
208+
return result;
200209
}
201210
}
202211
}

libai/src/test/java/ai/api/model/GoogleAssistantResponseMessagesTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class GoogleAssistantResponseMessagesTest {
3535

3636
private final String TEST_CHAT_BUBBLE_SINGLE_ITEM = "{"
3737
+ "\"customizeAudio\":true,"
38-
+ "\"textToSpeech\":\"hello\",\"ssml\":\"ssmlText\",\"displayText\":\"Hello\","
39-
+ "\"type\":\"simple_response\",\"platform\":\"google\""
38+
+ "\"type\":\"simple_response\",\"platform\":\"google\","
39+
+ "\"textToSpeech\":\"hello\",\"ssml\":\"ssmlText\",\"displayText\":\"Hello\""
4040
+ "}";
4141

4242
private final String TEST_BASIC_CARD = "{"
@@ -75,6 +75,20 @@ public class GoogleAssistantResponseMessagesTest {
7575
+ "\"type\":\"link_out_chip\",\"platform\":\"google\""
7676
+ "}";
7777

78+
@Test
79+
public void testResponseChatBubbleSerialization() {
80+
ResponseChatBubble chatBubble = new ResponseChatBubble();
81+
chatBubble.setCustomizeAudio(true);
82+
83+
ResponseChatBubble.Item item = new ResponseChatBubble.Item();
84+
item.setTextToSpeech("hello");
85+
item.setSsml("ssmlText");
86+
item.setDisplayText("Hello");
87+
88+
chatBubble.setItems(Arrays.asList(item));
89+
assertEquals(TEST_CHAT_BUBBLE_SINGLE_ITEM, gson.toJson(chatBubble));
90+
}
91+
7892
@Test
7993
public void testResponseChatBubbleDeserialization() {
8094
ResponseChatBubble chatBubble =

0 commit comments

Comments
 (0)