Search code examples
akka-stream

Akka Streams : add chars at the beggining and end of a Source


I have a json objects source,stored as strings that I'd like to render as a JSON array.

I'm doing this :

source.intersperse(",\n").concat(Source.single("]").prepend(Source.single("[")))

It does not seems to work , I never see the [ and ] char in the output.

Also, how can I say how can I tell Akka Streams that the end of stream is reached (I know the ending message), so it can add the ending char ? (I can know it's done reading a specific message in Kafka).

Thanks


Solution

  • This is working :

    source.takeWhile(_.value != "EOF").intersperse("[", ",\n","]")
    

    Note : of course, you need to have a EOF string at the end of your source to make this example work.