In our project we are using ReactiveMongo
with Play 2.2.1
.
The problem is, that to stream of data in a form of Enumerator[A]
, returned by ReactiveMongo
is actually a stream of value objects, which are not separated by commas, and do not have stream beginning and end annotations, which can be treated as array opening and close statements.
This creates a problem for JSON
consumer JS client
, since the expected format is
[A1,A2, ...]
So we jumped in hoops, and transformed our Enumeratee[A]
to Enumerator[String]
, with checking, if it's the first element, or not:
var first:Boolean = true
val aToStrs = (as.map(a => {
if(first) {
first = false;
Json.stringify(Json.toJson(a))
} else {
"," + Json.stringify(Json.toJson(a))
}
}))
Ok.chunked(
Enumerator.enumInput(Input.El("[")) andThen
aToStrs andThen
Enumerator.enumInput(Input.El("]")) andThen
Enumerator.enumInput(Input.EOF)
)
This works, but feels like inventing the wheel.
Is there a better solution, for this common problem?