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

com.github.seratch.jslack.common.json.GsonBlockElementFactory Maven / Gradle / Ivy

The newest version!
package com.github.seratch.jslack.common.json;

import com.github.seratch.jslack.api.model.block.UnknownBlockElement;
import com.github.seratch.jslack.api.model.block.element.*;
import com.google.gson.*;

import java.lang.reflect.Type;

/**
 * Factory for deserializing BlockKit 'block elements' (buttons, selects,
 * images, menus) from a {@link com.github.seratch.jslack.api.model.Message chat
 * message response}.
 *
 * @see Block
 * Elements documentation
 */
public class GsonBlockElementFactory implements JsonDeserializer, JsonSerializer {

    private boolean failOnUnknownProperties;

    public GsonBlockElementFactory() {
        this(false);
    }

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

    @Override
    public BlockElement 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 = getContextBlockElementClassInstance(typeName);
        return context.deserialize(jsonObject, clazz);
    }

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

    private Class getContextBlockElementClassInstance(String typeName) {
        switch (typeName) {
            case ButtonElement.TYPE:
                return ButtonElement.class;
            case ImageElement.TYPE:
                return ImageElement.class;
            case ChannelsSelectElement.TYPE:
                return ChannelsSelectElement.class;
            case UsersSelectElement.TYPE:
                return UsersSelectElement.class;
            case ExternalSelectElement.TYPE:
                return ExternalSelectElement.class;
            case ConversationsSelectElement.TYPE:
                return ConversationsSelectElement.class;
            case StaticSelectElement.TYPE:
                return StaticSelectElement.class;
            case MultiChannelsSelectElement.TYPE:
                return MultiChannelsSelectElement.class;
            case MultiUsersSelectElement.TYPE:
                return MultiUsersSelectElement.class;
            case MultiExternalSelectElement.TYPE:
                return MultiExternalSelectElement.class;
            case MultiConversationsSelectElement.TYPE:
                return MultiConversationsSelectElement.class;
            case MultiStaticSelectElement.TYPE:
                return MultiStaticSelectElement.class;
            case OverflowMenuElement.TYPE:
                return OverflowMenuElement.class;
            case DatePickerElement.TYPE:
                return DatePickerElement.class;
            case PlainTextInputElement.TYPE:
                return PlainTextInputElement.class;
            case RichTextSectionElement.TYPE:
                return RichTextSectionElement.class;
            case RichTextListElement.TYPE:
                return RichTextListElement.class;
            case RichTextQuoteElement.TYPE:
                return RichTextQuoteElement.class;
            case RichTextPreformattedElement.TYPE:
                return RichTextPreformattedElement.class;
            case RadioButtonsElement.TYPE:
                return RadioButtonsElement.class;
            case CheckboxesElement.TYPE:
                return CheckboxesElement.class;
            default:
                if (failOnUnknownProperties) {
                    throw new JsonParseException("Unknown block element type: " + typeName);
                } else {
                    return UnknownBlockElement.class;
                }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy