Skip to content

Commit 40a9404

Browse files
daedalus28Spencer
authored andcommitted
Document serializer configuration option (#727)
* Document serializer configuration option * Improve serializer configuration doc verbiage
1 parent 915e4ca commit 40a9404

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/configuration.asciidoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,31 @@ var client = new elasticsearch.Client({
276276

277277

278278

279+
`serializer`[[config-serializer]]:: `Serializer` -- Override the way that the client serializes the JSON payload sent to elasticsearch. This can be useful if you're using a third party library that needs to convert to "plain" JS objects, such as with https://github.com/elastic/elasticsearch-js/blob/master/src/lib/serializers/angular.js[angular.js]. Another helpful use case is in an advanced scenario where your application assembles queries dynamically. Using a stable stringify in that case ensures property order to prevent cache misses (as outlined in https://github.com/elastic/elasticsearch-js/issues/695[GH Issue 695]).
280+
281+
Default::: see https://github.com/elastic/elasticsearch-js/blob/master/src/lib/serializers/json.js[json.js]
282+
283+
To Use Stable Stringification:::
284+
+
285+
[source,js]
286+
-----
287+
import DefaultJsonSerializer from 'elasticsearch/src/lib/serializers/json';
288+
import jsonStableStringify from 'json-stable-stringify';
289+
290+
class CustomSerializer extends DefaultJsonSerializer {
291+
serialize(val, replacer, space) {
292+
return jsonStableStringify(val, { replacer, space })
293+
}
294+
}
295+
296+
CustomSerializer.prototype.serialize.contentType = 'application/json';
297+
298+
const client = new elasticsearch.Client({
299+
serializer: CustomSerializer
300+
})
301+
-----
302+
303+
279304

280305
=== Examples
281306

0 commit comments

Comments
 (0)