Question: I have a recursive data structure that I want to write a custom spray-json serializer for.

```
case class Counts(var count: Int, var properties: mutable.Map[String, Counts])

object MyJsonProtocol extends DefaultJsonProtocol {
  import DefaultJsonProtocol._

  implicit object CountsJsonFormat extends RootJsonFormat[Counts] {
    def read(json: JsValue) = ???
    def write(c: Counts) = {
      // Flatten count and properties into the same object.
      val properties = c.properties.toJson.asJsObject
      val fields = properties.fields + ("count" -> JsNumber(c.count))
      JsObject(fields.toSeq: _*)
    }
  }
}

```

I've seen the documentation for how to do this for a case class if you use the builtin serialization logic, but I have no idea how to apply that to a custom serializer. I get this compiler error:

```
Cannot find JsonWriter or JsonFormat type class for scala.collection.mutable.Map[String,co.asku.acuity.EventCounter.Counts]
val properties = c.properties.toJson.asJsObject
                              ^

```

Answer: 
spray-json formats can't handle mutable Maps by default (see [this disussion](https://groups.google.com/forum/#!msg/spray-user/vstZ4LibCPY/h6FfuUPxrFgJ) that has happened a while ago on the mailing list). Change the type of `properties` to be an immutable Map (which I think is better anyways) and your format will work as expected.
To add to edi's answer, using `toMap` would have worked in the example I posted, to convert the mutable map to immutable.

However, I actually ran into a more complex usecase using nested mutable maps, so I just added a format to serialize them like this:

```
object JsonProtocol extends DefaultJsonProtocol {
  import DefaultJsonProtocol._

  implicit def mutableMapFormat[K :JsonFormat, V :JsonFormat] = new RootJsonFormat[mutable.Map[K, V]] {
    def read(value: JsValue) = ???
    def write(m: mutable.Map[K, V]) = m.toMap.toJson
  }

  implicit object CountsJsonFormat extends RootJsonFormat[Counts] {
    // ...
  }
}

```