title: JSON serialisers/deserialisers Play Framework
body: <p>I want create JSON serialisers/deserialisers for a Play Framework project, this is my code:</p>
  
  <pre><code>    object ClientConnection {
  
        /**
         * Events to/from the client side
         */
        sealed trait ClientEvent
  
        case class UserPing() extends ClientEvent
  
        /**
         * Event sent from the client when they have moved
         */
        case class UserMoved(position: Point[LatLng]) extends ClientEvent
  
      /**
         * Formats WebSocket frames to be ClientEvents.
         */
        implicit def clientEventFrameFormatter: FrameFormatter[ClientEvent] = FrameFormatter.jsonFrame.transform(
          clientEvent =&gt; Json.toJson(clientEvent),
          json =&gt; Json.fromJson[ClientEvent](json).fold(
            invalid =&gt; throw new RuntimeException("Bad client event on WebSocket: " + invalid),
            valid =&gt; valid
          )
        )
  
  /**
     * JSON serialisers/deserialisers for the above messages
     */
    implicit def clientEventFormat: Format[ClientEvent] = Format(
      (__ \ "event").read[String].flatMap {
        case "user-moved" =&gt; UserMoved.userMovedFormat.map(identity)
        case "user-ping" =&gt; UserPing.userPingFormat.map(identity)
        case other =&gt; Reads(_ =&gt; JsError("Unknown client event: " + other))
      },
      Writes {
        case um: UserMoved =&gt; UserMoved.userMovedFormat.writes(um)
        case pi: UserPing =&gt; UserPing.userPingFormat.writes(pi)
      }
    )
  
    object UserMoved {
      implicit def userMovedFormat: Format[UserMoved] = (
        (__ \ "event").format[String] and
            (__ \ "position").format[Point[LatLng]]
        ).apply({
        case ("user-moved", position) =&gt; UserMoved(position)
      }, (userMoved: UserMoved) =&gt; ("user-moved", userMoved.position))
    }
  </code></pre>
  
  <p>Now my question is, how I can map the Ping request, that have a JSON format with the only 1 key like this:</p>
  
  <pre><code>{ "event" : "user-ping"}
  </code></pre>
  
  <p>I have tried to do this:</p>
  
  <pre><code>object UserPing {
      implicit def userPingFormat: Format[UserPing] = (
        (__ \ "event").format[String]
        ).apply({
        case ("user-ping") =&gt; UserPing()
      }, (userPing: UserPing) =&gt; ("user-ping"))
    }
  </code></pre>
  
  <p>but give me an error compiling, how can I do it?</p>