nestelasticsearch-net

v5.5 Elasticsearch Custom Serializer


Is it possible in NEST / Elasticsearch-net 5.5 to make a custom serializer to only work on my own app's defined types and let the built in serializer handle the rest of Elastic packages own types?

I would like to accomplish this because my custom serializer is failing to serialize Elasticsearch NEST queries to json string.


Solution

  • It's possible to define your own JSON.NET serializer with 5.x that will serialize NEST types correctly, and serialize your own types however you like too. It's not so straightforward to do however, which is why JSON.NET was internalized in NEST 6.x :) If you're not using Json.NET, you'll need to do much more work in implementing your own serializer to do this, as your serializer will also need to know how to serialize NEST types.

    For this to work in 5.x, your IContractResolver must derive from ElasticContractResolver to be able to inherit serialization of NEST types. The easier way to inherit however is to derive from JsonNetSerializer and implement the behaviour that you require in a way that does not globally affect all types that will be handled by the serializer.