You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+4-6Lines changed: 4 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,15 +39,13 @@ let addressSchema =
39
39
|> field "city" _.City
40
40
|> build
41
41
42
-
let personSchema =
42
+
let codec =
43
43
define<Person>
44
44
|> construct makePerson
45
45
|> field "id" _.Id
46
46
|> field "name" _.Name
47
47
|> fieldWith "home" _.Home addressSchema
48
-
|> build
49
-
50
-
let codec = Json.compile personSchema
48
+
|> Json.buildAndCompile
51
49
let person =
52
50
{
53
51
Id = 42
@@ -76,13 +74,13 @@ That schema reads almost like the data constructor:
76
74
77
75
The result is not hidden serializer behavior. It is the contract itself, written in normal F#.
78
76
79
-
If you want the compile step to read a bit smaller in samples, the format modules also expose `Json.codec`, `Xml.codec`, `Yaml.codec`, and `KeyValue.codec` as direct aliases for `compile`.
77
+
If the schema only exists inline at the end of the authoring pipeline, `Json.buildAndCompile`, `Xml.buildAndCompile`, `Yaml.buildAndCompile`, and `KeyValue.buildAndCompile` make that terminal step easier to scan. Keep `Json.compile personSchema` when the named schema is reused.
80
78
81
79
## Why use it
82
80
83
81
- The schema mirrors the data, so changes to the wire contract are visible in one place.
84
82
- Encode and decode come from the same definition, so drift is harder to introduce accidentally.
85
-
-`Json.compile`/ `Json.codec`and `Xml.compile` / `Xml.codec` reuse the same schema instead of making you maintain separate mappings.
83
+
-`Json.compile` and `Xml.compile` reuse the same schema instead of making you maintain separate mappings.
86
84
- Domain refinement stays explicit through `Schema.map` and `Schema.tryMap` instead of being buried in serializer settings.
87
85
- Versioned message and config contracts stay deliberate because the wire shape is authored directly.
Copy file name to clipboardExpand all lines: docs/GETTING_STARTED.md
+19-12Lines changed: 19 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,15 +31,26 @@ define<'T>
31
31
|> build
32
32
```
33
33
34
-
Compile explicitly and reuse the resulting codec:
34
+
When the schema is named and reused, compile explicitly and keep the resulting codec:
35
35
36
36
```fsharp
37
37
let codec = Json.compile personSchema
38
38
let json = Json.serialize codec person
39
39
let roundTripped = Json.deserialize codec json
40
40
```
41
41
42
-
The format modules also expose `Json.codec`, `Xml.codec`, `Yaml.codec`, and `KeyValue.codec` as direct aliases for the corresponding `compile` functions. This tutorial uses the shorter `*.codec` form in the small examples to reduce noise, but the separate `compile` step is still the important performance habit: compile once, keep the codec, and reuse it.
42
+
When the schema only exists to feed one inline pipeline, end the authoring flow with the format-specific `buildAndCompile` helper:
43
+
44
+
```fsharp
45
+
let codec =
46
+
define<Person>
47
+
|> construct makePerson
48
+
|> field "id" _.Id
49
+
|> field "name" _.Name
50
+
|> Json.buildAndCompile
51
+
```
52
+
53
+
Use `buildAndCompile` to make inline examples easier to scan. Use `Json.compile personSchema` when the schema itself is named, linked from other schemas, or reused across multiple codecs.
43
54
44
55
## How to read a schema
45
56
@@ -61,14 +72,12 @@ open CodecMapper.Schema
61
72
type Person = { Id: int; Name: string }
62
73
let makePerson id name = { Id = id; Name = name }
63
74
64
-
let personSchema =
75
+
let jsonCodec =
65
76
define<Person>
66
77
|> construct makePerson
67
78
|> field "id" _.Id
68
79
|> field "name" _.Name
69
-
|> build
70
-
71
-
let jsonCodec = Json.codec personSchema
80
+
|> Json.buildAndCompile
72
81
73
82
let person = { Id = 1; Name = "Ada" }
74
83
let json = Json.serialize jsonCodec person
@@ -86,7 +95,7 @@ Output:
86
95
Name = "Ada" }
87
96
```
88
97
89
-
This tutorial keeps using the shorter `Json.codec` spelling in the small snippets:
98
+
This tutorial uses `Json.buildAndCompile` for the small inline snippets:
90
99
91
100
```fsharp
92
101
open CodecMapper
@@ -95,17 +104,15 @@ open CodecMapper.Schema
95
104
type Person = { Id: int; Name: string }
96
105
let makePerson id name = { Id = id; Name = name }
97
106
98
-
let personSchema =
107
+
let jsonCodec =
99
108
define<Person>
100
109
|> construct makePerson
101
110
|> field "id" _.Id
102
111
|> field "name" _.Name
103
-
|> build
104
-
105
-
let jsonCodec = Json.codec personSchema
112
+
|> Json.buildAndCompile
106
113
```
107
114
108
-
In longer-lived application code, prefer keeping the explicit `Json.compile personSchema` shape when you want to emphasize that the codec should be created once and reused.
115
+
When you keep the schema in a named value, prefer the explicit `Json.compile personSchema` shape so the reuse boundary stays obvious.
Copy file name to clipboardExpand all lines: docs/HOW_TO_MODEL_A_BASIC_RECORD.md
+3-5Lines changed: 3 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,14 +9,12 @@ open CodecMapper.Schema
9
9
type Person = { Id: int; Name: string }
10
10
let makePerson id name = { Id = id; Name = name }
11
11
12
-
let personSchema =
12
+
let codec =
13
13
define<Person>
14
14
|> construct makePerson
15
15
|> field "id" _.Id
16
16
|> field "name" _.Name
17
-
|> build
18
-
19
-
let codec = Json.codec personSchema
17
+
|> Json.buildAndCompile
20
18
```
21
19
22
-
This is the smallest authored schema shape: define the record target, provide the constructor, then map each field explicitly.
20
+
This is the smallest authored schema shape: define the record target, provide the constructor, then map each field explicitly and finish with `Json.buildAndCompile`.
0 commit comments