This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| implicit def optionWriter[A] | |
| (implicit writer: JsonWriter[A]): JsonWriter[Option[A]] = | |
| new JsonWriter[Option[A]] { | |
| def write(option: Option[A]): Json = | |
| option match { | |
| case Some(aValue) => writer.write(aValue) | |
| case None => JsNull | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| implicit val writer1: JsonWriter[String] = JsonWriterInstances.stringWriter | |
| implicit val writer2: JsonWriter[String] = JsonWriterInstances.stringWriter | |
| Json.toJson("A string") | |
| <console>:27: error: ambiguous implicit values: | |
| both value stringWriter in object JsonWriterInstances of type => JsonWriter[String] | |
| and value writer1 of type => JsonWriter[String] | |
| match expected type JsonWriter[String] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import JsonWriterInstances._ | |
| import JsonSyntax._ | |
| Person("Dave", "dave@example.com").toJson | |
| // res6: Json = JsObject(Map(name -> JsString(Dave), email -> JsString(dave@example.com))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object JsonSyntax { | |
| implicit class JsonWriterOps[A](value: A) { | |
| def toJson(implicit w: JsonWriter[A]): Json = | |
| w.write(value) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import JsonWriterInstances._ | |
| Json.toJson(Person("Dave", "dave@example.com")) | |
| //res1: Json = JsObject(Map(name -> JsString(Dave), email -> JsString(dave@example.com))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Json { | |
| def toJson[A](value: A)(implicit w: JsonWriter[A]): Json = w.write(value) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final case class Person(name: String, email: String) | |
| object JsonWriterInstances { | |
| implicit val stringWriter: JsonWriter[String] = | |
| new JsonWriter[String] { | |
| def write(value: String): Json = JsString(value) | |
| } | |
| implicit val personWriter: JsonWriter[Person] = | |
| new JsonWriter[Person] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait Json | |
| final case class JsObject(get: Map[String, Json]) extends Json | |
| final case class JsString(get: String) extends Json | |
| final case class JsNumber(get: Double) extends Json | |
| case object JsNull extends Json |