I imagine that when you try to deserialize your JSON value, you have the logged in user's ID available somewhere. If so, what about doing something like:
implicit def groupReads(implicit groupOwnerId: Long): Reads[models.GroupDAL.Group] = (
(JsPath \ "name").read[String] and
(JsPath \ "avatarLink").readNullable[String]
)(models.GroupDAL.Group.apply(0L,_,_,groupOwnerId))
which allows you to write
val group: Option[Group] = Json.parse(jsonString).asOpt[Group]
as long as the groupOwnerId
is available in the implicit scope. However, in this case you probably don't want to use a Long
for the groupOwnerId
, since this might lead to ambiguous implicit values. A way around this is to wrap it inside a value class, for example:
class UserId(val underlying: Long) extends AnyVal
and use that type in your definition of the Group
type:
class Group(id: UserId, name: String, avatarLink: Option[String], groupOwnerId: UserId)
Of course then you must change your reader's signature to be:
implicit def groupReads(implicit groupOwnerId: UserId): Reads[models.GroupDAL.Group]