You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: daprdocs/content/en/developing-applications/local-development/sdk-serialization.md
+198-14
Original file line number
Diff line number
Diff line change
@@ -8,16 +8,40 @@ aliases:
8
8
- '/developing-applications/sdks/serialization/'
9
9
---
10
10
11
-
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](https://dapr.github.io/java-sdk/io/dapr/serializer/DefaultObjectSerializer.html) class, providing JSON serialization.
11
+
Dapr SDKs provide serialization for two use cases. First, for API objects sent through request and response payloads. Second, for objects to be persisted. For both of these cases, a default serialization method is provided in each language SDK.
client.invokeService("myappid", "saySomething", "My Message", HttpExtension.POST).block();
38
+
client.invokeMethod("myappid", "saySomething", "My Message", HttpExtension.POST).block();
18
39
```
19
40
20
-
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.
41
+
{{% /codetab %}}
42
+
43
+
In the example above, the app `myappid` receives a `POST` request for the `saySomething` method with the request payload as
44
+
`"My Message"` - quoted since the serializer will serialize the input String to JSON.
client.saveState("MyStateStore", "MyKey", "My Message").block();
36
80
```
37
-
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.
81
+
82
+
{{% /codetab %}}
83
+
84
+
In this example, `My Message` is saved. It is not quoted because Dapr's API internally parse the JSON request
85
+
object before saving it.
38
86
39
87
```JSON
40
88
[
@@ -47,12 +95,45 @@ In this example, `My Message` will be saved. It is not quoted because Dapr's API
47
95
48
96
## PubSub
49
97
98
+
{{< tabs ".NET" "Java" >}}
99
+
100
+
<!-- .NET -->
101
+
{{% codetab %}}
102
+
103
+
```csharp
104
+
usingvarclient= (newDaprClientBuilder()).Build();
105
+
awaitclient.PublishEventAsync("MyPubSubName", "TopicName", "My Message");
106
+
```
107
+
108
+
The event is published and the content is serialized to `byte[]` and sent to Dapr sidecar. The subscriber receives it as a [CloudEvent](https://github.com/cloudevents/spec). Cloud event defines `data` as String. The Dapr SDK also provides a built-in deserializer for `CloudEvent` object.
DaprClient client = (new DaprClientBuilder()).build();
52
133
client.publishEvent("TopicName", "My Message").block();
53
134
```
54
135
55
-
The event is published and the content is serialized to `byte[]` and sent to Dapr sidecar. The subscriber will receive it as a [CloudEvent](https://github.com/cloudevents/spec). Cloud event defines `data` as String. Dapr SDK also provides a built-in deserializer for `CloudEvent`object.
136
+
The event is published and the content is serialized to `byte[]` and sent to Dapr sidecar. The subscriber receives it as a [CloudEvent](https://github.com/cloudevents/spec). Cloud event defines `data` as String. The Dapr SDK also provides a built-in deserializer for `CloudEvent`objects.
56
137
57
138
```java
58
139
@PostMapping(path="/TopicName")
@@ -62,9 +143,50 @@ The event is published and the content is serialized to `byte[]` and sent to Dap
62
143
}
63
144
```
64
145
146
+
{{% /codetab %}}
147
+
65
148
## Bindings
66
149
67
-
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.
150
+
For output bindings the object is serialized to `byte[]` whereas the input binding receives the raw `byte[]` as-is and deserializes it to the expected object type.
151
+
152
+
{{< tabs ".NET" "Java" >}}
153
+
154
+
<!-- .NET -->
155
+
{{% codetab %}}
156
+
157
+
* Output binding:
158
+
```csharp
159
+
usingvarclient= (newDaprClientBuilder()).Build();
160
+
awaitclient.InvokeBindingAsync("sample", "My Message");
@@ -80,15 +202,49 @@ In this case, the object is serialized to `byte[]` as well and the input binding
80
202
System.out.println(message);
81
203
}
82
204
```
205
+
206
+
{{%/codetab%}}
207
+
83
208
Itshouldprint:
84
209
```
85
210
MyMessage
86
211
```
87
212
88
213
## Actor Method invocation
89
-
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.
214
+
Objectserializationanddeserializationfor Actor method invocation are same as for the service method invocation,
215
+
the only difference is that the application does not need to deserialize the request or serialize the response since it
216
+
is all done transparently by the SDK.
217
+
218
+
For Actor methods, the SDK only supports methods with zero or one parameter.
219
+
220
+
{{< tabs ".NET" "Java" >}}
90
221
91
-
For Actor's methods, the SDK only supports methods with zero or one parameter.
222
+
The .NETSDKsupportstwodifferentserializationtypesbasedonwhetheryou're using strongly-typed (DataContracts)
@@ -105,13 +261,37 @@ public String say(String something) {
105
261
return"OK";
106
262
}
107
263
```
264
+
265
+
{{%/codetab%}}
266
+
108
267
Itshouldprint:
109
268
```
110
269
MyMessage
111
270
```
112
271
113
272
## Actor's state management
114
-
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. Use of basic [JSON data types](https://www.w3schools.com/js/js_json_datatypes.asp) 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.
132
-
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.
313
+
1. Useofbasic [JSONdatatypes](https://www.w3schools.com/js/js_json_datatypes.asp) for cross-language and cross-platform compatibility: string, number, array,
5. 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.
328
+
5. Whenuserprovidesacustomserializer, itshouldbetransferredorpersistedas `byte[]`. Whenpersisting, also
*As of now, the [Java SDK](https://github.com/dapr/java-sdk/) is the only Dapr SDK that implements this specification. In the near future, other SDKs will also implement the same.*
0 commit comments