Skip to content

Commit 17b5fe2

Browse files
committed
add tuple converter docs
1 parent 0f286a3 commit 17b5fe2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

.jekyll-metadata

1.27 KB
Binary file not shown.

_docs/more/json-more.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ public enum MyFlagsEnum
7272

7373
To use this converter, apply the `[JsonConverter(typeof(EnumStringConverter<T>))]` to either the enum or an enum-valued property.
7474

75+
# Value tuple serialization
76+
77+
The `JsonArrayTupleConverter` will handle any size `ValueTuple<T>`/`ValueType<T1, T2>`/etc. by serializing the values to and from an array.
78+
79+
> Even though the `ValueTuple<T1, T2, ...>` classes only have up to eight generic parameters, the C# syntax `(v1, v2, ...)` does support tuples of any size. This is accomplished by stuffing values past the first seven into another `ValueTuple<...>` as the eighth value. Just like the compiler, the converter will automatically handle this for you.
80+
{: .prompt-tip }
81+
82+
Using the converter is simple:
83+
84+
```c#
85+
var options = new JsonSerializerOptions
86+
{
87+
Converters = { JsonArrayTupleConverter.Instance }
88+
};
89+
var json = JsonSerializer.Serialize((1, "string"), options); // [1,"string"]
90+
var tuple = JsonSerializer.Deserialize<(int, string)>(json, options);
91+
```
92+
7593
# Data conversions {#more-conversion}
7694

7795
## `.AsNode()` extension {#more-asnode}

0 commit comments

Comments
 (0)