A lightweight JSON and MessagePack serialization library for Scala
npx @tessl/cli install tessl/maven-com-lihaoyi--upickle_3@4.1.0uPickle is a lightweight JSON and MessagePack serialization library for Scala that provides simple, human-readable serialization with extensive customization options. It supports Scala 2.12, 2.13, and 3.x with cross-compilation for JVM, JavaScript (ScalaJS), and Native platforms, offering automatic derivation for case classes and sealed traits while maintaining superior performance.
"com.lihaoyi" %% "upickle" % "4.1.0"libraryDependencies += "com.lihaoyi" %% "upickle" % "4.1.0"import upickle.default._For custom behavior:
import upickle.{Api, AttributeTagged, legacy}For core types:
import upickle.core.{Reader, Writer, ReadWriter}For direct ujson operations:
import ujson._For direct upack operations:
import upack._For Scala.js web APIs:
import upickle.web._ // Scala.js onlyimport upickle.default._
// Case class serialization
case class Person(name: String, age: Int)
val person = Person("Alice", 30)
// JSON serialization
val json = write(person)
// Result: {"name":"Alice","age":30}
// JSON deserialization
val parsed = read[Person](json)
// Result: Person("Alice", 30)
// MessagePack binary serialization
val binary = writeBinary(person)
val parsedBinary = readBinary[Person](binary)
// Collection handling
val people = List(Person("Alice", 30), Person("Bob", 25))
val jsonArray = write(people)
val parsedArray = read[List[Person]](jsonArray)uPickle is built around several key components:
Reader[T], Writer[T], and ReadWriter[T] for serialization behavior definitionupickle.default provides automatic derivation for most typesupickle.legacy and trait extensions for specialized behaviorujson and upack modules provide direct JSON/MessagePack processing utilitiesPrimary read/write functions for JSON and binary formats, with streaming and configuration options.
def read[T: Reader](s: ujson.Readable, trace: Boolean = false): T
def write[T: Writer](t: T, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): String
def readBinary[T: Reader](s: upack.Readable, trace: Boolean = false): T
def writeBinary[T: Writer](t: T, sortKeys: Boolean = false): Array[Byte]Reader, Writer, and ReadWriter type classes that define serialization behavior for types.
trait Reader[T] extends Visitor[Any, T]
trait Writer[T] extends Transformer[T]
trait ReadWriter[T] extends Reader[T] with Writer[T]
def reader[T: Reader]: Reader[T]
def writer[T: Writer]: Writer[T]
def readwriter[T: ReadWriter]: ReadWriter[T]Comprehensive built-in readers and writers for primitive and collection types.
implicit val StringReader: Reader[String]
implicit val IntReader: Reader[Int]
implicit val BooleanReader: Reader[Boolean]
implicit def OptionReader[T: Reader]: Reader[Option[T]]
implicit def ArrayReader[T: Reader: ClassTag]: Reader[Array[T]]
implicit def MapReader[K: Reader, V: Reader]: Reader[Map[K, V]]Direct integration with ujson for working with JSON AST and value types.
def writeJs[T: Writer](t: T): ujson.Value
implicit def JsValueR: Reader[ujson.Value]
implicit def JsValueW: Writer[ujson.Value]Direct integration with upack for binary MessagePack serialization.
def writeMsg[T: Writer](t: T): upack.Msg
implicit val MsgValueR: Reader[upack.Msg]
implicit val MsgValueW: Writer[upack.Msg]Automatic tagging and discrimination for sealed trait hierarchies with customizable behavior.
trait TaggedReader[T] extends SimpleReader[T]
trait TaggedWriter[T] extends Writer[T]
trait TaggedReadWriter[T] extends ReadWriter[T] with TaggedReader[T] with TaggedWriter[T]Functions for writing to streams, OutputStreams, and creating streamable representations.
def writeTo[T: Writer](t: T, out: java.io.Writer, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): Unit
def stream[T: Writer](t: T, indent: Int = -1, escapeUnicode: Boolean = false, sortKeys: Boolean = false): geny.Writable
def writeBinaryTo[T: Writer](t: T, out: java.io.OutputStream, sortKeys: Boolean = false): Unittrait Api extends upickle.core.Types with implicits.Readers with implicits.Writers with implicits.CaseClassReadWriters
object default extends AttributeTagged
object legacy extends LegacyApi
case class transform[T: Writer](t: T) extends upack.Readable with ujson.Readable