Skip to content

Commit 0822481

Browse files
committed
Document collections
1 parent b1504f4 commit 0822481

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

README.md

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,63 @@ type MyHub() =
102102

103103
## Format
104104

105+
### Lists
106+
107+
F# lists are serialized as JSON arrays.
108+
109+
```fsharp
110+
JsonSerializer.Serialize([1; 2; 3], options)
111+
// --> [1,2,3]
112+
```
113+
114+
### Sets
115+
116+
F# sets are serialized as JSON arrays.
117+
118+
```fsharp
119+
JsonSerializer.Serialize(Set [1; 2; 3], options)
120+
// --> [1,2,3]
121+
```
122+
123+
### Maps
124+
125+
F# string-keyed maps are serialized as JSON objects.
126+
127+
```fsharp
128+
JsonSerializer.Serialize(Map [("a", 1); ("b", 2); ("c", 3)], options)
129+
// --> {"a":1,"b":2,"c":3}
130+
```
131+
132+
Maps with other types as keys are serialized as JSON arrays, where each item is a `[key,value]` array.
133+
134+
```fsharp
135+
JsonSerializer.Serialize(Map [(1, "a"); (2, "b"); (3, "c")], options)
136+
// --> [[1,"a"],[2,"b"],[3,"c"]]
137+
```
138+
139+
### Tuples
140+
141+
Tuples and struct tuples are serialized as JSON arrays.
142+
143+
```fsharp
144+
JsonSerializer.Serialize((1, "abc"), options)
145+
// --> [1,"abc"]
146+
147+
JsonSerializer.Serialize(struct (1, "abc"), options)
148+
// --> [1,"abc"]
149+
```
150+
105151
### Records and anonymous records
106152

107153
Records and anonymous records are serialized as JSON objects.
108154

109155
```fsharp
110156
type Example = { x: string; y: string }
111157
112-
JsonSerializer.Serialize { x = "Hello"; y = "world!" }
158+
JsonSerializer.Serialize({ x = "Hello"; y = "world!" }, options)
113159
// --> {"x":"Hello","y":"world!"}
114160
115-
JsonSerializer.Serialize {| x = "Hello"; y = "world!" |}
161+
JsonSerializer.Serialize({| x = "Hello"; y = "world!" |}, options)
116162
// --> {"x":"Hello","y":"world!"}
117163
```
118164

@@ -148,20 +194,20 @@ Here are the possible values:
148194
| WithArgs of anInt: int * aString: string
149195
| NoArgs
150196
151-
JsonSerializer.Serialize NoArgs
197+
JsonSerializer.Serialize(NoArgs, options)
152198
// --> {"Case":"NoArgs"}
153199
154-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
200+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
155201
// --> {"Case":"WithArgs","Fields":[123,"Hello world!"]}
156202
```
157203
158204
* `JsonUnionEncoding.AdjacentTag ||| JsonUnionEncoding.NamedFields` is similar, except that the fields are represented as an object instead of an array. The field names on this object are the names of the arguments.
159205
160206
```fsharp
161-
JsonSerializer.Serialize NoArgs
207+
JsonSerializer.Serialize(NoArgs, options)
162208
// --> {"Case":"NoArgs"}
163209
164-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
210+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
165211
// --> {"Case":"WithArgs","Fields":{"anInt":123,"aString":"Hello world!"}}
166212
```
167213
@@ -170,20 +216,20 @@ Here are the possible values:
170216
* `JsonUnionEncoding.ExternalTag` represents unions as a JSON object with one field, whose name is the name of the union case, and whose value is an array whose items are the arguments of the union case.
171217
172218
```fsharp
173-
JsonSerializer.Serialize NoArgs
219+
JsonSerializer.Serialize(NoArgs, options)
174220
// --> {"NoArgs":[]}
175221
176-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
222+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
177223
// --> {"WithArgs":[123,"Hello world!"]}
178224
```
179225
180226
* `JsonUnionEncoding.ExternalTag ||| JsonUnionEncoding.NamedFields` is similar, except that the fields are represented as an object instead of an array.
181227
182228
```fsharp
183-
JsonSerializer.Serialize NoArgs
229+
JsonSerializer.Serialize(NoArgs, options)
184230
// --> {"NoArgs":{}}
185231
186-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
232+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
187233
// --> {"WithArgs":{"anInt":123,"aString":"Hello world!"}}
188234
```
189235
@@ -192,41 +238,41 @@ Here are the possible values:
192238
This is the same format used by Thoth.Json.
193239
194240
```fsharp
195-
JsonSerializer.Serialize NoArgs
241+
JsonSerializer.Serialize(NoArgs, options)
196242
// --> ["NoArgs"]
197243
198-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
244+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
199245
// --> ["WithArgs",123,"Hello world!"]
200246
```
201247
202248
* `JsonUnionEncoding.InternalTag ||| JsonUnionEncoding.NamedFields` represents unions as an object whose first field has name `"Case"` and value the name of the case, and the rest of the fields have the names and values of the arguments.
203249
204250
```fsharp
205-
JsonSerializer.Serialize NoArgs
251+
JsonSerializer.Serialize(NoArgs, options)
206252
// --> {"Case":"NoArgs"}
207253
208-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
254+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
209255
// --> {"Case":"WithArgs","anInt":123,"aString":"Hello world!"}
210256
```
211257
212258
* `JsonUnionEncoding.Untagged` represents unions as an object whose fields have the names and values of the arguments. The name of the case is not encoded at all. Deserialization is only possible if the fields of all cases have different names.
213259
214260
215261
```fsharp
216-
JsonSerializer.Serialize NoArgs
262+
JsonSerializer.Serialize(NoArgs, options)
217263
// --> {}
218264
219-
JsonSerializer.Serialize (WithArgs (123, "Hello world!"))
265+
JsonSerializer.Serialize(WithArgs (123, "Hello world!"), options)
220266
// --> {"anInt":123,"aString":"Hello world!"}
221267
```
222268
223269
* Additionally, or-ing `||| JsonUnionEncoding.BareFieldlessTags` to any of the previous formats represents cases that don't have any arguments as a simple string.
224270
225271
```fsharp
226-
JsonSerializer.Serialize NoArgs
272+
JsonSerializer.Serialize(NoArgs, options)
227273
// --> "NoArgs"
228274
229-
JsonSerializer.Serialize (WithArgs (123, "HelloWorld!"))
275+
JsonSerializer.Serialize(WithArgs (123, "HelloWorld!"), options)
230276
// --> (same format as without BareFieldlessTags)
231277
```
232278

0 commit comments

Comments
 (0)