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

Commit

Permalink
Initial support for Google Assistant response messages
Browse files Browse the repository at this point in the history
  • Loading branch information
folomeev committed Jun 16, 2017
1 parent 3f1805d commit 11700d1
Show file tree
Hide file tree
Showing 5 changed files with 872 additions and 22 deletions.
65 changes: 55 additions & 10 deletions libai/src/main/java/ai/api/GsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -67,23 +71,64 @@ public Gson getGson() {
public static GsonFactory getDefaultFactory() {
return DEFAULT_FACTORY;
}

private static class ResponseMessagePlatformAdapter implements
JsonDeserializer<ResponseMessage.Platform>,
JsonSerializer<ResponseMessage.Platform> {

private static class ResponseItemAdapter implements JsonDeserializer<ResponseMessage>,
JsonSerializer<ResponseMessage> {
@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<ResponseMessage.MessageType>,
JsonSerializer<ResponseMessage.MessageType> {

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<ResponseMessage>,
JsonSerializer<ResponseMessage> {

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
Expand Down
Loading

0 comments on commit 11700d1

Please sign in to comment.