cornerman / chameleon

Typeclasses for serialization in scala

Version Matrix



Build Status

Typeclasses for serialization

Currently supports:

Get latest release:

libraryDependencies += "com.github.cornerman" %%% "chameleon" % "0.3.0"

Or get development snapshots via jitpack:

resolvers += "jitpack" at ""
libraryDependencies += "com.github.cornerman.chameleon" %%% "chameleon" % "master-SNAPSHOT"


Using for example boopickle:

import chameleon._

// boopickle-specific imports
import chameleon.ext.boopickle._
import java.nio.ByteBuffer
import boopickle.Default._

val serializer = Serializer[String, ByteBuffer]
val deserializer = Deserializer[String, ByteBuffer]

val input = "chameleon"
val serialized = serializer.serialize(input)
val deserialized = deserializer.deserialize(serialized)

Have typeclasses for cats (Contravariant, Functor, Invariant):

import chameleon.ext.cats._


Say, you want to write a library that needs serialization but abstracts over the type of serialization. Then you might end up with something like this:

trait Library[PickleType] {
    def readAndDo() = {
        val pickled: PickleType = ???

    def writeAndDo() = {
        val thing: Thing = ???

But how can you deserialize the pickled value and how do you serialize a thing? You then need to let the user provide an implementation for their serialization of PickleType.

With chameleon, you can use existing typeclasses Serializer and Deserializer which are generic on the pickled type:

import chameleon._

trait Library[PickleType] {
    def readAndDo(implicit d: Deserializer[Thing, PickleType]) = {
        val pickled: PickleType = ???
        d.deserialize(pickled) match {
            case Right(thing: Thing) => ???
            case Left(err: Throwable) => ???

    def writeAndDo(implicit s: Serializer[Thing, PickleType]) = {
        val thing: Thing = ???
        val pickled: PickleType = s.serialize(thing)

Users of this library can now decide what kind of serialization they want to use and rely on existing implementation for some serializers. If you want to use this library with, e.g., JSON using circe, you can do:

import io.circe._, io.circe.syntax._,
import chameleon.ext.circe._

val lib: Library[String] = ???

Support additional Serializers

If your favorite serialization library is not supported yet, you can easily add it (see existing implementations). You need to define implicit Serializer and Deserializer instances for that library. Then, please add a PR for it.