@outfoxx/sunday 1.0.8 → 1.1.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -2
- package/dist/any-type.d.ts +2 -0
- package/dist/class-type.d.ts +2 -2
- package/dist/date-time-types.d.ts +2 -7
- package/dist/date-time-types.js +2 -4
- package/dist/date-time-types.js.map +1 -1
- package/dist/event-parser.d.ts +17 -0
- package/dist/event-parser.js +140 -0
- package/dist/event-parser.js.map +1 -0
- package/dist/fetch-event-source.d.ts +12 -10
- package/dist/fetch-event-source.js +141 -138
- package/dist/fetch-event-source.js.map +1 -1
- package/dist/fetch-request-factory.d.ts +10 -5
- package/dist/fetch-request-factory.js +87 -71
- package/dist/fetch-request-factory.js.map +1 -1
- package/dist/fetch.d.ts +7 -1
- package/dist/fetch.js +61 -8
- package/dist/fetch.js.map +1 -1
- package/dist/header-parameters.d.ts +3 -0
- package/dist/header-parameters.js +40 -0
- package/dist/header-parameters.js.map +1 -0
- package/dist/index.d.ts +12 -12
- package/dist/index.js +12 -12
- package/dist/index.js.map +1 -1
- package/dist/{binary-decoder.d.ts → media-type-codecs/binary-decoder.d.ts} +2 -2
- package/dist/{binary-decoder.js → media-type-codecs/binary-decoder.js} +0 -0
- package/dist/media-type-codecs/binary-decoder.js.map +1 -0
- package/dist/{binary-encoder.d.ts → media-type-codecs/binary-encoder.d.ts} +0 -0
- package/dist/{binary-encoder.js → media-type-codecs/binary-encoder.js} +0 -0
- package/dist/media-type-codecs/binary-encoder.js.map +1 -0
- package/dist/media-type-codecs/cbor-decoder.d.ts +39 -0
- package/dist/media-type-codecs/cbor-decoder.js +413 -0
- package/dist/media-type-codecs/cbor-decoder.js.map +1 -0
- package/dist/media-type-codecs/cbor-encoder.d.ts +43 -0
- package/dist/media-type-codecs/cbor-encoder.js +226 -0
- package/dist/media-type-codecs/cbor-encoder.js.map +1 -0
- package/dist/media-type-codecs/cbor-tags.d.ts +4 -0
- package/dist/media-type-codecs/cbor-tags.js +5 -0
- package/dist/media-type-codecs/cbor-tags.js.map +1 -0
- package/dist/media-type-codecs/json-decoder.d.ts +39 -0
- package/dist/media-type-codecs/json-decoder.js +367 -0
- package/dist/media-type-codecs/json-decoder.js.map +1 -0
- package/dist/media-type-codecs/json-encoder.d.ts +44 -0
- package/dist/media-type-codecs/json-encoder.js +263 -0
- package/dist/media-type-codecs/json-encoder.js.map +1 -0
- package/dist/media-type-codecs/media-type-decoder.d.ts +11 -0
- package/dist/media-type-codecs/media-type-decoder.js +6 -0
- package/dist/media-type-codecs/media-type-decoder.js.map +1 -0
- package/dist/{media-type-decoders.d.ts → media-type-codecs/media-type-decoders.d.ts} +5 -4
- package/dist/{media-type-decoders.js → media-type-codecs/media-type-decoders.js} +6 -6
- package/dist/media-type-codecs/media-type-decoders.js.map +1 -0
- package/dist/{media-type-encoder.d.ts → media-type-codecs/media-type-encoder.d.ts} +5 -1
- package/dist/media-type-codecs/media-type-encoder.js +11 -0
- package/dist/media-type-codecs/media-type-encoder.js.map +1 -0
- package/dist/{media-type-encoders.d.ts → media-type-codecs/media-type-encoders.d.ts} +5 -4
- package/dist/{media-type-encoders.js → media-type-codecs/media-type-encoders.js} +8 -8
- package/dist/media-type-codecs/media-type-encoders.js.map +1 -0
- package/dist/{url-encoder.d.ts → media-type-codecs/www-form-url-encoder.d.ts} +8 -8
- package/dist/{url-encoder.js → media-type-codecs/www-form-url-encoder.js} +38 -37
- package/dist/media-type-codecs/www-form-url-encoder.js.map +1 -0
- package/dist/media-type.d.ts +91 -13
- package/dist/media-type.js +273 -15
- package/dist/media-type.js.map +1 -1
- package/dist/problem.d.ts +24 -9
- package/dist/problem.js +140 -6
- package/dist/problem.js.map +1 -1
- package/dist/request-factory.d.ts +10 -10
- package/dist/sunday-error.d.ts +11 -0
- package/dist/sunday-error.js +18 -0
- package/dist/sunday-error.js.map +1 -0
- package/dist/util/any.d.ts +2 -0
- package/dist/util/any.js +11 -0
- package/dist/util/any.js.map +1 -0
- package/dist/util/error.d.ts +2 -0
- package/dist/util/error.js +13 -0
- package/dist/util/error.js.map +1 -0
- package/dist/util/hex.js +3 -2
- package/dist/util/hex.js.map +1 -1
- package/dist/util/rxjs.d.ts +3 -0
- package/dist/util/rxjs.js +9 -5
- package/dist/util/rxjs.js.map +1 -1
- package/dist/util/temporal.d.ts +2 -0
- package/dist/util/temporal.js +18 -0
- package/dist/util/temporal.js.map +1 -0
- package/package.json +27 -21
- package/src/any-type.ts +4 -0
- package/src/class-type.ts +6 -2
- package/src/date-time-types.ts +22 -9
- package/src/event-parser.ts +190 -0
- package/src/fetch-event-source.ts +149 -159
- package/src/fetch-request-factory.ts +129 -101
- package/src/fetch.ts +65 -14
- package/src/header-parameters.ts +52 -0
- package/src/index.ts +12 -12
- package/src/{binary-decoder.ts → media-type-codecs/binary-decoder.ts} +3 -3
- package/src/{binary-encoder.ts → media-type-codecs/binary-encoder.ts} +0 -0
- package/src/media-type-codecs/cbor-decoder.ts +515 -0
- package/src/media-type-codecs/cbor-encoder.ts +307 -0
- package/src/media-type-codecs/cbor-tags.ts +4 -0
- package/src/media-type-codecs/json-decoder.ts +470 -0
- package/src/media-type-codecs/json-encoder.ts +328 -0
- package/src/media-type-codecs/media-type-decoder.ts +20 -0
- package/src/{media-type-decoders.ts → media-type-codecs/media-type-decoders.ts} +21 -13
- package/src/media-type-codecs/media-type-encoder.ts +31 -0
- package/src/{media-type-encoders.ts → media-type-codecs/media-type-encoders.ts} +23 -15
- package/src/{url-encoder.ts → media-type-codecs/www-form-url-encoder.ts} +53 -47
- package/src/media-type.ts +326 -22
- package/src/problem.ts +144 -12
- package/src/request-factory.ts +21 -12
- package/src/sunday-error.ts +37 -0
- package/src/util/any.ts +10 -0
- package/src/util/error.ts +14 -0
- package/src/util/hex.ts +3 -2
- package/src/util/rxjs.ts +16 -5
- package/src/util/temporal.ts +18 -0
- package/dist/binary-decoder.js.map +0 -1
- package/dist/binary-encoder.js.map +0 -1
- package/dist/cbor-decoder.d.ts +0 -15
- package/dist/cbor-decoder.js +0 -126
- package/dist/cbor-decoder.js.map +0 -1
- package/dist/cbor-encoder.d.ts +0 -29
- package/dist/cbor-encoder.js +0 -81
- package/dist/cbor-encoder.js.map +0 -1
- package/dist/cbor-tags.d.ts +0 -3
- package/dist/cbor-tags.js +0 -4
- package/dist/cbor-tags.js.map +0 -1
- package/dist/http-error.d.ts +0 -10
- package/dist/http-error.js +0 -45
- package/dist/http-error.js.map +0 -1
- package/dist/json-decoder.d.ts +0 -31
- package/dist/json-decoder.js +0 -139
- package/dist/json-decoder.js.map +0 -1
- package/dist/json-encoder.d.ts +0 -35
- package/dist/json-encoder.js +0 -116
- package/dist/json-encoder.js.map +0 -1
- package/dist/media-type-decoder.d.ts +0 -4
- package/dist/media-type-decoder.js +0 -2
- package/dist/media-type-decoder.js.map +0 -1
- package/dist/media-type-decoders.js.map +0 -1
- package/dist/media-type-encoder.js +0 -6
- package/dist/media-type-encoder.js.map +0 -1
- package/dist/media-type-encoders.js.map +0 -1
- package/dist/url-encoder.js.map +0 -1
- package/src/cbor-decoder.ts +0 -148
- package/src/cbor-encoder.ts +0 -95
- package/src/cbor-tags.ts +0 -3
- package/src/http-error.ts +0 -55
- package/src/json-decoder.ts +0 -164
- package/src/json-encoder.ts +0 -144
- package/src/media-type-decoder.ts +0 -5
- package/src/media-type-encoder.ts +0 -16
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DateTimeFormatter,
|
|
3
|
+
DateTimeFormatterBuilder,
|
|
4
|
+
Duration,
|
|
5
|
+
Instant,
|
|
6
|
+
LocalDate,
|
|
7
|
+
LocalDateTime,
|
|
8
|
+
LocalTime,
|
|
9
|
+
nativeJs,
|
|
10
|
+
OffsetDateTime,
|
|
11
|
+
OffsetTime,
|
|
12
|
+
ResolverStyle,
|
|
13
|
+
ZonedDateTime,
|
|
14
|
+
ZoneId,
|
|
15
|
+
ZoneOffset,
|
|
16
|
+
} from '@js-joda/core';
|
|
17
|
+
import { JsonParser } from '@outfoxx/jackson-js';
|
|
18
|
+
import { CustomMapper, Deserializer } from '@outfoxx/jackson-js/dist/@types';
|
|
19
|
+
import { AnyType } from '../any-type';
|
|
20
|
+
import {
|
|
21
|
+
StructuredMediaTypeDecoder,
|
|
22
|
+
TextMediaTypeDecoder,
|
|
23
|
+
} from './media-type-decoder';
|
|
24
|
+
import { Base64 } from '../util/base64';
|
|
25
|
+
|
|
26
|
+
export class JSONDecoder
|
|
27
|
+
implements TextMediaTypeDecoder, StructuredMediaTypeDecoder
|
|
28
|
+
{
|
|
29
|
+
static get default(): JSONDecoder {
|
|
30
|
+
return new JSONDecoder(
|
|
31
|
+
JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private readonly customDeserializers: CustomMapper<Deserializer>[];
|
|
36
|
+
private readonly parser = new JsonParser();
|
|
37
|
+
|
|
38
|
+
constructor(readonly numericDateDecoding: JSONDecoder.NumericDateDecoding) {
|
|
39
|
+
this.customDeserializers = [
|
|
40
|
+
{
|
|
41
|
+
type: () => Date,
|
|
42
|
+
mapper: this.dateDeserializer,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: () => Instant,
|
|
46
|
+
mapper: this.instantDeserializer,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
type: () => ZonedDateTime,
|
|
50
|
+
mapper: this.zonedDateTimeDeserializer,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: () => OffsetDateTime,
|
|
54
|
+
mapper: this.offsetDateTimeDeserializer,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: () => OffsetTime,
|
|
58
|
+
mapper: this.offsetTimeDeserializer,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
type: () => LocalDateTime,
|
|
62
|
+
mapper: this.localDateTimeDeserializer,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
type: () => LocalDate,
|
|
66
|
+
mapper: this.localDateDeserializer,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
type: () => LocalTime,
|
|
70
|
+
mapper: this.localTimeDeserializer,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
type: () => URL,
|
|
74
|
+
mapper: this.urlDeserializer,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
type: () => ArrayBuffer,
|
|
78
|
+
mapper: this.arrayBufferDeserializer,
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async decode<T>(response: Response, type: AnyType): Promise<T> {
|
|
84
|
+
return Promise.resolve(this.decodeObject(await response.json(), type));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
decodeText<T>(text: string, type: AnyType): T {
|
|
88
|
+
return this.parser.parse(text, {
|
|
89
|
+
deserializers: this.customDeserializers,
|
|
90
|
+
mainCreator: () => type,
|
|
91
|
+
}) as T;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
decodeObject<T>(value: unknown, type: AnyType): T {
|
|
95
|
+
return this.parser.transform(value, {
|
|
96
|
+
deserializers: this.customDeserializers,
|
|
97
|
+
mainCreator: () => type,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private instantDeserializer: Deserializer = (key: string, value: unknown) => {
|
|
102
|
+
if (value == null) {
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
if (value instanceof Instant) {
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
if (value instanceof Date) {
|
|
109
|
+
return Instant.from(nativeJs(value));
|
|
110
|
+
}
|
|
111
|
+
if (typeof value === 'number') {
|
|
112
|
+
if (
|
|
113
|
+
this.numericDateDecoding ===
|
|
114
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
115
|
+
) {
|
|
116
|
+
return Instant.ofEpochMilli(value);
|
|
117
|
+
} else if (
|
|
118
|
+
this.numericDateDecoding ===
|
|
119
|
+
JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
|
|
120
|
+
) {
|
|
121
|
+
const duration = Duration.parse(`PT${value}S`);
|
|
122
|
+
return Instant.ofEpochSecond(duration.seconds(), duration.nano());
|
|
123
|
+
} else {
|
|
124
|
+
throw Error('Unsupported date decoding format');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (typeof value === 'string') {
|
|
128
|
+
return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value));
|
|
129
|
+
}
|
|
130
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
private zonedDateTimeDeserializer: Deserializer = (
|
|
134
|
+
key: string,
|
|
135
|
+
value: unknown
|
|
136
|
+
) => {
|
|
137
|
+
if (value == null) {
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
if (value instanceof ZonedDateTime) {
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
if (value instanceof Date) {
|
|
144
|
+
const instant = Instant.from(nativeJs(value));
|
|
145
|
+
return ZonedDateTime.ofInstant(instant, ZoneId.UTC);
|
|
146
|
+
}
|
|
147
|
+
if (typeof value === 'number') {
|
|
148
|
+
if (
|
|
149
|
+
this.numericDateDecoding ===
|
|
150
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
151
|
+
) {
|
|
152
|
+
const instant = Instant.ofEpochMilli(value);
|
|
153
|
+
return ZonedDateTime.ofInstant(instant, ZoneId.UTC);
|
|
154
|
+
} else if (
|
|
155
|
+
this.numericDateDecoding ===
|
|
156
|
+
JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
|
|
157
|
+
) {
|
|
158
|
+
const duration = Duration.parse(`PT${value}S`);
|
|
159
|
+
const instant = Instant.ofEpochSecond(
|
|
160
|
+
duration.seconds(),
|
|
161
|
+
duration.nano()
|
|
162
|
+
);
|
|
163
|
+
return ZonedDateTime.ofInstant(instant, ZoneId.UTC);
|
|
164
|
+
} else {
|
|
165
|
+
throw Error('Unsupported date decoding format');
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (typeof value === 'string') {
|
|
169
|
+
return ZonedDateTime.from(
|
|
170
|
+
DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(value)
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
private offsetDateTimeDeserializer: Deserializer = (
|
|
177
|
+
key: string,
|
|
178
|
+
value: unknown
|
|
179
|
+
) => {
|
|
180
|
+
if (value == null) {
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
if (value instanceof OffsetDateTime) {
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
if (value instanceof Date) {
|
|
187
|
+
const instant = Instant.from(nativeJs(value));
|
|
188
|
+
return OffsetDateTime.ofInstant(instant, ZoneId.UTC);
|
|
189
|
+
}
|
|
190
|
+
if (typeof value === 'number') {
|
|
191
|
+
if (
|
|
192
|
+
this.numericDateDecoding ===
|
|
193
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
194
|
+
) {
|
|
195
|
+
const instant = Instant.ofEpochMilli(value);
|
|
196
|
+
return OffsetDateTime.ofInstant(instant, ZoneId.UTC);
|
|
197
|
+
} else if (
|
|
198
|
+
this.numericDateDecoding ===
|
|
199
|
+
JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
|
|
200
|
+
) {
|
|
201
|
+
const duration = Duration.parse(`PT${value}S`);
|
|
202
|
+
const instant = Instant.ofEpochSecond(
|
|
203
|
+
duration.seconds(),
|
|
204
|
+
duration.nano()
|
|
205
|
+
);
|
|
206
|
+
return OffsetDateTime.ofInstant(instant, ZoneId.UTC);
|
|
207
|
+
} else {
|
|
208
|
+
throw Error('Unsupported date decoding format');
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (typeof value === 'string') {
|
|
212
|
+
return OffsetDateTime.from(
|
|
213
|
+
DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(value)
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
private offsetTimeFormatter = new DateTimeFormatterBuilder()
|
|
220
|
+
.parseCaseInsensitive()
|
|
221
|
+
.append(DateTimeFormatter.ISO_LOCAL_TIME)
|
|
222
|
+
.appendOffsetId()
|
|
223
|
+
.toFormatter(ResolverStyle.STRICT);
|
|
224
|
+
|
|
225
|
+
private offsetTimeDeserializer: Deserializer = (
|
|
226
|
+
key: string,
|
|
227
|
+
value: unknown
|
|
228
|
+
) => {
|
|
229
|
+
if (value == null) {
|
|
230
|
+
return value;
|
|
231
|
+
}
|
|
232
|
+
if (value instanceof OffsetTime) {
|
|
233
|
+
return value;
|
|
234
|
+
}
|
|
235
|
+
if (value instanceof Array) {
|
|
236
|
+
let idx = 0;
|
|
237
|
+
const hour = value[idx++] as number;
|
|
238
|
+
const minute = value[idx++] as number;
|
|
239
|
+
let second = 0;
|
|
240
|
+
let nanoOfSecond = 0;
|
|
241
|
+
if (value.length > 3) {
|
|
242
|
+
second = value[idx++];
|
|
243
|
+
if (value.length > 4) {
|
|
244
|
+
nanoOfSecond = value[idx++];
|
|
245
|
+
if (
|
|
246
|
+
this.numericDateDecoding ==
|
|
247
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
248
|
+
) {
|
|
249
|
+
// millis to nanos
|
|
250
|
+
nanoOfSecond *= 1_000_000;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const offset = value[idx++] as string;
|
|
255
|
+
return OffsetTime.of(
|
|
256
|
+
hour,
|
|
257
|
+
minute,
|
|
258
|
+
second,
|
|
259
|
+
nanoOfSecond,
|
|
260
|
+
ZoneOffset.of(offset)
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
if (typeof value === 'string') {
|
|
264
|
+
return OffsetTime.from(this.offsetTimeFormatter.parse(value));
|
|
265
|
+
}
|
|
266
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
private localDateTimeDeserializer: Deserializer = (
|
|
270
|
+
key: string,
|
|
271
|
+
value: unknown
|
|
272
|
+
) => {
|
|
273
|
+
if (value == null) {
|
|
274
|
+
return value;
|
|
275
|
+
}
|
|
276
|
+
if (value instanceof LocalDateTime) {
|
|
277
|
+
return value;
|
|
278
|
+
}
|
|
279
|
+
if (value instanceof Date) {
|
|
280
|
+
const instant = Instant.ofEpochMilli(value.getTime());
|
|
281
|
+
return LocalDateTime.ofInstant(instant, ZoneId.UTC);
|
|
282
|
+
}
|
|
283
|
+
if (value instanceof Array) {
|
|
284
|
+
let idx = 0;
|
|
285
|
+
const year = value[idx++];
|
|
286
|
+
const month = value[idx++];
|
|
287
|
+
const day = value[idx++];
|
|
288
|
+
const hour = value[idx++];
|
|
289
|
+
const minute = value[idx++];
|
|
290
|
+
let second = 0;
|
|
291
|
+
let nanoOfSecond = 0;
|
|
292
|
+
if (value.length > 5) {
|
|
293
|
+
second = value[idx++];
|
|
294
|
+
if (value.length > 6) {
|
|
295
|
+
nanoOfSecond = value[idx++];
|
|
296
|
+
if (
|
|
297
|
+
this.numericDateDecoding ==
|
|
298
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
299
|
+
) {
|
|
300
|
+
// millis to nanos
|
|
301
|
+
nanoOfSecond *= 1_000_000;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return LocalDateTime.of(
|
|
306
|
+
year,
|
|
307
|
+
month,
|
|
308
|
+
day,
|
|
309
|
+
hour,
|
|
310
|
+
minute,
|
|
311
|
+
second,
|
|
312
|
+
nanoOfSecond
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
if (typeof value === 'string') {
|
|
316
|
+
return LocalDateTime.from(
|
|
317
|
+
DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(value)
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
private localDateDeserializer: Deserializer = (
|
|
324
|
+
key: string,
|
|
325
|
+
value: unknown
|
|
326
|
+
) => {
|
|
327
|
+
if (value == null) {
|
|
328
|
+
return value;
|
|
329
|
+
}
|
|
330
|
+
if (value instanceof LocalDate) {
|
|
331
|
+
return value;
|
|
332
|
+
}
|
|
333
|
+
if (value instanceof Date) {
|
|
334
|
+
const instant = Instant.ofEpochMilli(value.getTime());
|
|
335
|
+
return LocalDate.ofInstant(instant, ZoneId.UTC);
|
|
336
|
+
}
|
|
337
|
+
if (value instanceof Array) {
|
|
338
|
+
const year = value[0];
|
|
339
|
+
const month = value[1];
|
|
340
|
+
const day = value[2];
|
|
341
|
+
return LocalDate.of(year, month, day);
|
|
342
|
+
}
|
|
343
|
+
if (typeof value === 'number') {
|
|
344
|
+
return LocalDate.ofEpochDay(value);
|
|
345
|
+
}
|
|
346
|
+
if (typeof value === 'string') {
|
|
347
|
+
return LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse(value));
|
|
348
|
+
}
|
|
349
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
private localTimeDeserializer: Deserializer = (
|
|
353
|
+
key: string,
|
|
354
|
+
value: unknown
|
|
355
|
+
) => {
|
|
356
|
+
if (value == null) {
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
if (value instanceof LocalTime) {
|
|
360
|
+
return value;
|
|
361
|
+
}
|
|
362
|
+
if (value instanceof Date) {
|
|
363
|
+
const instant = Instant.ofEpochMilli(value.getTime());
|
|
364
|
+
return LocalTime.ofInstant(instant, ZoneId.UTC);
|
|
365
|
+
}
|
|
366
|
+
if (value instanceof Array) {
|
|
367
|
+
let idx = 0;
|
|
368
|
+
const hour = value[idx++];
|
|
369
|
+
const minute = value[idx++];
|
|
370
|
+
let second = 0;
|
|
371
|
+
let nanoOfSecond = 0;
|
|
372
|
+
if (value.length > 2) {
|
|
373
|
+
second = value[idx++];
|
|
374
|
+
if (value.length > 3) {
|
|
375
|
+
nanoOfSecond = value[idx++];
|
|
376
|
+
if (
|
|
377
|
+
this.numericDateDecoding ==
|
|
378
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
379
|
+
) {
|
|
380
|
+
// millis to nanos
|
|
381
|
+
nanoOfSecond *= 1_000_000;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return LocalTime.of(hour, minute, second, nanoOfSecond);
|
|
386
|
+
}
|
|
387
|
+
if (typeof value === 'number') {
|
|
388
|
+
return LocalTime.ofNanoOfDay(value);
|
|
389
|
+
}
|
|
390
|
+
if (typeof value === 'string') {
|
|
391
|
+
return LocalTime.from(DateTimeFormatter.ISO_LOCAL_TIME.parse(value));
|
|
392
|
+
}
|
|
393
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
private dateDeserializer: Deserializer = (key: string, value: unknown) => {
|
|
397
|
+
if (value == null) {
|
|
398
|
+
return value;
|
|
399
|
+
}
|
|
400
|
+
if (value instanceof Date) {
|
|
401
|
+
return value;
|
|
402
|
+
}
|
|
403
|
+
if (typeof value === 'number') {
|
|
404
|
+
if (
|
|
405
|
+
this.numericDateDecoding ===
|
|
406
|
+
JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
|
|
407
|
+
) {
|
|
408
|
+
return new Date(value);
|
|
409
|
+
} else if (
|
|
410
|
+
this.numericDateDecoding ===
|
|
411
|
+
JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
|
|
412
|
+
) {
|
|
413
|
+
return new Date(value * 1000);
|
|
414
|
+
} else {
|
|
415
|
+
throw Error('Unsupported date decoding format');
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (typeof value === 'string') {
|
|
419
|
+
return new Date(value);
|
|
420
|
+
}
|
|
421
|
+
throw new Error(`Invalid date value for property ${key}`);
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
private urlDeserializer: Deserializer = (key: string, value: unknown) => {
|
|
425
|
+
if (value == null) {
|
|
426
|
+
return value;
|
|
427
|
+
}
|
|
428
|
+
if (value instanceof URL) {
|
|
429
|
+
return value;
|
|
430
|
+
}
|
|
431
|
+
if (typeof value === 'string') {
|
|
432
|
+
return new URL(value);
|
|
433
|
+
}
|
|
434
|
+
throw Error(`Invalid URL value for property ${key}`);
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
private arrayBufferDeserializer: Deserializer = (
|
|
438
|
+
key: string,
|
|
439
|
+
value: unknown
|
|
440
|
+
) => {
|
|
441
|
+
if (value == null) {
|
|
442
|
+
return value;
|
|
443
|
+
}
|
|
444
|
+
if (value instanceof ArrayBuffer) {
|
|
445
|
+
return value;
|
|
446
|
+
}
|
|
447
|
+
if (typeof value === 'string') {
|
|
448
|
+
return Base64.decode(value);
|
|
449
|
+
}
|
|
450
|
+
throw Error(`Invalid ArrayBuffer value for property ${key}`);
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
export namespace JSONDecoder {
|
|
455
|
+
/**
|
|
456
|
+
* Configures how numeric temporal values are decoded.
|
|
457
|
+
*/
|
|
458
|
+
export enum NumericDateDecoding {
|
|
459
|
+
/**
|
|
460
|
+
* Decode numeric temporal values assuming they are seconds with decimal
|
|
461
|
+
* sub-second precision.
|
|
462
|
+
*/
|
|
463
|
+
DECIMAL_SECONDS_SINCE_EPOCH,
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Decode numeric temporal values assuming they are integer milliseconds.
|
|
467
|
+
*/
|
|
468
|
+
MILLISECONDS_SINCE_EPOCH,
|
|
469
|
+
}
|
|
470
|
+
}
|