Skip to content

Commit bf04dce

Browse files
committed
Document AllowUnorderedTag
1 parent d209b90 commit bf04dce

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

docs/Customizing.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,27 @@ type Location =
406406
// Instead of {"Item":{"lat":48.858,"long":2.295}}
407407
```
408408
409+
#### `AllowUnorderedTag`
410+
411+
`JsonUnionEncoding.AllowUnorderedTag` is enabled by default.
412+
It takes effect during deserialization in AdjacentTag and InternalTag modes.
413+
When it is disabled, the name of the case must be the first field of the JSON object.
414+
When it is enabled, the name of the case may come later in the object, at the cost of a slight performance penalty if it does.
415+
416+
For example, without `AllowUnorderedTag`, the following will fail to parse:
417+
418+
```fsharp
419+
JsonSerializer.Deserialize("""{"Fields":[3.14],"Case":"WithOneArg"}""", options)
420+
// --> Error: Failed to find union case field for Example: expected Case
421+
```
422+
423+
Whereas with `AllowUnorderedTag`, it will succeed:
424+
425+
```fsharp
426+
JsonSerializer.Deserialize("""{"Fields":[3.14],"Case":"WithOneArg"}""", options)
427+
// --> WithOneArg 3.14
428+
```
429+
409430
### Combined flags
410431

411432
`JsonUnionEncoding` also contains a few items that combine several of the above flags.
@@ -417,6 +438,7 @@ type Location =
417438
JsonUnionEncoding.AdjacentTag
418439
||| JsonUnionEncoding.UnwrapOption
419440
||| JsonUnionEncoding.UnwrapSingleCaseUnions
441+
||| JsonUnionEncoding.AllowUnorderedTag
420442
```
421443
422444
It is particularly useful if you want to use the default encoding with some additional options, for example:
@@ -430,6 +452,7 @@ type Location =
430452
431453
```fsharp
432454
JsonUnionEncoding.AdjacentTag
455+
||| JsonUnionEncoding.AllowUnorderedTag
433456
```
434457
435458
* `JsonUnionEncoding.ThothLike` causes similar behavior to the library [Thoth.Json](https://thoth-org.github.io/Thoth.Json/).
@@ -438,6 +461,7 @@ type Location =
438461
```fsharp
439462
JsonUnionEncoding.InternalTag
440463
||| JsonUnionEncoding.UnwrapFieldlessTags
464+
||| JsonUnionEncoding.AllowUnorderedTag
441465
```
442466
443467
* `JsonUnionEncoding.FSharpLuLike` causes similar behavior to the library [FSharpLu.Json](https://github.com/microsoft/fsharplu/wiki/FSharpLu.Json) in Compact mode.
@@ -448,6 +472,7 @@ type Location =
448472
||| JsonUnionEncoding.UnwrapFieldlessTags
449473
||| JsonUnionEncoding.UnwrapOption
450474
||| JsonUnionEncoding.UnwrapSingleFieldCases
475+
||| JsonUnionEncoding.AllowUnorderedTag
451476
```
452477
453478
## `unionTagName`

src/FSharp.SystemTextJson/Union.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ type JsonUnionConverter<'T>
316316
match document.RootElement.TryGetProperty fsOptions.UnionTagName with
317317
| true, element -> getCaseByTagString (element.GetString())
318318
| false, _ ->
319-
sprintf "Failed to find union case field for %s: expected %s" ty.FullName fsOptions.UnionFieldsName
319+
sprintf "Failed to find union case field for %s: expected %s" ty.FullName fsOptions.UnionTagName
320320
|> JsonException
321321
|> raise
322322

@@ -329,7 +329,7 @@ type JsonUnionConverter<'T>
329329
elif fsOptions.UnionEncoding.HasFlag JsonUnionEncoding.AllowUnorderedTag then
330330
struct (getCaseFromDocument reader, true)
331331
else
332-
sprintf "Failed to find union case field for %s: expected %s" ty.FullName fsOptions.UnionFieldsName
332+
sprintf "Failed to find union case field for %s: expected %s" ty.FullName fsOptions.UnionTagName
333333
|> JsonException
334334
|> raise
335335

0 commit comments

Comments
 (0)