Update codecs handout with TA feedback
This commit is contained in:
parent
b22f0c4bfb
commit
715509a1b2
@ -12,3 +12,5 @@ libraryDependencies ++= Seq(
|
|||||||
)
|
)
|
||||||
|
|
||||||
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
||||||
|
|
||||||
|
initialCommands in console := """import codecs.{_, given}"""
|
||||||
|
|||||||
@ -256,10 +256,10 @@ object Main {
|
|||||||
val maybeJsonObj = parseJson(""" { "name": "Alice", "age": 42 } """)
|
val maybeJsonObj = parseJson(""" { "name": "Alice", "age": 42 } """)
|
||||||
val maybeJsonObj2 = parseJson(""" { "name": "Alice", "age": "42" } """)
|
val maybeJsonObj2 = parseJson(""" { "name": "Alice", "age": "42" } """)
|
||||||
// Uncomment the following lines as you progress in the assignment
|
// Uncomment the following lines as you progress in the assignment
|
||||||
// println(maybeJsonString.toOption.flatMap(_.decodeAs[Int]))
|
// println(maybeJsonString.flatMap(_.decodeAs[Int]))
|
||||||
// println(maybeJsonString.toOption.flatMap(_.decodeAs[String]))
|
// println(maybeJsonString.flatMap(_.decodeAs[String]))
|
||||||
// println(maybeJsonObj.toOption.flatMap(_.decodeAs[Person]))
|
// println(maybeJsonObj.flatMap(_.decodeAs[Person]))
|
||||||
// println(maybeJsonObj2.toOption.flatMap(_.decodeAs[Person]))
|
// println(maybeJsonObj2.flatMap(_.decodeAs[Person]))
|
||||||
// println(renderJson(Person("Bob", 66)))
|
// println(renderJson(Person("Bob", 66)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,20 +8,21 @@ import scala.util.Try
|
|||||||
// render values to `String` JSON blobs
|
// render values to `String` JSON blobs
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a JSON document contained in a `String` value into a `Json` value
|
* Parse a JSON document contained in a `String` value into a `Json` value, returns
|
||||||
|
* `None` in case the supplied `s` value is not a valid JSON document.
|
||||||
*/
|
*/
|
||||||
def parseJson(s: String): Try[Json] = Parser.parseFromString[Json](s)
|
def parseJson(s: String): Option[Json] = Parser.parseFromString[Json](s).toOption
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the JSON value from the supplied `s` parameter, and then try to decode
|
* Parse the JSON value from the supplied `s` parameter, and then try to decode
|
||||||
* it as a value of type `A` using the given `decoder`.
|
* it as a value of type `A` using the given `decoder`.
|
||||||
*
|
*
|
||||||
* Returns a failure if JSON parsing failed, or if decoding failed.
|
* Returns `None` if JSON parsing failed, or if decoding failed.
|
||||||
*/
|
*/
|
||||||
def parseAndDecode[A](s: String)(given decoder: Decoder[A]): Try[A] =
|
def parseAndDecode[A](s: String)(given decoder: Decoder[A]): Option[A] =
|
||||||
for {
|
for {
|
||||||
json <- parseJson(s)
|
json <- parseJson(s)
|
||||||
a <- decoder.decode(json).toRight(new Exception("Decoding failed")).toTry
|
a <- decoder.decode(json)
|
||||||
} yield a
|
} yield a
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user