Announcement: The Alexa Skills Community Is Moving To Stack Overflow

For improved usability and experience, Alexa skills related forum support will be transitioned to Stack Overflow. Effective January 10, 2024, the Amazon Developer Forums will no longer be available. For continued Alexa skills support you can reach out to us on Stack Overflow or via Contact Us.

question

Rand M avatar image
Rand M asked

Can I put a Java object in session?

The javadocs ( https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/javadocs/alexa-skills-kit-java-library-reference) show that I should be able to set/get an Object in session: ==== [b]SetAttribute[/b] public void setAttribute(String name, Object value) Add or modify the attribute with the provided name. Parameters: name - the name of the attribute to set value - the new value for the attribute === However, when I try getting my object from session, I get a ClassCastException - the logs show it is trying to cast a LinkedHashMap to my object (Googling showed it has something to do with Jackson, which I don't directly use). My work-around is to just set/get Strings in session, which works fine and then recreate the object - this is ok for trivial use, but I'd like to be able to store and retrieve objects. Any idea what I'm doing wrong? Thank you.
alexa skills kitdebugging
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jjaquinta avatar image
jjaquinta answered
Ultimately what you are sending and receiving in the protocol is JSON. So it probably only supports simple types such as Strings, Integers, Boolean, etc. I'd stick with those. If you want to do more than that, write your own routines to serialize your POJO into a string, and deserialize it when you get the session object back. Play it safe. Also remember that Alexa drops sessions all the time. Unless you are writing a lambda function, you are better off persisting session data yourself in a global map indexed by the User ID. (You don't have globals in Lambda, so you are forced to use their session object.) In any event, anything significant or compelx, I wouldn't trust their session to. I'd push it off into a DynamoDB or something.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rand M avatar image
Rand M answered
Thanks a lot, Jo, that definitely helps, I appreciate it. You bring up several points that I want to get clarification on. Since they dovetail into my next (larger) question, I'll write them separately with the hopes that it might benefit some people...
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.