@@ -11,26 +11,29 @@ Defines the standard for how the Seam SDKs and other Seam API consumers
1111should serialize objects to [ URLSearchParams] in HTTP GET requests.
1212Serves as a reference implementation for Seam SDKs in other languages.
1313
14- See this test for the [ serialization behavior ] ( ./test/serialization.test.ts ) .
14+ This serializer may be used as a true inverse operation to [ @ seamapi/url-search-params-parser ] [ @url-search-params-parser ] .
1515
1616[ URLSearchParams ] : https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
17+ [ @url-search-params-parser ] : https://github.com/seamapi/url-search-params-parser
1718
1819### Serialization strategy
1920
2021Serialization uses
2122[ ` URLSearchParams.toString() ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString#return_value )
2223which encodes most non-alphanumeric characters.
2324
24- Serialization is guaranteed to be well-defined within each type, i.e. ,
25- if the type of value for a given key in the query string is fixed and known by
26- the consumer parsing the string, it can be unambigously parsed back to the original primitive value.
27-
25+ - The primitive ` null ` is serialized to an empty value ,
26+ e.g., ` { foo: null } ` serializes to ` foo= ` .
27+ - Any ` undefined ` values are removed,
28+ e.g., ` { foo: undefined, bar: 1 } ` serializes to ` bar=1 ` .
2829- The primitive type ` string ` is serialized using ` .toString() ` .
30+ - Serialization of the empty string
31+ is not supported and will throw an ` UnserializableParamError ` .
32+ Otherwise, the serialization of ` { foo: null } ` would conflict with ` { foo: '' } ` .
33+ This serializer chooses to support the more common and more useful case of ` null ` .
2934- The primitive ` number ` and ` bigint ` types are serialized using ` .toString() ` .
3035- The primitive ` boolean ` type is serialized using ` .toString() ` ,
3136 e.g., ` { foo: true, bar: false } ` serializes to ` foo=true&bar=false ` .
32- - The primitive ` null ` and ` undefined ` values are removed,
33- e.g., ` { foo: null, bar: undefined, baz: 1 } ` serializes to ` baz=1 ` .
3437- ` Date ` objects are detected and serialized using ` Date.toISOString() ` ,
3538 e.g., ` { foo: new Date(0) } ` serializes to ` foo=1970-01-01T00%3A00%3A00.000Z ` .
3639- ` Temporal.Instant ` objects are detected and serialized by first converting them to ` Date `
@@ -41,20 +44,60 @@ the consumer parsing the string, it can be unambigously parsed back to the origi
4144 - The array ` { foo: [1, 2] } ` serializes to ` foo=1&foo=2 ` .
4245 - The single element array ` { foo: [1] } ` serializes to ` foo=1 ` .
4346 - The empty array ` { foo: [] } ` serializes to ` foo= ` .
47+ As this serialization overlaps with ` null ` , parser implementations are advised
48+ not to support nullable array parameters and parse this as the empty array.
49+ - To support typed tuples, serialization of arrays containing mixed values is allowed.
4450 - Serialization of arrays containing ` null ` or ` undefined ` values
4551 is not supported and will throw an ` UnserializableParamError ` .
46- - Serialization of the single element array containing the empty string
52+ - Serialization of arrays containing the empty string
4753 is not supported and will throw an ` UnserializableParamError ` .
4854 Otherwise, the serialization of ` { foo: [''] } ` would conflict with ` { foo: [] } ` .
4955 This serializer chooses to support the more common and more useful case of an empty array.
5056- Serialization of objects and nested objects first serializes the keys
5157 to dot-path format and then serializes the values as above, e.g.,
5258 ` { foo: 'a', bar: { baz: 'b', fizz: [1, 2] } } ` serializes to
5359 ` foo=a&bar.baz=b&bar.fizz=1&bar.fizz=2 ` .
60+ - Serialization of keys containing a ` . `
61+ is not supported and will throw an ` UnserializableParamError ` .
5462- Serialization of nested arrays or objects nested inside arrays
5563 is not supported and will throw an ` UnserializableParamError ` .
5664- Serialization of functions or other objects is
5765 is not supported and will throw an ` UnserializableParamError ` .
66+ - Serialization of ` NaN ` , ` Infinity ` , and ` -Infinity `
67+ is not supported and will throw an ` UnserializableParamError ` .
68+
69+ ### Compatible parsing strategy
70+
71+ Serialization is guaranteed to be well-defined within each type, i.e.,
72+ if the value-type for a given key in the query string is fixed and known by
73+ the consumer parsing the string, it can be unambigously parsed back to the original primitive value:
74+
75+ - A parser can implement a inverse function to the serialization if it uses a schema which,
76+ for each node, defines a type matching exactly one of the primitive or nested types below.
77+ - Any node may be marked optional, i.e., ` undefined ` , which corresponds to the absence of the key in the query string.
78+ - The parser may choose ` Temporal.Instant ` as an equivalent alternative to ` Date ` .
79+ - Object keys must not include a ` . ` .
80+
81+ ##### Primitive
82+
83+ - ` string | null ` .
84+ - Excludes zero-length strings.
85+ - ` number | null ` .
86+ - ` bigint | null ` .
87+ - ` boolean | null ` .
88+ - ` Date | null ` .
89+ - ` Array<string | number | bigint | boolean | Date> ` .
90+ - Excludes zero-length strings.
91+ - Arrays must use a single value-type.
92+ - Otherwise, the array may be a tuple which may use a different value-type per slot.
93+
94+ #### Nested
95+
96+ - ` object | null ` .
97+ - Must meet the same constraints as the top level schema.
98+ - ` Record | null ` .
99+ - The record type must use a ` string ` type for the key,
100+ and a single primitive or single nested type for the value.
58101
59102## Installation
60103
0 commit comments