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
| Option | Type | Default | Purpose |
|---|---|---|---|
indentionStep | Int | 0 | Spaces per indentation level (0 = compact JSON) |
escapeUnicode | Boolean | false | Escape non-ASCII characters as \uXXXX for ASCII-only output |
preferredBufSize | Int | 32768 | Internal 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
| Option | Type | Default | Purpose |
|---|---|---|---|
checkForEndOfInput | Boolean | true | Reject anything but whitespace after the value |
preferredCharBufSize | Int | 4096 | Starting size of the character buffer for strings |
maxCharBufSize | Int | 4194304 | Longest string value or key, in chars |
preferredBufSize | Int | 32768 | Starting size of the byte buffer for InputStream and direct (not array-backed) ByteBuffer input; an array-backed ByteBuffer is read in place |
maxBufSize | Int | 33554432 | Largest 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 elsewhereShallow— Merges only the top-level object or array; nested values are replaced by the right onesConcat— Merges objects by key and appends the right array to the left oneReplace— The right value replaces the left oneCustom(f, r)—rdecides where to descend, andfmerges 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:
checkForEndOfInputskips 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
- Use WriterConfig for human-readable output only — Compact output is faster for wire transmission
- Choose MergeStrategy based on use case —
Autofor deep merges of defaults,Concatto accumulate lists,Replaceto override - Standardize on one NameMapper across your codebase to avoid confusion
- Cache WriterConfig/ReaderConfig instances — They're immutable and can be reused
See Also
- How to Customize JSON Field Names, ADT Encoding and Optional Fields: a task-oriented guide that applies these settings to a real model.
- Coming from circe, zio-json or jsoniter-scala: maps circe
Configuration, zio-json annotations and jsoniter-scalaCodecMakerConfigoptions onto these settings. - How to Tune JSON Encoding and Decoding for Throughput: decode from bytes and streams, reuse codecs and size buffers for throughput.