Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from zeryx/argonaut_read.scala
Last active January 5, 2017 21:12
Show Gist options
  • Save Jacoby6000/1536e794794b31227f296c124c3eb704 to your computer and use it in GitHub Desktop.
Save Jacoby6000/1536e794794b31227f296c124c3eb704 to your computer and use it in GitHub Desktop.
//reads input string, checks if its json, and if it is returns an encoded object of the first type that parses correctly.
def read[A](input: String)(implicit decoder: DecodeJson[A]): Option[A] = { // require that the decodeJson for type A be in scope
JsonParser.parse(input).toOption match {
case None => {
throw new AlgorithmException(s"input failed to parse.")
}
case Some(json: Json) => {
val decoded = decoder.decodeJson(json).toOption
decoded
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment