1
1
[[serializers]]
2
2
== Serializers
3
3
4
- The client has three serializers available. You will most likely never need
5
- to change the serializer, unless you have special requirements or are
4
+ The client has three serializers available. You will most likely never need
5
+ to change the serializer, unless you have special requirements or are
6
6
implementing a new protocol.
7
7
8
- The job of the serializer is to encode the outgoing request body and decode
9
- the incoming response body. In 99% of cases, this is a simple conversion
10
- to/from JSON.
8
+ The job of the serializer is to encode the outgoing request body and decode the
9
+ incoming response body. In 99% of cases, this is a simple conversion to/from
10
+ JSON.
11
+
12
+ The default serializer is the `SmartSerializer`.
11
13
12
- The default serializer is the `SmartSerializer`
13
14
14
15
=== SmartSerializer
16
+
15
17
==== Serialize()
16
- The `SmartSerializer` inspects the data to be encoded. If the request body
17
- is provided as a string, it is passed directly to Elasticsearch as a string.
18
- This allows users to provide raw JSON, or raw strings for certain endpoints that
19
- dont have structure (such as the Analyze endpoint).
20
-
21
- If the data is an array, it is converted to json. If the data provided was an
22
- empty array, the serializer manually converts the JSON from an empty array (`[]`)
23
- to an empty object (`{}`) so that it is valid JSON for Elasticsearch request
18
+
19
+ The `SmartSerializer` inspects the data to be encoded. If the request body is
20
+ provided as a string, it is passed directly to {es} as a string. This allows
21
+ users to provide raw JSON, or raw strings for certain endpoints that don't have
22
+ structure (such as the Analyze endpoint).
23
+
24
+ If the data is an array, it is converted to JSON. If the data provided was an
25
+ empty array, the serializer manually converts the JSON from an empty array
26
+ (`[]`) to an empty object (`{}`) so that it is valid JSON for {es} request
24
27
bodies.
25
28
26
29
==== Deserialize()
27
- When decoding the response body, the `SmartSerializer` introspects the
28
- `content_type` headers to determine the appropriate encoding. If the data is
29
- encoded as JSON, it is decoded into an array using `json_decode`. Otherwise,
30
- it is returned as a string.
31
30
32
- This functionality is required to cooperate with endpoints such as the `Cat`
31
+ When decoding the response body, the `SmartSerializer` introspects the
32
+ `content_type` headers to determine the appropriate encoding. If the data is
33
+ encoded as JSON, it is decoded into an array using `json_decode`. Otherwise, it
34
+ is returned as a string.
35
+
36
+ This functionality is required to cooperate with endpoints such as the `Cat`
33
37
endpoints, which return tabular text instead of JSON.
34
38
39
+
35
40
==== Selecting the SmartSerializer
36
41
37
- The SmartSerializer is selected by default, but if you wish to manually configure it for explicitness, you can
38
- do so by using the `setSerializer()` method on the ClientBuilder object:
42
+ The `SmartSerializer` is selected by default, but if you wish to manually
43
+ configure it for explicitness, you can do so by using the `setSerializer()`
44
+ method on the `ClientBuilder` object:
39
45
40
46
[source,php]
41
47
----
@@ -44,27 +50,36 @@ $client = ClientBuilder::create()
44
50
->build();
45
51
----
46
52
47
- Note that the serializer is configured by specifying a namespace path to the serializer.
53
+ Note that the serializer is configured by specifying a namespace path to the
54
+ serializer.
55
+
48
56
49
57
=== ArrayToJSONSerializer
58
+
50
59
==== Serialize()
51
- The `ArrayToJSONSerializer` inspects the data to be encoded. If the request body
52
- is provided as a string, it is passed directly to Elasticsearch as a string.
53
- This allows users to provide raw JSON, or raw strings for certain endpoints that
54
- dont have structure (such as the Analyze endpoint).
55
-
56
- If the data is an array, it is converted to json. If the data provided was an
57
- empty array, the serializer manually converts the JSON from an empty array (`[]`)
58
- to an empty object (`{}`) so that it is valid JSON for Elasticsearch request
60
+
61
+ The `ArrayToJSONSerializer` inspects the data to be encoded. If the request body
62
+ is provided as a string, it is passed directly to {es} as a string. This allows
63
+ users to provide raw JSON, or raw strings for certain endpoints that don't have
64
+ structure (such as the Analyze endpoint).
65
+
66
+ If the data is an array, it is converted to json. If the data provided was an
67
+ empty array, the serializer manually converts the JSON from an empty array
68
+ (`[]`) to an empty object (`{}`) so that it is valid JSON for {es} request
59
69
bodies.
60
70
71
+
61
72
==== Deserialize()
62
- When decoding the response body, everything is decoded to JSON from JSON. If
63
- the data is not valid JSON, `null` will be returned.
73
+
74
+ When decoding the response body, everything is decoded to JSON from JSON. If the
75
+ data is not valid JSON, `null` will be returned.
76
+
64
77
65
78
==== Selecting the ArrayToJSONSerializer
66
79
67
- You can select `ArrayToJSONSerializer` by using the `setSerializer()` method on the ClientBuilder object:
80
+ You can select `ArrayToJSONSerializer` by using the `setSerializer()` method on
81
+ the `ClientBuilder` object:
82
+
68
83
69
84
[source,php]
70
85
----
@@ -73,26 +88,32 @@ $client = ClientBuilder::create()
73
88
->build();
74
89
----
75
90
76
- Note that the serializer is configured by specifying a namespace path to the serializer.
91
+ Note that the serializer is configured by specifying a namespace path to the
92
+ serializer.
93
+
77
94
78
95
=== EverythingToJSONSerializer
96
+
79
97
==== Serialize()
98
+
80
99
The `EverythingToJSONSerializer` tries to convert everything to JSON.
81
100
82
- If the data provided was an empty array, the serializer manually converts the
101
+ If the data provided was an empty array, the serializer manually converts the
83
102
JSON from an empty array (`[]`) to an empty object (`{}`) so that it is valid
84
103
JSON for Elasticsearch request bodies.
85
104
86
105
If the data was not an array and/or not convertible to JSON, the method returns
87
106
`null`.
88
107
89
108
==== Deserialize()
90
- When decoding the response body, everything is decoded to JSON from JSON. If
91
- the data is not valid JSON, `null` will be returned.
109
+
110
+ When decoding the response body, everything is decoded to JSON from JSON. If the
111
+ data is not valid JSON, `null` is returned.
92
112
93
113
==== Selecting the EverythingToJSONSerializer
94
114
95
- You can select `EverythingToJSONSerializer` by using the `setSerializer()` method on the ClientBuilder object:
115
+ You can select `EverythingToJSONSerializer` by using the `setSerializer()`
116
+ method on the ClientBuilder object:
96
117
97
118
[source,php]
98
119
----
@@ -101,11 +122,14 @@ $client = ClientBuilder::create()
101
122
->build();
102
123
----
103
124
104
- Note that the serializer is configured by specifying a namespace path to the serializer.
125
+ Note that the serializer is configured by specifying a namespace path to the
126
+ serializer.
105
127
106
128
=== Implementing your own Serializer
107
- If you want to use your own custom serializer, you need to implement the `SerializerInterface` interface. Please
108
- keep in mind that the client uses a single Serializer object for all endpoints and all connections.
129
+
130
+ If you want to use your own custom serializer, you need to implement the
131
+ `SerializerInterface` interface. Please keep in mind that the client uses a
132
+ single Serializer object for all endpoints and all connections.
109
133
110
134
111
135
[source,php]
@@ -141,8 +165,8 @@ class MyCustomSerializer implements SerializerInterface
141
165
----
142
166
{zwsp} +
143
167
144
- To then use your custom serializer, you can specify the namespace path in the `setSerializer()` method of the ClientBuilder
145
- object:
168
+ To then use your custom serializer, you can specify the namespace path in the
169
+ `setSerializer()` method of the `ClientBuilder` object:
146
170
147
171
[source,php]
148
172
----
@@ -151,8 +175,9 @@ $client = ClientBuilder::create()
151
175
->build();
152
176
----
153
177
154
- Alternatively, if your serializer has a constructor or further initialization that should occur before given to the
155
- client, you can instantiate an object and provide that instead:
178
+ Alternatively, if your serializer has a constructor or further initialization
179
+ that should occur before given to the client, you can instantiate an object and
180
+ provide that instead:
156
181
157
182
[source,php]
158
183
----
@@ -162,6 +187,4 @@ $mySerializer->setFoo("bar");
162
187
$client = ClientBuilder::create()
163
188
->setSerializer($mySerializer);
164
189
->build();
165
- ----
166
-
167
-
190
+ ----
0 commit comments