Skip to main content

JSON Configuration

The JSON codec module provides four configuration types for controlling encoding and decoding behavior: WriterConfig, ReaderConfig, MergeStrategy, and NameMapper. These types allow fine-grained control over how JSON is serialized and deserialized.

WriterConfig​

WriterConfig controls the formatting and content of encoded JSON output. Use it when calling Json#print on Json values or when encoding values with specific formatting requirements.

Configuration Options​

OptionTypeDefaultPurpose
indentionStepInt0Spaces per indentation level (0 = compact JSON)
escapeUnicodeBooleanfalseEscape non-ASCII characters as \uXXXX for ASCII-only output
preferredBufSizeInt32768Internal buffer size in bytes for streaming

Usage Examples​

Pretty-print JSON output with configurable indentation:

import zio.blocks.schema._
import zio.blocks.schema.json.{WriterConfig}

case class Config(server: String, port: Int, debug: Boolean)
object Config { implicit val schema: Schema[Config] = Schema.derived }

val config = Config("localhost", 8080, true)
val json = config.toJson

// Pretty-printed with 2-space indentation
val pretty = json.print(WriterConfig.withIndentionStep(2))
// {
// "server": "localhost",
// "port": 8080,
// "debug": true
// }

Configure custom indentation for JSON output:

import zio.blocks.schema._
import zio.blocks.schema.json.WriterConfig

case class User(name: String, email: String, age: Int)
object User { implicit val schema: Schema[User] = Schema.derived }

val user = User("Alice", "alice@example.com", 30)
val json = user.toJson

// 4-space indentation
val sorted = json.print(WriterConfig
.withIndentionStep(4)
)
// {
// "name": "Alice",
// "email": "alice@example.com",
// "age": 30
// }

Escape non-ASCII characters for ASCII-only transmission:

import zio.blocks.schema._
import zio.blocks.schema.json.WriterConfig

case class Message(text: String)
object Message { implicit val schema: Schema[Message] = Schema.derived }

val msg = Message("Hello 世界")
val json = msg.toJson

// Escape non-ASCII characters - non-ASCII become \uXXXX sequences
val ascii = json.print(WriterConfig.withEscapeUnicode(true))
// Output: {"text":"Hello \\u4e16\\u754c"}

ReaderConfig​

ReaderConfig controls how JSON input is parsed and validated during decoding. Use it when calling Json.parse() with custom parsing behavior.

Configuration Options​

OptionTypeDefaultPurpose
checkForEndOfInputBooleantrueReject anything but whitespace after the value
preferredCharBufSizeInt4096Starting size of the character buffer for strings
maxCharBufSizeInt4194304Longest string value or key, in chars
preferredBufSizeInt32768Starting size of the byte buffer for InputStream and direct (not array-backed) ByteBuffer input; an array-backed ByteBuffer is read in place
maxBufSizeInt33554432Largest size of that byte buffer, for example while looking for a late discriminator field

Usage Examples​

Stop after the first value instead of rejecting the text after it. Whitespace after the value is always allowed:

import zio.blocks.schema._
import zio.blocks.schema.json.{Json, ReaderConfig}

// Lenient: ignore whatever follows the first value
val lenientConfig = ReaderConfig.withCheckForEndOfInput(false)

val jsonString = """{"name": "Alice"} {"name": "Bob"}"""
val result = Json.parse(jsonString, lenientConfig)
// Right({"name": "Alice"}); with the default ReaderConfig this is a Left

MergeStrategy​

MergeStrategy determines how field conflicts are resolved when merging two JSON objects. It provides strategies for different use cases, from strict validation to lenient concatenation.

Available Strategies:

  • Auto (the default) — Merges objects by key and arrays by index at every depth; the right value wins elsewhere
  • Shallow — Merges only the top-level object or array; nested values are replaced by the right ones
  • Concat — Merges objects by key and appends the right array to the left one
  • Replace — The right value replaces the left one
  • Custom(f, r) — r decides where to descend, and f merges everything else

For details on merge strategies and their usage, see the Merging section of the Json documentation.

NameMapper​

NameMapper customizes how field names are transformed between Scala types and JSON representation. It enables patterns like camelCase ↔ snake_case conversion.

Built-in Mappers​

Use identity mapping to keep field names unchanged:

import zio.blocks.schema._

case class User(firstName: String, lastName: String)
object User {
implicit val schema: Schema[User] = Schema.derived
}

// Default: no transformation
val user = User("Alice", "Smith")
val json = user.toJson
// {"firstName": "Alice", "lastName": "Smith"}

Snake Case: Convert camelCase to snake_case:

import zio.blocks.schema._

@Modifier.fieldNaming("snake_case")
case class User(firstName: String, lastName: String)
object User {
implicit val schema: Schema[User] = Schema.derived
}

val user = User("Alice", "Smith")
val json = user.toJson
// {"first_name": "Alice", "last_name": "Smith"}

Kebab Case: Convert camelCase to kebab-case:

import zio.blocks.schema._

@Modifier.fieldNaming("kebab-case")
case class User(firstName: String, lastName: String)
object User {
implicit val schema: Schema[User] = Schema.derived
}

val user = User("Alice", "Smith")
val json = user.toJson
// {"first-name": "Alice", "last-name": "Smith"}

Custom Mappers: Besides Identity, SnakeCase and KebabCase, NameMapper has CamelCase, PascalCase and Custom(f), which applies any String => String function. An implicit NameMapper in scope has no effect: annotate the type with @Modifier.fieldNaming (or @Modifier.caseNaming for case names), or derive a codec with JsonCodecDeriver.withFieldNameMapper and Schema#derive.

Combining Configuration​

Use multiple configuration options together for fine-grained control:

import zio.blocks.schema._
import zio.blocks.schema.json.WriterConfig

case class Config(server: String, port: Int, timeout: Option[Int])
object Config { implicit val schema: Schema[Config] = Schema.derived }

val config = Config("example.com", 443, None)
val json = config.toJson

// Pretty-printed with 4-space indentation
val output = json.print(WriterConfig
.withIndentionStep(4)
)
// {
// "server": "example.com",
// "port": 443
// }

Performance Implications​

  • WriterConfig: Indentation increases output size but has minimal performance impact
  • ReaderConfig: checkForEndOfInput skips the whitespace after the value to the end of the input
  • MergeStrategy: Strategy choice affects merge performance based on use case
  • NameMapper: Applied once per field during schema derivation; zero runtime cost if identity mapping

Best Practices​

  1. Use WriterConfig for human-readable output only — Compact output is faster for wire transmission
  2. Choose MergeStrategy based on use case — Auto for deep merges of defaults, Concat to accumulate lists, Replace to override
  3. Standardize on one NameMapper across your codebase to avoid confusion
  4. Cache WriterConfig/ReaderConfig instances — They're immutable and can be reused

See Also​