Skip to main content
Version: 2.x

Introduction to ZIO JSON

ZIO JSON is a fast and secure JSON library with tight ZIO integration.

Production Ready CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO JSON

Introductionโ€‹

The goal of this project is to create the best all-round JSON library for Scala:

  • Performance to handle more requests per second than the incumbents, i.e. reduced operational costs.
  • Security to mitigate against adversarial JSON payloads that threaten the capacity of the server.
  • Fast Compilation no shapeless, no type astronautics.
  • Future-Proof, prepared for Scala 3 and next-generation Java.
  • Simple small codebase, concise documentation that covers everything.
  • Helpful errors are readable by humans and machines.
  • ZIO Integration so nothing more is required.

Installationโ€‹

Add the core module to your build.sbt. It is published for Scala 2.12, 2.13 and 3:

libraryDependencies ++= Seq(
// Codecs, derivation, the Json AST and, on the JVM, streaming (JVM, Scala.js, Scala Native)
"dev.zio" %% "zio-json" % "1.1.3"
)

In a Scala.js or Scala Native project, use %%%:

libraryDependencies ++= Seq(
// Codecs, derivation and the Json AST
"dev.zio" %%% "zio-json" % "1.1.3"
)

Getting Started continues from here with a first program.

Modulesโ€‹

The optional modules below have the same version number as zio-json and depend on it, so adding one also brings in the core module. Add only the lines you need.

Formatsโ€‹

Read and write YAML with your JSON codecs. This module is JVM only:

libraryDependencies ++= Seq(
// Read and write YAML with your JSON codecs, through SnakeYAML (JVM)
"dev.zio" %% "zio-json-yaml" % "1.1.3"
)

See YAML.

Derivationโ€‹

Generate codec instances with the @jsonDerive annotation. This module is published for Scala 2.12 and 2.13 only, and needs macro annotations turned on: the -Ymacro-annotations compiler option on Scala 2.13, or the macro paradise plugin on Scala 2.12 (the @jsonDerive page has both settings). Use %%% on Scala.js and Scala Native:

libraryDependencies ++= Seq(
// The @jsonDerive annotation, which writes codec instances for you (JVM, Scala.js, Scala Native; Scala 2 only)
"dev.zio" %% "zio-json-macros" % "1.1.3"
)

See @jsonDerive Macro Annotation.

Integrationsโ€‹

Codecs for types from other libraries:

libraryDependencies ++= Seq(
// http4s 0.23 entity encoders and decoders built from ZIO JSON codecs (JVM)
"dev.zio" %% "zio-json-interop-http4s" % "1.1.3",

// Codecs and map-key codecs for refined types (JVM, Scala.js, Scala Native)
"dev.zio" %% "zio-json-interop-refined" % "1.1.3",

// Codecs for Scalaz 7 IList (JVM, Scala.js, Scala Native; Scala 2 only)
"dev.zio" %% "zio-json-interop-scalaz7x" % "1.1.3",

// Codecs and map-key codecs for enumeratum enums (JVM, Scala.js, Scala Native)
"dev.zio" %% "zio-json-interop-enumeratum" % "1.1.3"
)

The http4s module is JVM only. The others are also built for Scala.js and Scala Native:

libraryDependencies ++= Seq(
// Codecs and map-key codecs for refined types
"dev.zio" %%% "zio-json-interop-refined" % "1.1.3",

// Codecs for Scalaz 7 IList (Scala 2 only)
"dev.zio" %%% "zio-json-interop-scalaz7x" % "1.1.3",

// Codecs and map-key codecs for enumeratum enums
"dev.zio" %%% "zio-json-interop-enumeratum" % "1.1.3"
)

See HTTP4s Interop, Refined Interop, Scalaz 7.x Interop and Enumeratum Interop.

Testingโ€‹

Golden (snapshot) tests that catch unintended changes to encoded JSON. This module is JVM only and belongs in the Test configuration:

libraryDependencies ++= Seq(
// Golden tests for JSON codecs, built on ZIO Test (JVM)
"dev.zio" %% "zio-json-golden" % "1.1.3" % Test
)

See Golden Testing API and Catch Unintended Changes to Your JSON with Golden Tests.

Documentationโ€‹

The documentation is split by what you want to do:

  • Getting Started installs ZIO JSON and walks through encoding and decoding your first types.
  • Concepts explain the design and the trade-offs: how derivation and decoding work, Scala 2 and Scala 3 differences, decoder strictness, JSON shapes, performance and security.
  • Reference pages list every type, method, annotation and option.
  • Integrations connect ZIO JSON to http4s, refined, Scalaz 7 and enumeratum.
  • Tutorials teach a larger example step by step: Model a Real API Payload.
  • How-to Guides solve one task each, such as renaming fields, encoding sealed traits or streaming large JSON documents.

ZIO Chat

๐ŸŒŸ Greetings from ZIO Chat! ๐ŸŒŸ Have questions about ZIO? Look no further! I'm here to be your ZIO guru. Whether you're a seasoned pro or just starting out, I've got answers tailored just for you. Fire away with your ZIO queries, and I'll hit you back with the best insights I've got. Let's dive into the world of ZIO together!