leonfs
leonfs

Reputation: 719

spray-json for normal classes (non case) on a List

I'm finding myself in a situation in which I need to serialize into JSON a non case class.

Having a class as:

class MyClass(val name: String) {
  def SaySomething() : String = {
    return "Saying something... "
  }
}

I've created a JsonProtocol for this class:

object MyClassJsonProtocol extends DefaultJsonProtocol {

  implicit object MyClassJsonFormat extends JsonWriter[MyClass] {
  override def write(obj: MyClass): JsValue =
    JsObject(
      "name" -> JsString(obj.name)
    )
  }
}

Later on in the code I import the protocol..

val aListOfMyClasses = List[MyClass]() ... // lets assume that has items and not an empty list
import spray.json._
import MyClassJsonProtocol._

val json = aListOfMyClasses.toJson

When trying to build the project I get the following error:

Cannot find JsonWriter or JsonFormat for type class List[MyClass]

spray-json has already a format for generic list and I'm providing a format for my class, what would be the problem?

Thanks in advance...!!!

Upvotes: 6

Views: 1329

Answers (2)

akauppi
akauppi

Reputation: 18056

The reason seems to be mentioned here:

An issue you might run into with just JsonReader/JsonWriter is that when you try to lookup JsonReader/JsonWriter for Option or a collection, it looks for a JsonFormat for the contained type, which will fail. Not sure if there is something I am missing that will fix that issue.

You and I have run into this. I don't see other way out at the moment than @user007's suggestion to use a full JsonFormat. That, itself, brings more difficulties at least to me - I was planning to use the default reader for my class.

Oh, well...

Upvotes: 0

Vishal John
Vishal John

Reputation: 4382

When I extended MyClassJsonFormat from JsonFormat instead of JsonWriter, it stared working fine. Looks like the CollectionFormats trait will work only if you extend from JsonFormat

The following code compiles fine for me

  object MyClassJsonProtocol extends DefaultJsonProtocol {

    implicit object MyClassJsonFormat extends JsonFormat[MyClass] {
    override def write(obj: MyClass): JsValue =
      JsObject(
        "name" -> JsString(obj.name)
      )

      override def read(json: JsValue): MyClass = new MyClass(json.convertTo[String])
    }
  }

Upvotes: 3

Related Questions