Last active
August 29, 2015 14:05
-
-
Save ClaireNeveu/8985dab8a0966b3311a9 to your computer and use it in GitHub Desktop.
Mixing and Matching Type Class Instances for Nested Objects
This file contains 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
package com.serialization | |
trait CanSerialize[A] { | |
def serialize0(a: A): String | |
} | |
package object compact { | |
import com.model._ | |
val compactBlogSerializer = new CanSerialize[Blog] { | |
def serialize0(b: Blog) = s"{ COMPACTBLOG }" | |
} | |
def compactAuthorSerializer(sb: CanSerialize[Blog]) = new CanSerialize[Author] { | |
def serialize0(a: Author) = s"{ COMPACTAUTHOR, blog: ${serialize(sb)(a.blog)} }" | |
} | |
def compactPostSerializer(sa: CanSerialize[Author]) = new CanSerialize[Post] { | |
def serialize0(p: Post) = s"{ COMPACTPOST, author: ${serialize(sa)(p.author)} }" | |
} | |
} |
This file contains 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
package com.main | |
import com.model._ | |
import com.serialization._ | |
object Main extends App { | |
val blog = Blog(1, "My Blog") | |
val author = Author(1, "John Doe", blog) | |
val post = Post(1, "First Post", author) | |
import com.serialization.compact._ | |
println("The result is: " + serialize( | |
Post.postSerializer(compactAuthorSerializer( | |
compactBlogSerializer)))(post)) | |
} |
This file contains 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
package com.model | |
import com.serialization._ | |
case class Blog(id: Int, name: String) | |
object Blog { | |
implicit val blogSerializer = new CanSerialize[Blog] { | |
def serialize0(b: Blog) = s"{ id: ${b.id}, name: ${b.name} }" | |
} | |
} | |
case class Author(id: Int, name: String, blog: Blog) | |
object Author { | |
def authorSerializer(sb: CanSerialize[Blog]) = new CanSerialize[Author] { | |
def serialize0(a: Author) = s"{ id: ${a.id}, name: ${a.name}, blog: ${serialize(sb)(a.blog)} }" | |
} | |
} | |
case class Post(id: Int, title: String, author: Author) | |
object Post { | |
def postSerializer(sa: CanSerialize[Author]) = new CanSerialize[Post] { | |
def serialize0(p: Post) = s"{ id: ${p.id}, title: ${p.title}, author: ${serialize(sa)(p.author)} }" | |
} | |
} |
This file contains 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
package com | |
package object serialization { | |
def serialize[A](instance: CanSerialize[A])(a: A): String = instance.serialize0(a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment