diff --git a/libai/src/main/java/ai/api/GsonFactory.java b/libai/src/main/java/ai/api/GsonFactory.java index 1b25979..15105b3 100644 --- a/libai/src/main/java/ai/api/GsonFactory.java +++ b/libai/src/main/java/ai/api/GsonFactory.java @@ -29,11 +29,13 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import ai.api.model.ResponseMessage; import ai.api.model.ResponseMessage.MessageType; +import ai.api.model.ResponseMessage.Platform; import java.lang.reflect.Type; import java.text.SimpleDateFormat; @@ -49,6 +51,8 @@ public class GsonFactory { private static final Gson PROTOCOL_GSON = new GsonBuilder() .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US).toPattern()) .registerTypeAdapter(ResponseMessage.class, new ResponseItemAdapter()) + .registerTypeAdapter(ResponseMessage.MessageType.class, new ResponseMessageTypeAdapter()) + .registerTypeAdapter(ResponseMessage.Platform.class, new ResponseMessagePlatformAdapter()) .registerTypeAdapter(ResponseMessage.ResponseSpeech.class, new ResponseSpeechDeserializer()) .create(); @@ -67,23 +71,64 @@ public Gson getGson() { public static GsonFactory getDefaultFactory() { return DEFAULT_FACTORY; } + + private static class ResponseMessagePlatformAdapter implements + JsonDeserializer, + JsonSerializer { - private static class ResponseItemAdapter implements JsonDeserializer, - JsonSerializer { + @Override + public JsonElement serialize(Platform src, Type typeOfT, JsonSerializationContext context) { + return context.serialize(src.getName()); + } @Override - public ResponseMessage deserialize(JsonElement json, Type typeOfT, - JsonDeserializationContext context) throws JsonParseException { + public Platform deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + String name = json.getAsString(); + if (name == null) { + return Platform.DEFAULT; + } + Platform result = Platform.fromName(name); + if (result == null) { + throw new JsonParseException(String.format("Unexpected platform name: %s", json)); + } + return result; + } + } + private static class ResponseMessageTypeAdapter implements + JsonDeserializer, + JsonSerializer { - int typeCode = json.getAsJsonObject().get("type").getAsInt(); + @Override + public JsonElement serialize(MessageType src, Type typeOfT, JsonSerializationContext context) { + return context.serialize(src.getCode() <= 4 ? src.getCode() : src.getName()); + } - for (MessageType type : MessageType.values()) { - if (type.getCode() == typeCode) { - return context.deserialize(json, type.getType()); - } + @Override + public MessageType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + JsonPrimitive jsonValue = json.getAsJsonPrimitive(); + MessageType result = null; + if (jsonValue.isNumber()) { + result = MessageType.fromCode(jsonValue.getAsInt()); + } else { + result = MessageType.fromName(jsonValue.getAsString()); + } + if (result == null) { + throw new JsonParseException(String.format("Unexpected message type value: %s", jsonValue)); } + return result; + } + } + + private static class ResponseItemAdapter implements JsonDeserializer, + JsonSerializer { - throw new JsonParseException(String.format("Unexpected message type value: %d", typeCode)); + @Override + public ResponseMessage deserialize(JsonElement json, Type typeOfT, + JsonDeserializationContext context) throws JsonParseException { + MessageType messageType = context.deserialize(json.getAsJsonObject().get("type"), MessageType.class); + return context.deserialize(json, messageType.getType()); } @Override diff --git a/libai/src/main/java/ai/api/model/GoogleAssistantResponseMessages.java b/libai/src/main/java/ai/api/model/GoogleAssistantResponseMessages.java new file mode 100644 index 0000000..0d2bb9a --- /dev/null +++ b/libai/src/main/java/ai/api/model/GoogleAssistantResponseMessages.java @@ -0,0 +1,477 @@ +package ai.api.model; + +import java.util.List; + +public abstract class GoogleAssistantResponseMessages extends ResponseMessage { + + protected GoogleAssistantResponseMessages(MessageType type) { + super(type, Platform.GOOGLE); + } + + public static class CardImage { + private String url; + + /** + * @return the URL + */ + public final String getUrl() { + return url; + } + + /** + * @param url the URL to set + */ + public final void setUrl(String url) { + this.url = url; + } + } + + public static class OptionInfo { + private String key; + private List synonyms; + + /** + * @return the key + */ + public final String getKey() { + return key; + } + + /** + * @param key the key to set + */ + public final void setKey(String key) { + this.key = key; + } + + /** + * @return the synonyms + */ + public final List getSynonyms() { + return synonyms; + } + + /** + * @param synonyms the synonyms to set + */ + public final void setSynonyms(List synonyms) { + this.synonyms = synonyms; + } + } + + public static class CardItem { + private OptionInfo optionInfo; + private String title; + private String description; + private CardImage image; + /** + * @return the optionInfo + */ + public final OptionInfo getOptionInfo() { + return optionInfo; + } + /** + * @param optionInfo the optionInfo to set + */ + public final void setOptionInfo(OptionInfo optionInfo) { + this.optionInfo = optionInfo; + } + /** + * @return the title + */ + public final String getTitle() { + return title; + } + /** + * @param title the title to set + */ + public final void setTitle(String title) { + this.title = title; + } + /** + * @return the description + */ + public final String getDescription() { + return description; + } + /** + * @param description the description to set + */ + public final void setDescription(String description) { + this.description = description; + } + /** + * @return the image + */ + public final CardImage getImage() { + return image; + } + /** + * @param image the image to set + */ + public final void setImage(CardImage image) { + this.image = image; + } + } + + public static class ResponseChatBubble extends GoogleAssistantResponseMessages { + + private Boolean customizeAudio; + private List items; + + public ResponseChatBubble() { + super(MessageType.CHAT_BUBBLE); + } + + /** + * @return the customizeAudio + */ + public final Boolean getCustomizeAudio() { + return customizeAudio; + } + + /** + * @param customizeAudio the customizeAudio to set + */ + public final void setCustomizeAudio(Boolean customizeAudio) { + this.customizeAudio = customizeAudio; + } + + /** + * @return the items + */ + public final List getItems() { + return items; + } + + /** + * @param items the items to set + */ + public final void setItems(List items) { + this.items = items; + } + + public static class Item { + private String textToSpeech; + private String ssml; + private String displayText; + + /** + * @return the textToSpeech + */ + public final String getTextToSpeech() { + return textToSpeech; + } + + /** + * @param textToSpeech the textToSpeech to set + */ + public final void setTextToSpeech(String textToSpeech) { + this.textToSpeech = textToSpeech; + } + + /** + * @return the SSML + */ + public final String getSsml() { + return ssml; + } + + /** + * @param ssml the SSML to set + */ + public final void setSsml(String ssml) { + this.ssml = ssml; + } + + /** + * @return the displayText + */ + public final String getDisplayText() { + return displayText; + } + + /** + * @param displayText the displayText to set + */ + public final void setDisplayText(String displayText) { + this.displayText = displayText; + } + } + } + + public static class ResponseBasicCard extends GoogleAssistantResponseMessages { + private String title; + private String subtitle; + private String formattedText; + private CardImage image; + private List