0

I've built a RESTful API using Spray.io. It contains a number of endpoints, each of which returns JSON. I use the Spray JSON marshaller to marshal my internal objects to JSON which are returned to the user. So I have something similar to...

respondWithMediaType(`application/json`) {
        complete(MyResponse(username, password))
      }

Where MyResponse is a case class...

case class MyResponse(username:String, password:String)

However, I want to pick the fields that are returned in the response. So in this case I want to remove the "password" field before it is returned. Whats the best way to do this with Spray?

Thanks

fatlog
  • 1,182
  • 2
  • 14
  • 28
  • 2
    Why not create `PasswordLessResponse` case class, instead of jumping hooks to deserialize `MyResponse` in different ways. – Yuval Itzchakov Aug 09 '16 at 12:36
  • hhhmmm, maybe I'm over thinking this... So I would just return the custom object picking out the fields I need in the complete directive? – fatlog Aug 09 '16 at 12:55
  • That's the way I'd tackle it. You can always create a base trait or abstract class to hold the common fields, have the custom case classes extend the base. That may be an overkill for now, but may be viable down the road. – Yuval Itzchakov Aug 09 '16 at 12:59
  • Worked perfectly. Add as answer? – fatlog Aug 09 '16 at 19:25

1 Answers1

0

Posting an answer to close question. Went with Yuvals suggestion above (he didn't add as answer)...

"Why not create PasswordLessResponse case class, instead of jumping hooks to deserialize MyResponse in different ways"

and just created the relevant case classes and marshallers, populated and returned where needed.

Thanks Yuval!

fatlog
  • 1,182
  • 2
  • 14
  • 28