0

I have the following function (which works on a protobuf object MyRequest

  def createRequestFromJson(requestJson: String): MyRequest = {
    val protoJson = getResource(requestJson)
    JsonFormat.fromJsonString[MyRequest](protoJson)
  }

I want to reuse this function with a different object so I added a type

  def createRequestFromJson[A](requestJson: String): A = {
    val protoJson = getResource(requestJson)
    JsonFormat.fromJsonString[A](protoJson)
  }

but then I get an error

Error:(68, 30) type arguments [A] do not conform to method fromJsonString's type parameter bounds [A <: scalapb.GeneratedMessage with scalapb.Message[A]]
JsonFormat.fromJsonString[A](protoJson)

I tried changing the definition to

  def createResponseFromJson[A <: scalapb.GeneratedMessage with scalapb.Message[A]](protoJsonFile: String): A = {

but still gives more errors

What am I doing wrong?

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

1 Answers1

0

JsonFormat.fromJsonString needs an implicit GeneratedMessageCompanion. It would work if you change the signature to:

def createResponseFromJson[A <: scalapb.GeneratedMessage with scalapb.Message[A]
     : GeneratedMessageCompanion](protoJsonFile: String): A
thesamet
  • 6,382
  • 2
  • 31
  • 42