File tree Expand file tree Collapse file tree 1 file changed +31
-3
lines changed Expand file tree Collapse file tree 1 file changed +31
-3
lines changed Original file line number Diff line number Diff line change 11# Tuples
22
3- In TypeScript, tuples are a compact format for data structures. They're like fixed-length arrays
4- that only contain the type. This is especially useful when using JSON, as including property names
5- means the messages are much larger.
3+ In TypeScript,
4+ [ tuples] ( https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types )
5+ are a compact format for data structures. They're like fixed-length arrays that only contain the
6+ type, not the property names. Excluding the property names is especially useful when size and speed
7+ is important, because the JSON will be much more compact.
8+
9+ ### Tuple example
10+
11+ Here's an example of a tuple definition in TypeScript:
12+
13+ ``` typescript
14+ type StringNumberPair = [str : string , num : number ];
15+ ```
16+
17+ This would get serialized to a JSON array
18+
19+ [ // ] : # ( @formatter:off )
20+ ``` json
21+ [" some string value" , 123 ]
22+ ```
23+ [ // ] : # ( @formatter:on )
24+
25+ which is more compact than an equivalent JSON object, which requires property names.
26+
27+ [ // ] : # ( @formatter:off )
28+ ``` json
29+ { "str" : " some string value" , "num" : 123 }
30+ ```
31+ [ // ] : # ( @formatter:on )
32+
33+ ## Tuples in KxsTsGen
634
735Tuples are a bit difficult to create in Kotlinx Serialization, but KxsTsGen includes
836[ TupleSerializer] ( ../modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/experiments/tuple.kt )
You can’t perform that action at this time.
0 commit comments