spray / spray-json

A lightweight, clean and simple JSON implementation in Scala
Apache License 2.0
972 stars 190 forks source link

Can't use CollectionFormats with types which has only JsonWriter, not entire JsonFormat #187

Open Tolsi opened 8 years ago

Tolsi commented 8 years ago

All implicit methods of CollectionFormats trait contains JsonFormat type bounds. Why? I think it's not uncomfortable and confuse, because I can have only JsonReader/JsonWriter. For example:

package test

import spray.json._
import spray.json.DefaultJsonProtocol._

object Test extends App {
  case class A(a: String)
  implicit object AJsonWriter extends JsonWriter[A] {
    override def write(obj: A): JsValue = { JsObject() }
  }
  Seq(A("A")).toJson
}

There will be a compile-time errors: "Cannot find JsonWriter or JsonFormat type class for Seq[test.Test.A]." and "not enough arguments for method toJson: (implicit writer: spray.json.JsonWriter[Seq[test.Test.A]])spray.json.JsValue. Unspecified value parameter writer." :< To understand what was going on, I can use -Xlog-implicits:

seqFormat is not a valid implicit value for spray.json.JsonWriter[Seq[test.Test.A]] because:
hasMatchingSymbol reported error: could not find implicit value for evidence parameter of type spray.json.JsonFormat[test.Test.A]

But I'm not going to read this object here, I do not need JsonFormat really.

RichardBradley commented 7 years ago

Similarly, the ProductFormatsInstances which create JsonFormats for Case Classes from JsonFormats for their components only work for read/write formats, not for write-only JsonWriters

RichardBradley commented 7 years ago

This looks like a duplicate of #51