Serialization in Dapr’s SDKs

How Dapr serializes data within the SDKs

An SDK for Dapr should provide serialization for two use cases. First, for API objects sent through request and response payloads. Second, for objects to be persisted. For both these use cases, a default serialization is provided. In the Java SDK, it is the DefaultObjectSerializer class, providing JSON serialization.

Service invocation

  1. DaprClient client = (new DaprClientBuilder()).build();
  2. client.invokeService("myappid", "saySomething", "My Message", HttpExtension.POST).block();

In the example above, the app will receive a POST request for the saySomething method with the request payload as "My Message" - quoted since the serializer will serialize the input String to JSON.

  1. POST /saySomething HTTP/1.1
  2. Host: localhost
  3. Content-Type: text/plain
  4. Content-Length: 12
  5. "My Message"

State management

  1. DaprClient client = (new DaprClientBuilder()).build();
  2. client.saveState("MyStateStore", "MyKey", "My Message").block();

In this example, My Message will be saved. It is not quoted because Dapr’s API will internally parse the JSON request object before saving it.

  1. [
  2. {
  3. "key": "MyKey",
  4. "value": "My Message"
  5. }
  6. ]

PubSub

  1. DaprClient client = (new DaprClientBuilder()).build();
  2. client.publishEvent("TopicName", "My Message").block();

The event is published and the content is serialized to byte[] and sent to Dapr sidecar. The subscriber will receive it as a CloudEvent. Cloud event defines data as String. Dapr SDK also provides a built-in deserializer for CloudEvent object.

  1. @PostMapping(path = "/TopicName")
  2. public void handleMessage(@RequestBody(required = false) byte[] body) {
  3. // Dapr's event is compliant to CloudEvent.
  4. CloudEvent event = CloudEvent.deserialize(body);
  5. }

Bindings

In this case, the object is serialized to byte[] as well and the input binding receives the raw byte[] as-is and deserializes it to the expected object type.

  • Output binding:
  1. DaprClient client = (new DaprClientBuilder()).build();
  2. client.invokeBinding("sample", "My Message").block();
  • Input binding:
  1. @PostMapping(path = "/sample")
  2. public void handleInputBinding(@RequestBody(required = false) byte[] body) {
  3. String message = (new DefaultObjectSerializer()).deserialize(body, String.class);
  4. System.out.println(message);
  5. }

It should print:

  1. My Message

Actor Method invocation

Object serialization and deserialization for invocation of Actor’s methods are same as for the service method invocation, the only difference is that the application does not need to deserialize the request or serialize the response since it is all done transparently by the SDK.

For Actor’s methods, the SDK only supports methods with zero or one parameter.

  • Invoking an Actor’s method:
  1. public static void main() {
  2. ActorProxyBuilder builder = new ActorProxyBuilder("DemoActor");
  3. String result = actor.invokeActorMethod("say", "My Message", String.class).block();
  4. }
  • Implementing an Actor’s method:
  1. public String say(String something) {
  2. System.out.println(something);
  3. return "OK";
  4. }

It should print:

  1. My Message

Actor’s state management

Actors can also have state. In this case, the state manager will serialize and deserialize the objects using the state serializer and handle it transparently to the application.

  1. public String actorMethod(String message) {
  2. // Reads a state from key and deserializes it to String.
  3. String previousMessage = super.getActorStateManager().get("lastmessage", String.class).block();
  4. // Sets the new state for the key after serializing it.
  5. super.getActorStateManager().set("lastmessage", message).block();
  6. return previousMessage;
  7. }

Default serializer

The default serializer for Dapr is a JSON serializer with the following expectations:

  1. Use of basic JSON data types for cross-language and cross-platform compatibility: string, number, array, boolean, null and another JSON object. Every complex property type in application’s serializable objects (DateTime, for example), should be represented as one of the JSON’s basic types.
  2. Data persisted with the default serializer should be saved as JSON objects too, without extra quotes or encoding. The example below shows how a string and a JSON object would look like in a Redis store.
  1. redis-cli MGET "ActorStateIT_StatefulActorService||StatefulActorTest||1581130928192||message
  2. "This is a message to be saved and retrieved."
  1. redis-cli MGET "ActorStateIT_StatefulActorService||StatefulActorTest||1581130928192||mydata
  2. {"value":"My data value."}
  1. Custom serializers must serialize object to byte[].
  2. Custom serializers must deserialize byte[] to object.
  3. When user provides a custom serializer, it should be transferred or persisted as byte[]. When persisting, also encode as Base64 string. This is done natively by most JSON libraries.
  1. redis-cli MGET "ActorStateIT_StatefulActorService||StatefulActorTest||1581130928192||message
  2. "VGhpcyBpcyBhIG1lc3NhZ2UgdG8gYmUgc2F2ZWQgYW5kIHJldHJpZXZlZC4="
  1. redis-cli MGET "ActorStateIT_StatefulActorService||StatefulActorTest||1581130928192||mydata
  2. "eyJ2YWx1ZSI6Ik15IGRhdGEgdmFsdWUuIn0="

As of now, the Java SDK is the only Dapr SDK that implements this specification. In the near future, other SDKs will also implement the same.

Last modified August 2, 2021 : Fix Java SDK link (#1695) (2c67fd1)