0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Encoding to JSON

Serialize your data out.

Encoders Produce JSON

An Encoder[A] is the dual of a decoder: it turns a Scala value of type A into a Circe Json tree.

From there you serialize to a String for HTTP responses, logs, or storage.

The asJson Syntax

Import io.circe.syntax._ to get the .asJson extension method on any value that has an Encoder in scope.

Primitives and collections work immediately.

import io.circe.syntax._

println(42.asJson.noSpaces)        // 42
println("hi".asJson.noSpaces)      // "hi"
println(List(1,2,3).asJson.noSpaces)  // [1,2,3]

All lessons in this course

  1. Parsing JSON
  2. Decoding into Case Classes
  3. Encoding to JSON
  4. Custom Codecs
← Back to Scala for Backend Engineering & Functional Programming