Search in sources :

Example 1 with GroupBuilder

use of me.perrycate.groupmeutils.data.GroupBuilder in project groupme-utils by TheGuyWithTheFace.

the class GroupDeserializer method deserialize.

@Override
public Group deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject json = getResponse(jsonElement).getAsJsonObject();
    GroupBuilder g = new GroupBuilder();
    g.setId(json.get("id").getAsString());
    g.setName(json.get("name").getAsString());
    g.setType(json.get("type").getAsString());
    g.setDescription(json.get("description").getAsString());
    if (!json.get("image_url").isJsonNull()) {
        g.setImageUrl(json.get("image_url").getAsString());
    }
    g.setCreatorUserId(json.get("creator_user_id").getAsString());
    long createTime = json.get("created_at").getAsLong();
    g.setCreatedAt(Instant.ofEpochSecond(createTime));
    long updateTime = json.get("updated_at").getAsLong();
    g.setUpdatedAt(Instant.ofEpochSecond(updateTime));
    if (!json.get("share_url").isJsonNull()) {
        g.setShareUrl(json.get("share_url").getAsString());
    }
    // Get members of the group
    JsonArray response = json.get("members").getAsJsonArray();
    int numMembers = response.size();
    Member[] members = new Member[numMembers];
    for (int i = 0; i < numMembers; i++) {
        JsonElement elem = response.get(i);
        members[i] = context.deserialize(elem, Member.class);
    }
    g.setMembers(members);
    // TODO this will be reworked later, see note in Group Class.
    JsonObject messages = json.get("messages").getAsJsonObject();
    g.setMessageCount(messages.get("count").getAsInt());
    g.setLastMessageId(messages.get("last_message_id").getAsString());
    long lastCreate = messages.get("last_message_created_at").getAsLong();
    g.setLastMessageCreatedAt(Instant.ofEpochSecond(lastCreate));
    return g.createGroup();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) GroupBuilder(me.perrycate.groupmeutils.data.GroupBuilder) JsonObject(com.google.gson.JsonObject) Member(me.perrycate.groupmeutils.data.Member)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 GroupBuilder (me.perrycate.groupmeutils.data.GroupBuilder)1 Member (me.perrycate.groupmeutils.data.Member)1