content: /*
   * Copyright (C) 2016-2023 ActionTech.
   * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher.
   */
  
  package com.actiontech.dble.cluster.zkprocess.entity.user;
  
  import com.google.gson.*;
  
  import java.lang.reflect.Type;
  
  public class UserGsonAdapter implements JsonSerializer<User>, JsonDeserializer<User> {
      @Override
      public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
          JsonObject result = new JsonObject();
          result.add("type", new JsonPrimitive(src.getClass().getSimpleName()));
          result.add("properties", context.serialize(src, src.getClass()));
  
          return result;
      }
  
      @Override
      public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
              throws JsonParseException {
          JsonObject jsonObject = json.getAsJsonObject();
          String type = jsonObject.get("type").getAsString();
          JsonElement element = jsonObject.get("properties");
  
          try {
              return context.deserialize(element, Class.forName("com.actiontech.dble.cluster.zkprocess.entity.user." + type));
          } catch (ClassNotFoundException cnfe) {
              throw new JsonParseException("Unknown element type: " + type, cnfe);
          }
      }
  }
  