Snapshot of ArrayList<Foo> deserializes to ArrayList<scala.collection.immutable.Map$Map2>

Hi

I use akka version 2.6.6 with scala version 2.13 for akka-serialization-jackson.

I want to save a snapshot from an AbstractPersistentActor’s state:

ArrayList<Foo> state

where Foo is defined as:

public class Foo {
    public final String name;
    public final double value;

    public Foo(String name, double value) {
        this.name = name;
        this.value = value;
    }
}

Akka tells me the correct serializer is used when saving the snapshot:
Using serializer [akka.serialization.jackson.JacksonCborSerializer] for message [java.util.ArrayList].
This is as expected because I added a serialization-bindings "java.util.List" = jackson-cbor

But when I get the SnapshotOffer and set state to the deserialized snapshot:

state = (ArrayList<Foo>) snapshotOffer.snapshot();

I get an ArrayList<scala$collection$immutable$Map$Map2> instead.

How can I prevent this and configure persistence such that I get an ArrayList<Foo> from the SnapshotOffer?
This should also work with more complex data structures than Foo.

I use inmem journal with local snapshot-store for testing.

Thanks!

I think there is no way for jackson to figure out the right type for the nested object if you use generic type as the top level. Try wrapping the list with a class of your own so that the explicit type is encoded somewhere and have jackson serialize that. And if that doesn’t help, you have a place to attach jackson annotations to give jackson further type hints.

Wrapping the list with a class of my own did the trick.
There is no additional type hint needed.
Thanks a lot @johanandren !

Note: in case you wanna have a List<Object>, make sure to add @JsonTypeInfo annotation to the parameter of your state-wrapper class.
Example mixin (use different value than the JsonTypeInfo.Id.CLASS if security is of concerns):

@JsonCreator
public StateMixin(@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@stateClass") List<Object> state) {}