0

How can I convert the following case class object to JSON response, the case class contains multiple nested object value.

I got exception when I use jsonFormat5(responseData)

val simpleBanner1 = Banner(112, "http://dummyimage.com/300x250", 300, 250)
val responseData = BidResponse(uuid, "XN2zZQABxJsKK0jU4QnIzw", campaignData.bid, Option("4548"), Option(simpleBanner1))

Exception

Error:(63, 47) could not find implicit value for evidence parameter of type bidder.RestService.JF[Option[bidder.Banner]]
implicit val bidResponseFormat = jsonFormat5(BidResponse)

Error:(63, 47) not enough arguments for method jsonFormat5: (implicit evidence$25: bidder.RestService.JF[String], implicit evidence$26: bidder.RestService.JF[String], implicit evidence$27: bidder.RestService.JF[Double], implicit evidence$28: bidder.RestService.JF[Option[String]], implicit evidence$29: bidder.RestService.JF[Option[bidder.Banner]], implicit evidence$30: scala.reflect.ClassTag[bidder.BidResponse])spray.json.RootJsonFormat[bidder.BidResponse].
Unspecified value parameters evidence$29, evidence$30.
implicit val bidResponseFormat = jsonFormat5(BidResponse)

Error:(76, 28) Cannot find JsonWriter or JsonFormat type class for bidder.BidResponse
          responseData.toJson
Error:(76, 28) not enough arguments for method toJson: (implicit writer: spray.json.JsonWriter[bidder.BidResponse])spray.json.JsValue.
Unspecified value parameter writer.
          responseData.toJson
0

1 Answer 1

1

jsonFormat5 should be used with the case class, as it defines how to the object should be encoded. Your code should look like this:

import spray.json._
import DefaultJsonProtocol._

// define format implicitly for BidResponse and Banner case classes
implicit val bannerFormat = jsonFormat4(Banner)
implicit val bidResponseFormat = jsonFormat5(BidResponse)

// serialize BidResponse object
val bidResponseJson = responseData.toJson

Here is an extended example:

import spray.json._
import DefaultJsonProtocol._

// example case classes
case class Banner(num: Int, url: String, num2: Int, num3: Int)
case class BidResponse(uuid: String, code: String, num: Int, opt: Option[String], banner: Option[Banner])

implicit val bformat = jsonFormat4(Banner)
implicit val format = jsonFormat5(BidResponse)

val simpleBanner1 = Banner(112, "http://dummyimage.com/300x250", 300, 250)
val responseData = BidResponse("abcdef", "XN2zZQABxJsKK0jU4QnIzw", 1234, Option("4548"), Option(simpleBanner1))

println( responseData.toJson )

As mentioned in the comments, the Banner format must be defined implicitly because the BidResponse object depends on it. ref: bformat variable.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi @terminally-chill, I already tried it before, got exception Error:(63, 47) could not find implicit value for evidence parameter of type bidder.RestService.JF[Option[bidder.Banner]] implicit val bidResponseFormat = jsonFormat5(BidResponse)
Error:(63, 47) not enough arguments for method jsonFormat5: (implicit evidence$25: bidder.RestService.JF[String], implicit evidence$26: bidder.RestService.JF[String], implicit evidence$27: bidder.RestService.JF[Double], implicit evidence$28: bidder.RestService.JF[Option[String]], implicit evidence$29: bidder.RestService.JF[Option[bidder.Banner]], implicit evidence$30: scala.reflect.ClassTag[bidder.BidResponse])spray.json.RootJsonFormat[bidder.BidResponse]. Unspecified value parameters evidence$29, evidence$30. implicit val bidResponseFormat = jsonFormat5(BidResponse)
@AbdulAwal you should add Banner as an implicit val as well. Updated the answer showing both in scope.
I update it as implicit val simpleBanner1 = Banner(112, "http://dummyimage.com/300x250", 300, 250) but the error remain the same
@AbdulAwal Can you include the case class definitions? I can re-create this exception when using the wrong jsonFormat function. Are there any default values in the case class definition?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.