All Downloads are FREE. Search and download functionalities are using the official Maven repository.

clarifai2.dto.model.output_info.OutputInfo Maven / Gradle / Ivy

The newest version!
package clarifai2.dto.model.output_info;

import clarifai2.internal.grpc.api.ModelOuterClass;
import clarifai2.dto.model.ModelType;
import clarifai2.internal.JSONAdapterFactory;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static clarifai2.internal.InternalUtil.assertJsonIs;
import static clarifai2.internal.InternalUtil.fromJson;

@JsonAdapter(OutputInfo.Adapter.class)
public abstract class OutputInfo {

  OutputInfo() {}

  @NotNull public abstract ModelOuterClass.OutputInfo serialize();

  @Nullable public static OutputInfo deserialize(ModelOuterClass.OutputInfo outputInfo) {
    Class aClass = ModelType.determineModelType(outputInfo).infoType();

    Method m;
    try {
      m = aClass.getMethod("deserializeInner", ModelOuterClass.OutputInfo.class);
    } catch (NoSuchMethodException e) {
      return null;
    }
    Object result = null;
    try {
      result = m.invoke(null, outputInfo);
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
    return (OutputInfo) result;
  }

  static class Adapter extends JSONAdapterFactory {

    @Nullable @Override protected Serializer serializer() {
      return new Serializer() {
        @NotNull @Override public JsonElement serialize(@Nullable OutputInfo value, @NotNull final Gson gson) {

          JsonElement jsonOutputInfo;
          if (value instanceof ConceptOutputInfo) {
            jsonOutputInfo = gson.toJsonTree(value, ConceptOutputInfo.class);
          } else {
            throw new RuntimeException("Unsupported serialization for this OutputInfo object.");
          }

          return jsonOutputInfo;
        }
      };
    }

    @Nullable @Override protected Deserializer deserializer() {
      return new Deserializer() {
        @Nullable @Override
        public OutputInfo deserialize(
            @NotNull JsonElement json,
            @NotNull TypeToken type,
            @NotNull Gson gson
        ) {
          final JsonObject root = assertJsonIs(json, JsonObject.class);
          if (root.has("message")) {
            return null;
          }
          if (root.has("type") && root.size() == 1) {
            return null;
          }
          return fromJson(gson, json, ModelType.determineModelType(root).infoType());
        }
      };
    }

    @NotNull @Override protected TypeToken typeToken() {
      return new TypeToken() {};
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy