Lazy formatted recursive JSON type cannot be found as an implicit value

I use Spray to create a REST API. One of my JSON data types is recursive:

case class Container(id: String,
                 name: String,
                 read_only: Boolean,
                 containers: List[Container],
                 content: List[Content])

object PeriusJsonProtocol extends DefaultJsonProtocol {
  implicit val contentFormat = jsonFormat8(Content)
  implicit val containerFormat: JsonFormat[Container] = lazyFormat(jsonFormat5(Container))
}

When completing the route in the HttpService, I get the following error:

Error:(49, 18) could not find implicit value for parameter um: spray.httpx.unmarshalling.FromRequestUnmarshaller[Container]
        entity(as[Container]) { container =>
                 ^

Error:(49, 18) not enough arguments for method as: (implicit um: spray.httpx.unmarshalling.FromRequestUnmarshaller[Container])spray.httpx.unmarshalling.FromRequestUnmarshaller[Container].
Unspecified value parameter um.
        entity(as[Container]) { container =>
                 ^

HttpService is as follows:

import akka.actor.Actor
import spray.routing._
import spray.httpx.SprayJsonSupport._

import PeriusJsonProtocol._

class RestServiceActor extends Actor with RestService {
  def actorRefFactory = context
  def receive = runRoute(projectsRoute)
}

// this trait defines our service behavior independently from the service actor
trait RestService extends HttpService {
  private val PROJECTS  = "projects"

  val projectsRoute =
    path(PROJECTS) {
      get {
        complete(MongoUtil.getAll(PROJECTS, META) map ContainerMap.fromBson)
        //complete(Container("test", "name", false, List(), List()))
      } ~ post {
        entity(as[Container]) { 
          //complete(MongoUtil.insertProject(container))
          complete(container)
        }
      }
    }
}

Works completein GET, although this function returns a list of containers. However, the commented line in GETdoes not work, and it does not work to implicitly convert the container to post, as can be seen from the error messages. What can I do to make the container work implicitly and maintain a recursive structure?

+4
source share
1 answer

Container json, :

implicit val containerFormat: RootJsonFormat[Container] = rootFormat(lazyFormat(jsonFormat5(Container)))
+4

Source: https://habr.com/ru/post/1611641/


All Articles