Skip to content

Instantly share code, notes, and snippets.

@matthughes
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthughes/c905557fbd85f4e91709 to your computer and use it in GitHub Desktop.
Save matthughes/c905557fbd85f4e91709 to your computer and use it in GitHub Desktop.
implicit def AgentCodecJson: CodecJson[Agent] =
CodecJson(
(agent: Agent) => jString(agent match {
case Agent.System => "System"
case Agent.User(name) => name
}),
c => c.focus.string.map {
_ match {
case "System" => Agent.System
case username => Agent.User(username)
}
}
)
// Becomes
implicit def AgentCodecJson: CodecJson[Agent] = {
CodecJson(
implicitly[EncodeJson[String]].contramap {
_ match {
case Agent.System => "System"
case Agent.User(name) => name
}
},
implicitly[DecodeJson[String]].map {
_ match {
case "System" => Agent.System
case username => Agent.User(username)
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment