How to fix the error
You must add an implicit parameter to your method as follows:
def buildJsArray[T](l: List[T])(result: JsArray)(implicit tjs: Writes[T]): JsArray
Json.toJson has such a parameter.
The reason you need to add this parameter is because you know how to convert T to json only when you know what T . This means that you have a way to serialize T only when calling buildJsArray , and this parameter allows you to pass this serialization method to the buildJsArray method.
How to create JSArray
You can just use the JsArray constructor. Seq[JsValue] :
new JsArray(l.map{Json.toJson(_)})
Implicit Writes already exist for Traversable , so you don't need your own buildJsArray method, you could just use the Json.toJson method with a parameter of type List[T] .
Adding
You should look at the api collection. This allows you to write more readable and shorter code:
def buildJsArray[T](l: List[T])(implicit tjs: Writes[T]): JsArray = l.foldLeft(new JsArray){ (r, x) => r :+ Json.toJson(x) }
source share