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

com.slack.api.util.json.GsonLayoutBlockFactory Maven / Gradle / Ivy

There is a newer version: 1.44.1
Show newest version
package com.slack.api.util.json;

import com.google.gson.*;
import com.slack.api.model.block.*;

import java.lang.reflect.Type;

/**
 * Factory for deserializing BlockKit elements from a
 * {@link com.slack.api.model.Message chat message response}.
 */
public class GsonLayoutBlockFactory implements JsonDeserializer, JsonSerializer {

    private final boolean failOnUnknownProperties;

    public GsonLayoutBlockFactory() {
        this(false);
    }

    public GsonLayoutBlockFactory(boolean failOnUnknownProperties) {
        this.failOnUnknownProperties = failOnUnknownProperties;
    }

    @Override
    public LayoutBlock deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        final JsonObject jsonObject = json.getAsJsonObject();
        final JsonPrimitive prim = (JsonPrimitive) jsonObject.get("type");
        final String typeName = prim.getAsString();
        final Class clazz = getLayoutClassInstance(typeName);
        return context.deserialize(jsonObject, clazz);
    }

    private Class getLayoutClassInstance(String typeName) {
        switch (typeName) {
            case SectionBlock.TYPE:
                return SectionBlock.class;
            case DividerBlock.TYPE:
                return DividerBlock.class;
            case ImageBlock.TYPE:
                return ImageBlock.class;
            case ContextBlock.TYPE:
                return ContextBlock.class;
            case CallBlock.TYPE:
                return CallBlock.class;
            case ActionsBlock.TYPE:
                return ActionsBlock.class;
            case FileBlock.TYPE:
                return FileBlock.class;
            case InputBlock.TYPE:
                return InputBlock.class;
            case HeaderBlock.TYPE:
                return HeaderBlock.class;
            case RichTextBlock.TYPE:
                return RichTextBlock.class;
            default:
                if (failOnUnknownProperties) {
                    throw new JsonParseException("Unsupported layout block type: " + typeName);
                } else {
                    return UnknownBlock.class;
                }
        }
    }

    @Override
    public JsonElement serialize(LayoutBlock src, Type typeOfSrc, JsonSerializationContext context) {
        return context.serialize(src);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy