ConversationAdapter.java

  1. /*
  2.  * Copyright (C) 2020-2024 by Savoir-faire Linux
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16.  */
  17. package net.jami.jams.common.serialization.adapters;

  18. import com.google.gson.*;

  19. import net.jami.jams.common.objects.conversations.Conversation;

  20. import java.lang.reflect.Type;

  21. public class ConversationAdapter
  22.         implements JsonSerializer<Conversation>, JsonDeserializer<Conversation> {

  23.     /**
  24.      * { "created" : 1701180312, "erased" : 1701180454, "id" :
  25.      * "63afe87b8b18a3cfc40a4524ebd441c4a7d2336a", "lastDisplayed" :
  26.      * "63afe87b8b18a3cfc40a4524ebd441c4a7d2336a", "members" : [ { "uri" :
  27.      * "7172ad8cf00c93235886d8ce1b2889638c0da68d" }, { "uri" :
  28.      * "bf82b61537582a53fb0a28553f77060cc7a332c3" } ], "removed" : 1701180454 }
  29.      */
  30.     @Override
  31.     public Conversation deserialize(
  32.             JsonElement json, Type typeOfT, JsonDeserializationContext context)
  33.             throws JsonParseException {
  34.         Gson gson = new Gson();
  35.         JsonObject input = json.getAsJsonObject();
  36.         Conversation conversation = new Conversation();
  37.         conversation.setId(input.get("id").getAsString());
  38.         conversation.setCreated(input.get("created").getAsLong());

  39.         long timeRemoved = 0L;
  40.         if (input.has("removed")) {
  41.             timeRemoved = input.get("removed").getAsLong();
  42.         }
  43.         conversation.setRemoved(timeRemoved);

  44.         long timeErased = 0L;
  45.         if (input.has("erased")) {
  46.             timeErased = input.get("erased").getAsLong();
  47.         }
  48.         conversation.setErased(timeErased);
  49.         if (input.has("members")) {
  50.             conversation.setMembers(gson.toJson(input.get("members")));
  51.         }
  52.         conversation.setLastDisplayed(input.get("lastDisplayed").getAsString());
  53.         return conversation;
  54.     }

  55.     @Override
  56.     public JsonElement serialize(
  57.             Conversation conversation, Type typeOfSrc, JsonSerializationContext context) {
  58.         JsonObject output = new JsonObject();
  59.         output.addProperty("id", conversation.getId());
  60.         output.addProperty("created", conversation.getCreated());
  61.         output.addProperty("removed", conversation.getRemoved());
  62.         output.addProperty("erased", conversation.getErased());
  63.         JsonElement jsonMembers = JsonParser.parseString(conversation.getMembers());
  64.         output.add("members", jsonMembers);
  65.         output.addProperty("lastDisplayed", conversation.getLastDisplayed());
  66.         return output;
  67.     }
  68. }