MCPcopy Create free account
hub / github.com/dipscope/TypeManager.TS / ArraySerializer

Class ArraySerializer

src/serializers/array-serializer.ts:10–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8 * @type {ArraySerializer}
9 */
10export class ArraySerializer implements Serializer<Array<any>>
11{
12 /**
13 * Serializes provided value.
14 *
15 * @param {TypeLike<Array<any>>} x Some value.
16 * @param {SerializerContext<Array<any>>} serializerContext Serializer context.
17 *
18 * @returns {TypeLike<any>} Serialized value or undefined.
19 */
20 public serialize(x: TypeLike<Array<any>>, serializerContext: SerializerContext<Array<any>>): TypeLike<any>
21 {
22 if (x === undefined)
23 {
24 return serializerContext.serializedDefaultValue;
25 }
26
27 if (x === null)
28 {
29 return serializerContext.serializedNullValue;
30 }
31
32 if (Array.isArray(x))
33 {
34 return serializerContext.defineReference(x, () =>
35 {
36 const arrayInput = x;
37 const arrayOutput = new Array<any>(x.length);
38 const genericSerializerContext = serializerContext.defineGenericSerializerContext(0);
39 const valueSerializerContext = genericSerializerContext.defineChildSerializerContext();
40 const serializer = valueSerializerContext.serializer;
41
42 valueSerializerContext.referenceValueSetter = (v, k) => arrayOutput[k] = v;
43
44 for (let i = 0; i < arrayInput.length; i++)
45 {
46 valueSerializerContext.jsonPathKey = i;
47
48 arrayOutput[i] = serializer.serialize(arrayInput[i], valueSerializerContext);
49 }
50
51 return arrayOutput;
52 });
53 }
54
55 serializerContext.logger.error('ArraySerializer', `${serializerContext.jsonPath}: cannot serialize value as array.`, x);
56
57 return undefined;
58 }
59
60 /**
61 * Deserializes provided value.
62 *
63 * @param {TypeLike<any>} x Some value.
64 * @param {SerializerContext<Array<any>>} serializerContext Serializer context.
65 *
66 * @returns {TypeLike<Array<any>>} Deserialized value.
67 */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected