0

Okay so.... I have this:

  def convertPost  = extract {
    _.request.entity.asString.parseJson.convertTo[CustomClass]
  }

  private def myRoute: Route =
    (post & terminalPath("routeness")) {
      convertPost { req =>
        detach() {
           ThingHandler.getMyResults( req )
        }
      }
    }

but I want to template it, like this:

  def convertPost[T] = extract {
    _.request.entity.asString.parseJson.convertTo[T]
  }

  private def myRoute: Route =
    (post & terminalPath("routeness")) {
      convertPost[CustomClass] { req =>
        detach() {
           ThingHandler.getMyResults( req )
        }
      }
    }

But that doesn't work. I am using spray-json-shapeless. My error is

Error:(28, 50) Cannot find JsonReader or JsonFormat type class for T
    _.request.entity.asString.parseJson.convertTo[T]
                                                 ^

when I try:

  def getStuff[T] = extract {
    _.request.entity.asInstanceOf[T] // .convertTo[T]
  }

it gives:

spray.http.HttpEntity$NonEmpty cannot be cast to com.stuff.CustomClass
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
  • Why don't you use `entity(as[CustomClass])`? – Mustafa Simav Jun 02 '16 at 12:13
  • Some of my json has more than 22 elements and I can only get entity as to work with a json protocol class that uses jsonformat. I know work arounds but I feel like this should be ok if I just knew generics better. – gloomy.penguin Jun 02 '16 at 13:05
  • What `entity(as[CustomClass])` does is to parse your JSON entity as CustomClass and it is exactly what `getStuff` wants to do. Implementation of JsonFormat is another issue, you can still implement custom JsonFormat. – Mustafa Simav Jun 02 '16 at 14:18
  • With json format I have to break things into sub classes/objects and I'd rather not. I have it written the other way. I just think this is better - if I could figure it out. – gloomy.penguin Jun 02 '16 at 16:37

0 Answers0