Serialization options for Python services

You do not need to handle serialization for messages. Akka Serverless functions serve gRPC interfaces, and the input and output messages are protobuf messages that get serialized to the protobuf format. However, for objects to be persisted such as Event Sourced events and snapshots you have a choice.

By default, Akka Serverless will automatically detect if an emitted event or snapshot is a protobuf and handle them correctly, so we recommend using the default behavior. However, if you have specific requirements, you also can choose between primitive type and JSON (Python Object Notation) serialization.

As a Python developer, JSON is likely more familiar to you. When creating Event Sourced Entities, you can configure serialization for primitives or for Json, using serializeAllowPrimitives or serializeFallbacktoJson, respectively.

Setting the serialization flag

The following example sets JSON serialization fo a snapshot:

const EventSourcedEntity = require("@lightbend/akkaserverless-python-sdk").EventSourcedEntity;


const entity = new EventSourcedEntity(
  ["shoppingcart.proto", "domain.proto"],
  "com.example.shoppingcart.ShoppingCart",
  {
    persistenceId: "shopping-cart",
    snapshotEvery: 5, // Usually you wouldn't snapshot this frequently, but this helps to demonstrate snapshotting
    includeDirs: ["./"],
    serializeFallbackToJson: true // Enables JSON support for persistence
  }
);