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: docs/client-concepts/serialization/custom-serialization.asciidoc
+55-10
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
-
[[custom-serialization]]
2
-
==== Custom serialization
1
+
[[customizing-source-serialization]]
2
+
==== Customizing source serialization
3
3
4
4
The built-in source serializer handles most POCO document models correctly. Sometimes, you may need further control over how your types are serialized.
5
5
@@ -8,19 +8,19 @@ NOTE: The built-in source serializer uses the Microsoft https://learn.microsoft.
8
8
[[system-text-json-attributes]]
9
9
===== Using `System.Text.Json` attributes
10
10
11
-
`System.Text.Json` includes attributes that can be applied to types and properties to control how they are serialized. These can be applied to your POCO document types to perform actions such as controlling the name of a property, or ignoring a property entirely. Visit the https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/overview[Microsoft documentation for further examples].
11
+
`System.Text.Json` includes attributes that can be applied to types and properties to control their serialization. These can be applied to your POCO document types to perform actions such as controlling the name of a property or ignoring a property entirely. Visit the https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/overview[Microsoft documentation for further examples].
12
12
13
-
We can model a document to represent data about a person using a regular class (POCO), applying `System.Text.Json`` attributes as necessary.
13
+
We can model a document to represent data about a person using a regular class (POCO), applying `System.Text.Json` attributes as necessary.
<1> the `JsonPropertyName` attribute is used to provide a specific name (`forename`) for the `FirstName` property when serialized.
21
-
<2> the `JsonIgnore` attribute is used to prevent the `Age` property from appearing in the serialized JSON.
20
+
<1> The `JsonPropertyName` attribute is used to ensure the `FirstName` property uses the JSON property name `forename` when serialized.
21
+
<2> The `JsonIgnore` attribute is used to prevent the `Age` property from appearing in the serialized JSON.
22
22
23
-
We can then index the an instance of the document into {es}.
23
+
We can then index an instance of the document into {es}.
24
24
25
25
[source,csharp]
26
26
----
@@ -45,11 +45,56 @@ TODO
45
45
[[configuring-custom-jsonserializeroptions]]
46
46
===== Configuring custom `JsonSerializerOptions`
47
47
48
-
TODO
48
+
The default source serializer applies a set of standard `JsonSerializerOptions` when serializing source document types. In some circumstances, you may wish to override some of our defaults. This is possible by creating an instance of `DefaultSourceSerializer` and passing an `Action<JsonSerializerOptions>` which is applied after our defaults have been set. This mechanism allows you to apply additional settings, or change the value of our defaults.
49
+
50
+
The `DefaultSourceSerializer` includes a constructor that accepts the current `IElasticsearchClientSettings` and a `configureOptions` `Action`.
51
+
52
+
[source,csharp]
53
+
----
54
+
public DefaultSourceSerializer(IElasticsearchClientSettings settings, Action<JsonSerializerOptions> configureOptions);
55
+
----
56
+
57
+
Our application defines the following `Person` class, which models a document we will index to {es}.
We want to serialize our source document using Pascal Casing for the JSON properties. Since the options applied in the `DefaultSouceSerializer` set the `PropertyNamingPolicy` to `JsonNamingPolicy.CamelCase`, we must override this setting.
<1> A local function can be defined, accepting a `JsonSerializerOptions` parameter. Here, we set `PropertyNamingPolicy` to `null`. This returns to the default behavior for `System.Text.Json`, which uses Pascal Case.
77
+
<2> When creating the `ElasticsearchClientSettings`, we supply a `SourceSerializerFactory` using a lambda. The factory function creates a new instance of `DefaultSourceSerializer`, passing in the `settings` and our `ConfigureOptions` local function. We have now configured the settings with a custom instance of the source serializer.
78
+
79
+
The `Person` instance is serialized, with the source serializer serializing the POCO property named `FirstName` using Pascal Case.
80
+
81
+
[source,javascript]
82
+
----
83
+
{
84
+
"FirstName": "Steve"
85
+
}
86
+
----
87
+
88
+
As an alternative to using a local function, we could store an `Action<JsonSerializerOptions>` into a variable instead, which can be passed to the `DefaultSouceSerializer` constructor.
0 commit comments