Alpakka Elasticsearch and JSON serialization

I noticed that Elasticsearch alpakka uses spray-json to serialize objects. ElasticsearchFlow.create, for example, needs implicit sprayJsonWriter: JsonWriter[T]

def create[T](indexName: String,
                typeName: String,
                settings: ElasticsearchWriteSettings = ElasticsearchWriteSettings.Default)(
      implicit elasticsearchClient: RestClient,
      sprayJsonWriter: JsonWriter[T]

In our project we use Json4s and have lots of custom serializers for our custom types. I’m considering using Elasticsearch alpakka. So, my question is: is there a way I can use Json4s as a (de)serialization tool in Elasticsearch alpakka?

I think there is always an inefficient but working solution namely: .toString.parse
spray-json -> json4s => parse(sjson.toString)
json4s -> spray-json => render(j4sjson).parseJson
I think you can also write these to a SprayJsonFormatter, so it will happen implicitly.

1 Like

This is exactly what I needed. Thank you for your advice.

Hi @SirMullich

Please note that you may avoid double conversion by using String directly in your write messages.
See https://doc.akka.io/docs/alpakka/current/elasticsearch.html#storing-documents-from-strings

Cheers,
Enno.