title: How to implement implicit Json Writes of embedded object in Play Framework 2.x
body: <p>there are two classes <code>Foo</code> and <code>Bar</code>. <code>Foo</code> contains a field of <code>Bar</code>. The question is, how do I implement an implicit json <code>Writes</code> for class <code>Foo</code>?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>package models
  
  import play.api.libs.json._
  
  case class Foo(id: String, bar: Bar)
  
  object Foo {
    implicit val implicitFooWrites = new Writes[Foo] {
      def writes(foo: Foo): JsValue = {
        Json.obj(
          "id" -&gt; foo.id,
          "bar" -&gt; foo.bar
        )
      }
    }
  }
  
  case class Bar(x: String, y: Int)
  
  object Bar {
    implicit val implicitBarWrites = new Writes[Bar] {
      def writes(bar: Bar): JsValue = {
        Json.obj(
          "x" -&gt; bar.x,
          "y" -&gt; bar.y
        )
      }
    }
  }
  </code></pre>
  
  <p>When I try to compile, I get the following error:</p>
  
  <blockquote>
    <p>No Json deserializer found for type models.Bar. Try to implement an
    implicit Writes or Format for this type.</p>
  </blockquote>
  
  <p>I don't understand this compiler error, since I implemented an implicit Writes for models.Bar class. What is the problem here?</p>