Skip to content

Instantly share code, notes, and snippets.

@zeryx
Created January 5, 2017 21:06
Show Gist options
  • Save zeryx/8cce0df844d02e2368a344c18b9fdefc to your computer and use it in GitHub Desktop.
Save zeryx/8cce0df844d02e2368a344c18b9fdefc 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[SomeType](input: String): Option[SomeType] = {
//maybe I need to loop over every defined codec I've generated? How woud I do that?
val decoder: DecodeJson[SomeType] = DecodeJson.of[SomeType]
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