bsky
bsky

Reputation: 20242

Serializer not found

I am currently doing a Play tutorial. I have defined a controller Event like this:

package controllers

import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}

import com.semisafe.ticketoverlords.Event

class Event extends Controller{

  def list = Action { request =>
    val events: Seq[Event] = ???
    Ok(Json.toJson(events))
  }
}

Then, in the package com.semisafe.ticketoverlords, I defined the corresponding object and case class:

package com.semisafe.ticketoverlords

import org.joda.time.DateTime
import play.api.libs.json.{Json, Format}

case class Event(
  id: Long,
  name: String,
  start: DateTime,
  end: DateTime,
  address: String,
  city: String,
  state: String,
  country: String
)


object Event {
  implicit val format: Format[Event] = Json.format[Event]
}

I get the following compilation error:

No Json serializer found for type Seq[controllers.Event]. Try to implement an implicit Writes or Format for this type.

On the line:

Ok(Json.toJson(events))

But this should not be the case, because I have defined a formatter and I am importing the class which contains that formatter.

Why is this error occurring?

Upvotes: 0

Views: 485

Answers (1)

John K
John K

Reputation: 1285

Your implicit formatter doesn't seem to be visible to the piece of code that needs it. I am not sure the Event class in you Controller is the one you think: it might be a reference to the controller itself, as it is also named Event. Try to rename your Controller to something like EventController and import the implicit formatter if needed.

Upvotes: 1

Related Questions