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

Commit

Permalink
Improved ResponseChatBubble w/single item serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
folomeev committed Aug 7, 2017
1 parent 9a679a2 commit 1d0d76e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion libai/src/main/java/ai/api/GsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,16 @@ public ResponseChatBubble deserialize(JsonElement json, Type typeOfT,

@Override
public JsonElement serialize(ResponseChatBubble src, Type typeOfSrc, JsonSerializationContext context) {
return SIMPLIFIED_GSON.toJsonTree(src, ResponseMessage.class);
JsonObject result = (JsonObject)SIMPLIFIED_GSON.toJsonTree(src, ResponseMessage.class);
JsonArray items = result.getAsJsonArray("items");
if ((items != null) && (items.size() == 1)) {
JsonObject item = (JsonObject)items.get(0);
result.add("textToSpeech", item.get("textToSpeech"));
result.add("ssml", item.get("ssml"));
result.add("displayText", item.get("displayText"));
result.remove("items");
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class GoogleAssistantResponseMessagesTest {

private final String TEST_CHAT_BUBBLE_SINGLE_ITEM = "{"
+ "\"customizeAudio\":true,"
+ "\"textToSpeech\":\"hello\",\"ssml\":\"ssmlText\",\"displayText\":\"Hello\","
+ "\"type\":\"simple_response\",\"platform\":\"google\""
+ "\"type\":\"simple_response\",\"platform\":\"google\","
+ "\"textToSpeech\":\"hello\",\"ssml\":\"ssmlText\",\"displayText\":\"Hello\""
+ "}";

private final String TEST_BASIC_CARD = "{"
Expand Down Expand Up @@ -75,6 +75,20 @@ public class GoogleAssistantResponseMessagesTest {
+ "\"type\":\"link_out_chip\",\"platform\":\"google\""
+ "}";

@Test
public void testResponseChatBubbleSerialization() {
ResponseChatBubble chatBubble = new ResponseChatBubble();
chatBubble.setCustomizeAudio(true);

ResponseChatBubble.Item item = new ResponseChatBubble.Item();
item.setTextToSpeech("hello");
item.setSsml("ssmlText");
item.setDisplayText("Hello");

chatBubble.setItems(Arrays.asList(item));
assertEquals(TEST_CHAT_BUBBLE_SINGLE_ITEM, gson.toJson(chatBubble));
}

@Test
public void testResponseChatBubbleDeserialization() {
ResponseChatBubble chatBubble =
Expand Down

0 comments on commit 1d0d76e

Please sign in to comment.