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

io.klerch.alexa.state.model.serializer.AlexaStateSerializer Maven / Gradle / Ivy

Go to download

This SDK is an extension to the Alexa Skills SDK for Java. It provides a framework for managing state of POJO models in a variety of persistence stores in an Alexa skill.

There is a newer version: 1.1.0
Show newest version
/**
 * Made by Kay Lerch (https://twitter.com/KayLerch)
 *
 * Attached license applies.
 * This library is licensed under GNU GENERAL PUBLIC LICENSE Version 3 as of 29 June 2007
 */
package io.klerch.alexa.state.model.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.klerch.alexa.state.utils.AlexaStateException;
import io.klerch.alexa.state.model.AlexaScope;
import io.klerch.alexa.state.model.AlexaStateModel;

import java.io.IOException;
import java.lang.reflect.Field;

/**
 * This custom JSON serializer considers all fields tagged with the AlexaStateSave annotation in a given scope.
 * The scope is read out from an attribute which comes with the provider and is set by another Serializer extending
 * the abstract class.
 */
public abstract class AlexaStateSerializer extends JsonSerializer {
    final String scopeAttributeKey = "AlexaScope";

    @Override
    public void serialize(AlexaStateModel alexaStateModel, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        {
            final AlexaScope scope = (AlexaScope)serializerProvider.getAttribute(scopeAttributeKey);
            jsonGenerator.writeObjectField("id", alexaStateModel.getId());
            // look for statesave fields in model if not already whole class is statesave
            for (final Field field : alexaStateModel.getSaveStateFields(scope)) {
                final String fieldName = field.getName();
                Object fieldValue;
                try {
                    fieldValue = alexaStateModel.get(field);
                } catch (AlexaStateException e) {
                    // wrap custom exception with expected IOException
                    throw new IOException("Could not get value of field " + fieldName, e);
                }
                jsonGenerator.writeObjectField(fieldName, fieldValue);
            }
        }
        jsonGenerator.writeEndObject();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy