@zio.dev/zio-blocks 0.0.1 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.md +163 -15
- package/package.json +5 -2
- package/path-interpolator.md +24 -23
- package/reference/binding.md +8 -14
- package/reference/chunk.md +36 -36
- package/reference/codec.md +384 -0
- package/reference/docs.md +2 -2
- package/reference/dynamic-optic.md +392 -0
- package/reference/dynamic-value.md +34 -34
- package/reference/formats.md +84 -28
- package/reference/json-schema.md +33 -30
- package/reference/json.md +47 -47
- package/reference/lazy.md +361 -0
- package/reference/modifier.md +151 -87
- package/reference/optics.md +51 -37
- package/reference/reflect.md +3 -3
- package/reference/registers.md +7 -7
- package/reference/schema.md +18 -18
- package/reference/syntax.md +27 -27
- package/reference/type-class-derivation.md +1959 -0
- package/scope.md +521 -203
- package/sidebars.js +6 -1
- package/reference/reflect-transform.md +0 -387
- package/reference/type-class-derivation-internals.md +0 -632
|
@@ -33,7 +33,7 @@ Key design decisions:
|
|
|
33
33
|
|
|
34
34
|
Wraps scalar values in a `PrimitiveValue`:
|
|
35
35
|
|
|
36
|
-
```scala
|
|
36
|
+
```scala
|
|
37
37
|
import zio.blocks.schema.DynamicValue
|
|
38
38
|
|
|
39
39
|
// Using convenience constructors
|
|
@@ -53,7 +53,7 @@ val instant = DynamicValue.Primitive(
|
|
|
53
53
|
|
|
54
54
|
A collection of named fields, analogous to case classes or JSON objects:
|
|
55
55
|
|
|
56
|
-
```scala
|
|
56
|
+
```scala
|
|
57
57
|
import zio.blocks.schema.DynamicValue
|
|
58
58
|
import zio.blocks.chunk.Chunk
|
|
59
59
|
|
|
@@ -80,7 +80,7 @@ Field order is preserved and significant for equality. Use `sortFields` to norma
|
|
|
80
80
|
|
|
81
81
|
A tagged union value, analogous to sealed traits:
|
|
82
82
|
|
|
83
|
-
```scala
|
|
83
|
+
```scala
|
|
84
84
|
import zio.blocks.schema.DynamicValue
|
|
85
85
|
|
|
86
86
|
// A Some variant containing a value
|
|
@@ -101,7 +101,7 @@ some.caseValue // Some(DynamicValue.Primitive(...))
|
|
|
101
101
|
|
|
102
102
|
An ordered collection of values:
|
|
103
103
|
|
|
104
|
-
```scala
|
|
104
|
+
```scala
|
|
105
105
|
import zio.blocks.schema.DynamicValue
|
|
106
106
|
import zio.blocks.chunk.Chunk
|
|
107
107
|
|
|
@@ -126,7 +126,7 @@ val empty = DynamicValue.Sequence.empty
|
|
|
126
126
|
|
|
127
127
|
Key-value pairs where both keys and values are `DynamicValue`:
|
|
128
128
|
|
|
129
|
-
```scala
|
|
129
|
+
```scala
|
|
130
130
|
import zio.blocks.schema.DynamicValue
|
|
131
131
|
import zio.blocks.chunk.Chunk
|
|
132
132
|
|
|
@@ -152,7 +152,7 @@ Unlike `Record` which uses String keys, `Map` supports arbitrary `DynamicValue`
|
|
|
152
152
|
|
|
153
153
|
Represents the absence of a value:
|
|
154
154
|
|
|
155
|
-
```scala
|
|
155
|
+
```scala
|
|
156
156
|
import zio.blocks.schema.DynamicValue
|
|
157
157
|
|
|
158
158
|
val absent = DynamicValue.Null
|
|
@@ -199,7 +199,7 @@ val absent = DynamicValue.Null
|
|
|
199
199
|
|
|
200
200
|
Use `Schema.toDynamicValue` to convert typed Scala values to `DynamicValue`:
|
|
201
201
|
|
|
202
|
-
```scala
|
|
202
|
+
```scala
|
|
203
203
|
import zio.blocks.schema.{Schema, DynamicValue}
|
|
204
204
|
|
|
205
205
|
case class Person(name: String, age: Int)
|
|
@@ -220,7 +220,7 @@ val listDynamic = Schema[List[Int]].toDynamicValue(List(1, 2, 3))
|
|
|
220
220
|
|
|
221
221
|
Use `Schema.fromDynamicValue` to convert `DynamicValue` back to typed Scala values:
|
|
222
222
|
|
|
223
|
-
```scala
|
|
223
|
+
```scala
|
|
224
224
|
import zio.blocks.schema.{Schema, DynamicValue, SchemaError}
|
|
225
225
|
|
|
226
226
|
case class Person(name: String, age: Int)
|
|
@@ -248,7 +248,7 @@ val error = Schema[Person].fromDynamicValue(badDynamic)
|
|
|
248
248
|
|
|
249
249
|
Each `DynamicValue` has a corresponding `DynamicValueType` for runtime type checking:
|
|
250
250
|
|
|
251
|
-
```scala
|
|
251
|
+
```scala
|
|
252
252
|
import zio.blocks.schema.{DynamicValue, DynamicValueType}
|
|
253
253
|
|
|
254
254
|
val dv = DynamicValue.Record("x" -> DynamicValue.int(1))
|
|
@@ -269,7 +269,7 @@ val fields: Option[Chunk[(String, DynamicValue)]] =
|
|
|
269
269
|
|
|
270
270
|
### Extracting Primitive Values
|
|
271
271
|
|
|
272
|
-
```scala
|
|
272
|
+
```scala
|
|
273
273
|
import zio.blocks.schema.{DynamicValue, PrimitiveType, Validation}
|
|
274
274
|
|
|
275
275
|
val dv = DynamicValue.int(42)
|
|
@@ -288,7 +288,7 @@ val stringValue: Option[String] = dv.asPrimitive(PrimitiveType.String(Validation
|
|
|
288
288
|
|
|
289
289
|
Navigate using `get` methods that return `DynamicValueSelection`:
|
|
290
290
|
|
|
291
|
-
```scala
|
|
291
|
+
```scala
|
|
292
292
|
import zio.blocks.schema.DynamicValue
|
|
293
293
|
|
|
294
294
|
val data = DynamicValue.Record(
|
|
@@ -315,7 +315,7 @@ val name = firstName.one // Either[SchemaError, DynamicValue]
|
|
|
315
315
|
|
|
316
316
|
Use `DynamicOptic` for complex path expressions:
|
|
317
317
|
|
|
318
|
-
```scala
|
|
318
|
+
```scala
|
|
319
319
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
320
320
|
|
|
321
321
|
val data = DynamicValue.Record(
|
|
@@ -337,7 +337,7 @@ val result = data.get(path).one // Right(DynamicValue.Primitive(String("Alice")
|
|
|
337
337
|
|
|
338
338
|
`DynamicValueSelection` wraps navigation results and provides fluent chaining:
|
|
339
339
|
|
|
340
|
-
```scala
|
|
340
|
+
```scala
|
|
341
341
|
import zio.blocks.schema.{DynamicValue, DynamicValueSelection}
|
|
342
342
|
|
|
343
343
|
val selection: DynamicValueSelection = ???
|
|
@@ -366,7 +366,7 @@ selection.flatMap(dv => ???) // Chain selections
|
|
|
366
366
|
|
|
367
367
|
Update values at a path:
|
|
368
368
|
|
|
369
|
-
```scala
|
|
369
|
+
```scala
|
|
370
370
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
371
371
|
|
|
372
372
|
val data = DynamicValue.Record(
|
|
@@ -386,7 +386,7 @@ val updated = data.modify(path)(dv => DynamicValue.string("Bob"))
|
|
|
386
386
|
|
|
387
387
|
Replace a value at a path:
|
|
388
388
|
|
|
389
|
-
```scala
|
|
389
|
+
```scala
|
|
390
390
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
391
391
|
|
|
392
392
|
val data = DynamicValue.Record("x" -> DynamicValue.int(1))
|
|
@@ -400,7 +400,7 @@ val updated = data.set(path, DynamicValue.int(99))
|
|
|
400
400
|
|
|
401
401
|
Remove a value at a path:
|
|
402
402
|
|
|
403
|
-
```scala
|
|
403
|
+
```scala
|
|
404
404
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
405
405
|
|
|
406
406
|
val data = DynamicValue.Record(
|
|
@@ -416,7 +416,7 @@ val updated = data.delete(DynamicOptic.root.field("a"))
|
|
|
416
416
|
|
|
417
417
|
Add a value at a path (fails if path exists):
|
|
418
418
|
|
|
419
|
-
```scala
|
|
419
|
+
```scala
|
|
420
420
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
421
421
|
|
|
422
422
|
val data = DynamicValue.Record("a" -> DynamicValue.int(1))
|
|
@@ -432,7 +432,7 @@ val updated = data.insert(
|
|
|
432
432
|
|
|
433
433
|
Use `*OrFail` variants for operations that should fail explicitly:
|
|
434
434
|
|
|
435
|
-
```scala
|
|
435
|
+
```scala
|
|
436
436
|
import zio.blocks.schema.{DynamicValue, DynamicOptic, SchemaError}
|
|
437
437
|
|
|
438
438
|
val data = DynamicValue.Record("x" -> DynamicValue.int(1))
|
|
@@ -452,7 +452,7 @@ val result: Either[SchemaError, DynamicValue] =
|
|
|
452
452
|
- Adds `@ {tag: "..."}` annotations for Variants
|
|
453
453
|
- Adds `@ {type: "..."}` annotations for typed primitives (Instant, Duration, etc.)
|
|
454
454
|
|
|
455
|
-
```scala
|
|
455
|
+
```scala
|
|
456
456
|
import zio.blocks.schema.{DynamicValue, PrimitiveValue}
|
|
457
457
|
|
|
458
458
|
val person = DynamicValue.Record(
|
|
@@ -488,7 +488,7 @@ Use `toEjson(indent)` to control indentation level.
|
|
|
488
488
|
|
|
489
489
|
Merge two `DynamicValue` structures using configurable strategies:
|
|
490
490
|
|
|
491
|
-
```scala
|
|
491
|
+
```scala
|
|
492
492
|
import zio.blocks.schema.{DynamicValue, DynamicValueMergeStrategy}
|
|
493
493
|
|
|
494
494
|
val left = DynamicValue.Record(
|
|
@@ -517,7 +517,7 @@ val merged = left.merge(right, DynamicValueMergeStrategy.Auto)
|
|
|
517
517
|
| `Concat` | Concatenate Sequences instead of merging by index |
|
|
518
518
|
| `Custom(f, r)` | Custom function with custom recursion control |
|
|
519
519
|
|
|
520
|
-
```scala
|
|
520
|
+
```scala
|
|
521
521
|
import zio.blocks.schema.{DynamicValue, DynamicValueMergeStrategy}
|
|
522
522
|
|
|
523
523
|
val list1 = DynamicValue.Sequence(DynamicValue.int(1), DynamicValue.int(2))
|
|
@@ -532,7 +532,7 @@ val concatted = list1.merge(list2, DynamicValueMergeStrategy.Concat)
|
|
|
532
532
|
|
|
533
533
|
Transform `DynamicValue` structures for comparison or serialization:
|
|
534
534
|
|
|
535
|
-
```scala
|
|
535
|
+
```scala
|
|
536
536
|
import zio.blocks.schema.DynamicValue
|
|
537
537
|
|
|
538
538
|
val data = DynamicValue.Record(
|
|
@@ -566,7 +566,7 @@ data.normalize
|
|
|
566
566
|
|
|
567
567
|
Apply functions to all values in a structure:
|
|
568
568
|
|
|
569
|
-
```scala
|
|
569
|
+
```scala
|
|
570
570
|
import zio.blocks.schema.{DynamicValue, DynamicOptic, PrimitiveValue}
|
|
571
571
|
|
|
572
572
|
val data = DynamicValue.Record(
|
|
@@ -593,7 +593,7 @@ val topDown = data.transformDown { (path, dv) => ??? }
|
|
|
593
593
|
|
|
594
594
|
Rename all record fields:
|
|
595
595
|
|
|
596
|
-
```scala
|
|
596
|
+
```scala
|
|
597
597
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
598
598
|
|
|
599
599
|
val data = DynamicValue.Record(
|
|
@@ -615,7 +615,7 @@ val camelCase = data.transformFields { (path, name) =>
|
|
|
615
615
|
|
|
616
616
|
Aggregate values from a `DynamicValue` tree:
|
|
617
617
|
|
|
618
|
-
```scala
|
|
618
|
+
```scala
|
|
619
619
|
import zio.blocks.schema.{DynamicValue, DynamicOptic, PrimitiveValue}
|
|
620
620
|
|
|
621
621
|
val data = DynamicValue.Record(
|
|
@@ -638,7 +638,7 @@ val sum = data.foldUp(0) { (path, dv, acc) =>
|
|
|
638
638
|
|
|
639
639
|
### To JSON
|
|
640
640
|
|
|
641
|
-
```scala
|
|
641
|
+
```scala
|
|
642
642
|
import zio.blocks.schema.DynamicValue
|
|
643
643
|
import zio.blocks.schema.json.Json
|
|
644
644
|
|
|
@@ -653,7 +653,7 @@ val json: Json = dynamic.toJson
|
|
|
653
653
|
|
|
654
654
|
### From JSON
|
|
655
655
|
|
|
656
|
-
```scala
|
|
656
|
+
```scala
|
|
657
657
|
import zio.blocks.schema.DynamicValue
|
|
658
658
|
import zio.blocks.schema.json.Json
|
|
659
659
|
|
|
@@ -667,7 +667,7 @@ val dynamic: DynamicValue = json.toDynamicValue
|
|
|
667
667
|
|
|
668
668
|
Search recursively for values matching a predicate:
|
|
669
669
|
|
|
670
|
-
```scala
|
|
670
|
+
```scala
|
|
671
671
|
import zio.blocks.schema.{DynamicValue, DynamicValueType, PrimitiveValue}
|
|
672
672
|
|
|
673
673
|
val data = DynamicValue.Record(
|
|
@@ -691,7 +691,7 @@ val atDepth2 = data.select.queryPath(path => path.nodes.length == 2)
|
|
|
691
691
|
|
|
692
692
|
Work with data when the schema isn't known at compile time:
|
|
693
693
|
|
|
694
|
-
```scala
|
|
694
|
+
```scala
|
|
695
695
|
import zio.blocks.schema.{DynamicValue, PrimitiveValue}
|
|
696
696
|
|
|
697
697
|
def processAnyData(data: DynamicValue): DynamicValue = {
|
|
@@ -712,7 +712,7 @@ def processAnyData(data: DynamicValue): DynamicValue = {
|
|
|
712
712
|
|
|
713
713
|
Transform data between schema versions:
|
|
714
714
|
|
|
715
|
-
```scala
|
|
715
|
+
```scala
|
|
716
716
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
717
717
|
|
|
718
718
|
def migrateV1toV2(data: DynamicValue): DynamicValue = {
|
|
@@ -735,7 +735,7 @@ def migrateV1toV2(data: DynamicValue): DynamicValue = {
|
|
|
735
735
|
|
|
736
736
|
Build queries at runtime:
|
|
737
737
|
|
|
738
|
-
```scala
|
|
738
|
+
```scala
|
|
739
739
|
import zio.blocks.schema.{DynamicValue, DynamicOptic}
|
|
740
740
|
|
|
741
741
|
def buildPath(fields: List[String]): DynamicOptic =
|
|
@@ -761,7 +761,7 @@ val email = getValue(data, List("user", "profile", "email"))
|
|
|
761
761
|
|
|
762
762
|
Use `DynamicValue` as an intermediate format:
|
|
763
763
|
|
|
764
|
-
```scala
|
|
764
|
+
```scala
|
|
765
765
|
import zio.blocks.schema.{Schema, DynamicValue}
|
|
766
766
|
import zio.blocks.schema.json.Json
|
|
767
767
|
|
|
@@ -784,7 +784,7 @@ val json2 = dynamic2.toJson
|
|
|
784
784
|
|
|
785
785
|
`DynamicValue` has a total ordering for sorting and comparison:
|
|
786
786
|
|
|
787
|
-
```scala
|
|
787
|
+
```scala
|
|
788
788
|
import zio.blocks.schema.DynamicValue
|
|
789
789
|
|
|
790
790
|
val a = DynamicValue.int(1)
|
|
@@ -804,7 +804,7 @@ primitive < record // true
|
|
|
804
804
|
|
|
805
805
|
Compute differences between `DynamicValue` instances:
|
|
806
806
|
|
|
807
|
-
```scala
|
|
807
|
+
```scala
|
|
808
808
|
import zio.blocks.schema.DynamicValue
|
|
809
809
|
import zio.blocks.schema.patch.DynamicPatch
|
|
810
810
|
|
package/reference/formats.md
CHANGED
|
@@ -1,15 +1,82 @@
|
|
|
1
1
|
---
|
|
2
2
|
id: formats
|
|
3
3
|
title: "Serialization Formats"
|
|
4
|
+
sidebar_label: "Formats"
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
ZIO Blocks Schema provides automatic codec derivation for multiple serialization formats. Once you have a `Schema[A]` for your data type, you can derive codecs for any supported format using the unified `Schema.derive(Format)` pattern.
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
A `Format` is an abstraction that bundles together everything needed to serialize and deserialize data in a specific format (JSON, Avro, Protobuf, etc.).
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
Each format defines the types of input for decoding and output for encoding, as well as the typeclass used as a codec for that format. Each format contains a `Deriver` corresponding to its specific MIME type, which is used to derive codecs from schemas:
|
|
14
|
+
|
|
15
|
+
```scala
|
|
16
|
+
trait Format {
|
|
17
|
+
type DecodeInput
|
|
18
|
+
type EncodeOutput
|
|
19
|
+
type TypeClass[A] <: Codec[DecodeInput, EncodeOutput, A]
|
|
20
|
+
def mimeType: String
|
|
21
|
+
def deriver: Deriver[TypeClass]
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
It unifies all metadata related to serialization formats, such as MIME type and codec deriver, in a single place. This allows for a consistent API across different formats when deriving codecs from schemas. Having MIME type information helps with runtime content negotiation and format routing, for example in HTTP servers or message queues.
|
|
26
|
+
|
|
27
|
+
That is, you can easily call [`Schema[A].derive(format)`](./type-class-derivation.md#using-the-deriver-to-derive-type-class-instances) for any format that implements the `Format` trait, and receive a codec that can encode and decode values of type `A` according to the rules of that format.
|
|
28
|
+
|
|
29
|
+
Formats are categorized into `BinaryFormat` and `TextFormat`, which specify the types of input and output for encoding and decoding:
|
|
30
|
+
|
|
31
|
+
```scala
|
|
32
|
+
sealed trait Format
|
|
33
|
+
abstract class BinaryFormat[...](...) extends Format { ... }
|
|
34
|
+
abstract class TextFormat[...](...) extends Format { ... }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For example, the `JsonFormat` is a `BinaryFormat` that represents a JSON binary format, where the input for decoding is `ByteBuffer` and the output for encoding is also `ByteBuffer`, the MIME type is `application/json`, and the deriver for generating codecs from schemas is `JsonBinaryCodecDeriver`:
|
|
38
|
+
|
|
39
|
+
```scala
|
|
40
|
+
object JsonFormat extends BinaryFormat("application/json", JsonBinaryCodecDeriver)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Built-in Formats
|
|
44
|
+
|
|
45
|
+
Here's a summary of the formats currently supported by ZIO Blocks. Each format provides a `BinaryFormat` object that can be passed to `derive`:
|
|
46
|
+
|
|
47
|
+
| Format Object | Codec Type | MIME Type | Module |
|
|
48
|
+
|---------------------|-----------------------------|-----------------------|---------------------------------|
|
|
49
|
+
| `JsonFormat` | `JsonBinaryCodec[A]` | `application/json` | `zio-blocks-schema` |
|
|
50
|
+
| `ToonFormat` | `ToonBinaryCodec[A]` | `text/toon` | `zio-blocks-schema-toon` |
|
|
51
|
+
| `MessagePackFormat` | `MessagePackBinaryCodec[A]` | `application/msgpack` | `zio-blocks-schema-messagepack` |
|
|
52
|
+
| `AvroFormat` | `AvroBinaryCodec[A]` | `application/avro` | `zio-blocks-schema-avro` |
|
|
53
|
+
| `ThriftFormat` | `ThriftBinaryCodec[A]` | `application/thrift` | `zio-blocks-schema-thrift` |
|
|
54
|
+
|
|
55
|
+
## Defining a Custom Format
|
|
56
|
+
|
|
57
|
+
To add a new serialization format, define a `BinaryFormat` (or `TextFormat`) singleton with a custom `Deriver`:
|
|
58
|
+
|
|
59
|
+
```scala
|
|
60
|
+
import zio.blocks.schema.codec.{BinaryCodec, BinaryFormat}
|
|
61
|
+
import zio.blocks.schema.derive.Deriver
|
|
62
|
+
|
|
63
|
+
// 1. Define your codec base class
|
|
64
|
+
abstract class MyCodec[A] extends BinaryCodec[A]
|
|
65
|
+
|
|
66
|
+
// 2. Implement a Deriver[MyCodec] (see Type-class Derivation docs)
|
|
67
|
+
// val myDeriver: Deriver[MyCodec] = ...
|
|
68
|
+
|
|
69
|
+
// 3. Create the format singleton
|
|
70
|
+
// object MyFormat extends BinaryFormat[MyCodec]("application/x-myformat", myDeriver)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
For details on implementing a `Deriver`, see [Type-class Derivation](./type-class-derivation.md).
|
|
74
|
+
|
|
75
|
+
## Codec Derivation System
|
|
9
76
|
|
|
10
77
|
All serialization formats in ZIO Blocks follow the same pattern: given a `Schema[A]`, you derive a codec by calling `derive` with a format object:
|
|
11
78
|
|
|
12
|
-
```scala
|
|
79
|
+
```scala
|
|
13
80
|
import zio.blocks.schema._
|
|
14
81
|
import zio.blocks.schema.toon._
|
|
15
82
|
|
|
@@ -29,17 +96,6 @@ val bytes: Array[Byte] = codec.encode(Person("Alice", 30))
|
|
|
29
96
|
val result: Either[SchemaError, Person] = codec.decode(bytes)
|
|
30
97
|
```
|
|
31
98
|
|
|
32
|
-
Each format provides a `BinaryFormat` object that can be passed to `derive`:
|
|
33
|
-
|
|
34
|
-
| Format | Object | MIME Type | Platform Support |
|
|
35
|
-
|--------|--------|-----------|------------------|
|
|
36
|
-
| JSON | `JsonFormat` | `application/json` | JVM, JS |
|
|
37
|
-
| TOON | `ToonFormat` | `text/toon` | JVM, JS |
|
|
38
|
-
| MessagePack | `MessagePackFormat` | `application/msgpack` | JVM, JS |
|
|
39
|
-
| Avro | `AvroFormat` | `application/avro` | JVM only |
|
|
40
|
-
| Thrift | `ThriftFormat` | `application/thrift` | JVM only |
|
|
41
|
-
| BSON | `BsonSchemaCodec` | Binary | JVM only |
|
|
42
|
-
|
|
43
99
|
## JSON Format
|
|
44
100
|
|
|
45
101
|
JSON format is the most commonly used text-based serialization format. See the dedicated [JSON documentation](json.md) for comprehensive coverage of the `Json` ADT, navigation, and transformation features.
|
|
@@ -54,7 +110,7 @@ libraryDependencies += "dev.zio" %% "zio-blocks-schema" % "<version>"
|
|
|
54
110
|
|
|
55
111
|
### Basic Usage
|
|
56
112
|
|
|
57
|
-
```scala
|
|
113
|
+
```scala
|
|
58
114
|
import zio.blocks.schema._
|
|
59
115
|
import zio.blocks.schema.json._
|
|
60
116
|
|
|
@@ -89,7 +145,7 @@ Requires the Apache Avro library (1.12.x).
|
|
|
89
145
|
|
|
90
146
|
### Basic Usage
|
|
91
147
|
|
|
92
|
-
```scala
|
|
148
|
+
```scala
|
|
93
149
|
import zio.blocks.schema._
|
|
94
150
|
import zio.blocks.schema.avro._
|
|
95
151
|
|
|
@@ -114,7 +170,7 @@ val decoded: Either[SchemaError, Person] = codec.decode(bytes)
|
|
|
114
170
|
|
|
115
171
|
Each `AvroBinaryCodec` exposes an `avroSchema` property containing the Apache Avro schema:
|
|
116
172
|
|
|
117
|
-
```scala
|
|
173
|
+
```scala
|
|
118
174
|
import zio.blocks.schema._
|
|
119
175
|
import zio.blocks.schema.avro._
|
|
120
176
|
import org.apache.avro.{Schema => AvroSchema}
|
|
@@ -162,7 +218,7 @@ println(avroSchema.toString(true))
|
|
|
162
218
|
|
|
163
219
|
Sealed traits are encoded as Avro unions with an integer index prefix:
|
|
164
220
|
|
|
165
|
-
```scala
|
|
221
|
+
```scala
|
|
166
222
|
import zio.blocks.schema._
|
|
167
223
|
import zio.blocks.schema.avro._
|
|
168
224
|
|
|
@@ -202,7 +258,7 @@ libraryDependencies += "dev.zio" %% "zio-blocks-schema-toon" % "<version>"
|
|
|
202
258
|
|
|
203
259
|
### Basic Usage
|
|
204
260
|
|
|
205
|
-
```scala
|
|
261
|
+
```scala
|
|
206
262
|
import zio.blocks.schema._
|
|
207
263
|
import zio.blocks.schema.toon._
|
|
208
264
|
|
|
@@ -260,7 +316,7 @@ orders[2]{id,total}:
|
|
|
260
316
|
|
|
261
317
|
The `ToonBinaryCodecDeriver` provides extensive configuration:
|
|
262
318
|
|
|
263
|
-
```scala
|
|
319
|
+
```scala
|
|
264
320
|
import zio.blocks.schema._
|
|
265
321
|
import zio.blocks.schema.toon._
|
|
266
322
|
|
|
@@ -295,7 +351,7 @@ val codec = Schema[Person].derive(customDeriver)
|
|
|
295
351
|
|
|
296
352
|
### ADT Encoding Styles
|
|
297
353
|
|
|
298
|
-
```scala
|
|
354
|
+
```scala
|
|
299
355
|
import zio.blocks.schema._
|
|
300
356
|
import zio.blocks.schema.toon._
|
|
301
357
|
|
|
@@ -331,7 +387,7 @@ libraryDependencies += "dev.zio" %% "zio-blocks-schema-messagepack" % "<version>
|
|
|
331
387
|
|
|
332
388
|
### Basic Usage
|
|
333
389
|
|
|
334
|
-
```scala
|
|
390
|
+
```scala
|
|
335
391
|
import zio.blocks.schema._
|
|
336
392
|
import zio.blocks.schema.msgpack._
|
|
337
393
|
|
|
@@ -383,7 +439,7 @@ MessagePack provides significant space savings compared to JSON:
|
|
|
383
439
|
|
|
384
440
|
Sealed traits encode a variant index followed by the case value:
|
|
385
441
|
|
|
386
|
-
```scala
|
|
442
|
+
```scala
|
|
387
443
|
import zio.blocks.schema._
|
|
388
444
|
import zio.blocks.schema.msgpack._
|
|
389
445
|
|
|
@@ -416,7 +472,7 @@ Requires the MongoDB BSON library (5.x).
|
|
|
416
472
|
|
|
417
473
|
### Basic Usage
|
|
418
474
|
|
|
419
|
-
```scala
|
|
475
|
+
```scala
|
|
420
476
|
import zio.blocks.schema._
|
|
421
477
|
import zio.blocks.schema.bson._
|
|
422
478
|
|
|
@@ -438,7 +494,7 @@ val codec: BsonCodec[Person] = BsonSchemaCodec.bsonCodec(Schema[Person])
|
|
|
438
494
|
|
|
439
495
|
BSON provides native support for MongoDB ObjectIds:
|
|
440
496
|
|
|
441
|
-
```scala
|
|
497
|
+
```scala
|
|
442
498
|
import zio.blocks.schema._
|
|
443
499
|
import zio.blocks.schema.bson._
|
|
444
500
|
import org.bson.types.ObjectId
|
|
@@ -458,7 +514,7 @@ val codec = BsonSchemaCodec.bsonCodec(Schema[Document])
|
|
|
458
514
|
|
|
459
515
|
### Configuration Options
|
|
460
516
|
|
|
461
|
-
```scala
|
|
517
|
+
```scala
|
|
462
518
|
import zio.blocks.schema._
|
|
463
519
|
import zio.blocks.schema.bson._
|
|
464
520
|
import BsonSchemaCodec._
|
|
@@ -486,7 +542,7 @@ val codec = BsonSchemaCodec.bsonCodec(Schema[Person], config)
|
|
|
486
542
|
|
|
487
543
|
### Sum Type Handling
|
|
488
544
|
|
|
489
|
-
```scala
|
|
545
|
+
```scala
|
|
490
546
|
import zio.blocks.schema.bson.BsonSchemaCodec.SumTypeHandling
|
|
491
547
|
|
|
492
548
|
// Option 1: Wrapper with class name as field key (default)
|
|
@@ -515,7 +571,7 @@ Requires the Apache Thrift library (0.22.x).
|
|
|
515
571
|
|
|
516
572
|
### Basic Usage
|
|
517
573
|
|
|
518
|
-
```scala
|
|
574
|
+
```scala
|
|
519
575
|
import zio.blocks.schema._
|
|
520
576
|
import zio.blocks.schema.thrift._
|
|
521
577
|
import java.nio.ByteBuffer
|
|
@@ -616,7 +672,7 @@ All formats support the full set of ZIO Blocks Schema primitive types:
|
|
|
616
672
|
|
|
617
673
|
All formats return `Either[SchemaError, A]` for decoding operations. Errors include path information for debugging:
|
|
618
674
|
|
|
619
|
-
```scala
|
|
675
|
+
```scala
|
|
620
676
|
import zio.blocks.schema._
|
|
621
677
|
import zio.blocks.schema.toon._
|
|
622
678
|
|