-
Notifications
You must be signed in to change notification settings - Fork 3
/
array-serializer.ts
114 lines (98 loc) · 3.57 KB
/
array-serializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import isArray from 'lodash/isArray';
import isNull from 'lodash/isNull';
import isUndefined from 'lodash/isUndefined';
import { Serializer } from '../serializer';
import { SerializerContext } from '../serializer-context';
import { TypeLike } from '../type-like';
/**
* Array serializer.
*
* @type {ArraySerializer}
*/
export class ArraySerializer implements Serializer<Array<any>>
{
/**
* Serializes provided value.
*
* @param {TypeLike<Array<any>>} x Some value.
* @param {SerializerContext<Array<any>>} serializerContext Serializer context.
*
* @returns {TypeLike<any>} Serialized value or undefined.
*/
public serialize(x: TypeLike<Array<any>>, serializerContext: SerializerContext<Array<any>>): TypeLike<any>
{
if (isUndefined(x))
{
return serializerContext.serializedDefaultValue;
}
if (isNull(x))
{
return serializerContext.serializedNullValue;
}
if (isArray(x))
{
return serializerContext.defineReference(x, () =>
{
const arrayInput = x;
const arrayOutput = new Array<any>(x.length);
const genericSerializerContext = serializerContext.defineGenericSerializerContext(0);
for (let i = 0; i < arrayInput.length; i++)
{
const valueSerializerContext = genericSerializerContext.defineChildSerializerContext({
jsonPathKey: i,
referenceValueSetter: v => arrayOutput[i] = v
});
arrayOutput[i] = valueSerializerContext.serialize(arrayInput[i]);
}
return arrayOutput;
});
}
if (serializerContext.log.errorEnabled)
{
serializerContext.log.error(`${serializerContext.jsonPath}: cannot serialize value as array.`, x);
}
return undefined;
}
/**
* Deserializes provided value.
*
* @param {TypeLike<any>} x Some value.
* @param {SerializerContext<Array<any>>} serializerContext Serializer context.
*
* @returns {TypeLike<Array<any>>} Deserialized value.
*/
public deserialize(x: TypeLike<any>, serializerContext: SerializerContext<Array<any>>): TypeLike<Array<any>>
{
if (isUndefined(x))
{
return serializerContext.deserializedDefaultValue;
}
if (isNull(x))
{
return serializerContext.deserializedNullValue;
}
if (isArray(x))
{
return serializerContext.restoreReference(x, () =>
{
const arrayInput = x;
const arrayOutput = new Array<any>(x.length);
const genericSerializerContext = serializerContext.defineGenericSerializerContext(0);
for (let i = 0; i < arrayInput.length; i++)
{
const valueSerializerContext = genericSerializerContext.defineChildSerializerContext({
jsonPathKey: i,
referenceValueSetter: v => arrayOutput[i] = v
});
arrayOutput[i] = valueSerializerContext.deserialize(arrayInput[i]);
}
return arrayOutput;
});
}
if (serializerContext.log.errorEnabled)
{
serializerContext.log.error(`${serializerContext.jsonPath}: cannot deserialize value as array.`, x);
}
return undefined;
}
}