-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
serialization.ts
213 lines (189 loc) · 4.98 KB
/
serialization.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import GeographyPoint from "./geographyPoint";
const ISO8601DateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/i;
const GeoJSONPointTypeName = "Point";
const WorldGeodeticSystem1984 = "EPSG:4326"; // See https://epsg.io/4326
export function serialize<OutputT>(obj: unknown): OutputT {
return walk(obj, (value) => {
const result = serializeSpecialNumbers(value);
return result;
});
}
export function deserialize<OutputT>(obj: unknown): OutputT {
return walk(obj, (value) => {
let result = deserializeSpecialNumbers(value);
result = deserializeDates(result);
result = deserializeGeoPoint(result);
return result;
});
}
function walk(start: unknown, mapper: (val: any) => any): any {
// eslint-disable-next-line @typescript-eslint/ban-types
const seenMarker = new WeakMap<object, boolean>();
const result = { value: undefined };
const queue: { value: any; parent: any; key: string }[] = [
{ value: start, parent: result, key: "value" }
];
while (queue.length) {
const current = queue.shift()!;
if (typeof current.value === "object" && current.value !== null) {
if (seenMarker.has(current.value)) {
throw new Error("Cannot map a recusive structure.");
} else {
seenMarker.set(current.value, true);
}
}
const mapped = mapper(current.value);
if (current?.parent && current.key) {
current.parent[current.key] = mapped;
}
if (typeof mapped === "object" && mapped !== null) {
for (const key of Object.keys(mapped)) {
queue.push({
value: mapped[key],
parent: mapped,
key
});
}
}
}
return result.value;
}
function serializeSpecialNumbers(input: unknown): unknown {
if (typeof input === "number") {
if (isNaN(input)) {
return "NaN";
} else if (input === Infinity) {
return "INF";
} else if (input === -Infinity) {
return "-INF";
}
}
return input;
}
function deserializeSpecialNumbers(input: unknown): unknown {
if (typeof input === "string") {
if (input === "NaN") {
return NaN;
} else if (input === "INF") {
return Infinity;
} else if (input === "-INF") {
return -Infinity;
}
}
return input;
}
function deserializeDates(input: unknown): Date | unknown {
if (typeof input === "string") {
if (ISO8601DateRegex.test(input)) {
return new Date(input);
}
}
return input;
}
function deserializeGeoPoint(input: unknown): GeographyPoint | unknown {
if (isGeoJSONPoint(input)) {
return new GeographyPoint({ longitude: input.coordinates[0], latitude: input.coordinates[1] });
}
return input;
}
interface GeoJSONPoint {
type: "Point";
coordinates: [number, number];
crs: {
type: "name";
properties: {
name: "EPSG:4326";
};
};
}
function isGeoJSONPoint(obj: any): obj is GeoJSONPoint {
const requiredKeys = ["type", "coordinates"];
return isValidObject(obj, {
requiredKeys,
propertyValidator: (key) => {
switch (key) {
case "type":
return obj.type === GeoJSONPointTypeName;
break;
case "coordinates":
return isCoordinateArray(obj.coordinates);
break;
case "crs":
return isCrs(obj.crs);
break;
default:
return false;
}
}
});
}
function isCoordinateArray(maybeCoordinates: any): boolean {
if (!Array.isArray(maybeCoordinates)) {
return false;
}
if (maybeCoordinates.length !== 2) {
return false;
}
if (typeof maybeCoordinates[0] !== "number" || typeof maybeCoordinates[1] !== "number") {
return false;
}
return true;
}
function isCrs(maybeCrs: any): boolean {
return isValidObject(maybeCrs, {
requiredKeys: ["type", "properties"],
propertyValidator: (key) => {
switch (key) {
case "type":
return maybeCrs.type === "name";
break;
case "properties":
return isCrsProperties(maybeCrs.properties);
break;
default:
return false;
}
}
});
}
function isCrsProperties(maybeProperties: any): boolean {
return isValidObject(maybeProperties, {
requiredKeys: ["name"],
propertyValidator: (key) => {
if (key === "name") {
return maybeProperties.name === WorldGeodeticSystem1984;
} else {
return false;
}
}
});
}
function isValidObject(
obj: any,
options: {
requiredKeys?: string[];
propertyValidator?: (keyName: string) => boolean;
} = {}
): boolean {
if (typeof obj !== "object" || obj === null) {
return false;
}
const keys = Object.keys(obj);
if (options.requiredKeys) {
for (const requiredKey of options.requiredKeys) {
if (!keys.includes(requiredKey)) {
return false;
}
}
}
if (options.propertyValidator) {
for (const key of keys) {
if (!options.propertyValidator(key)) {
return false;
}
}
}
return true;
}