@tsed/json-mapper 8.32.2 → 8.33.0
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.
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isBoolean } from "@tsed/core";
|
|
2
|
+
import { JsonMapper } from "../decorators/jsonMapper.js";
|
|
3
|
+
/**
|
|
4
|
+
* Older runtimes (e.g. Node < 24) don't expose it, so it may be `undefined` at import time.
|
|
5
|
+
*/
|
|
6
|
+
const Temporal = globalThis.Temporal;
|
|
7
|
+
/**
|
|
8
|
+
* Mapper for the `Temporal.*` types (`Instant`, `ZonedDateTime`, `PlainDate`, `PlainDateTime`,
|
|
9
|
+
* `PlainTime`, `PlainYearMonth`, `PlainMonthDay`, `Duration`).
|
|
10
|
+
*
|
|
11
|
+
* Every Temporal type exposes a static `from(string)` factory and an ISO-8601 `toString()`, so a
|
|
12
|
+
* single mapper handles all of them: `serialize()` calls `toString()`, and `deserialize()` rebuilds
|
|
13
|
+
* the concrete type read from `ctx.type` (the deserializer sets it to the registered constructor).
|
|
14
|
+
*
|
|
15
|
+
* Registration is guarded by a runtime `Temporal` check so importing `@tsed/json-mapper` stays safe
|
|
16
|
+
* on runtimes that don't expose the global `Temporal` object.
|
|
17
|
+
*
|
|
18
|
+
* @jsonmapper
|
|
19
|
+
* @component
|
|
20
|
+
*/
|
|
21
|
+
export class TemporalMapper {
|
|
22
|
+
deserialize(data, ctx) {
|
|
23
|
+
if (isBoolean(data) || data === null || data === undefined) {
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
return ctx.type.from(data);
|
|
27
|
+
}
|
|
28
|
+
serialize(object) {
|
|
29
|
+
return object ? object.toString() : object;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (Temporal) {
|
|
33
|
+
JsonMapper(Temporal.Instant, Temporal.ZonedDateTime, Temporal.PlainDate, Temporal.PlainDateTime, Temporal.PlainTime, Temporal.PlainYearMonth, Temporal.PlainMonthDay, Temporal.Duration)(TemporalMapper);
|
|
34
|
+
}
|
package/lib/esm/index.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
export * from "./components/DateMapper.js";
|
|
5
5
|
export * from "./components/PrimitiveMapper.js";
|
|
6
6
|
export * from "./components/SymbolMapper.js";
|
|
7
|
+
export * from "./components/TemporalMapper.js";
|
|
7
8
|
export * from "./decorators/afterDeserialize.js";
|
|
8
9
|
export * from "./decorators/beforeDeserialize.js";
|
|
9
10
|
export * from "./decorators/jsonMapper.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { JsonMapperCtx, JsonMapperMethods } from "../interfaces/JsonMapperMethods.js";
|
|
2
|
+
/**
|
|
3
|
+
* Mapper for the `Temporal.*` types (`Instant`, `ZonedDateTime`, `PlainDate`, `PlainDateTime`,
|
|
4
|
+
* `PlainTime`, `PlainYearMonth`, `PlainMonthDay`, `Duration`).
|
|
5
|
+
*
|
|
6
|
+
* Every Temporal type exposes a static `from(string)` factory and an ISO-8601 `toString()`, so a
|
|
7
|
+
* single mapper handles all of them: `serialize()` calls `toString()`, and `deserialize()` rebuilds
|
|
8
|
+
* the concrete type read from `ctx.type` (the deserializer sets it to the registered constructor).
|
|
9
|
+
*
|
|
10
|
+
* Registration is guarded by a runtime `Temporal` check so importing `@tsed/json-mapper` stays safe
|
|
11
|
+
* on runtimes that don't expose the global `Temporal` object.
|
|
12
|
+
*
|
|
13
|
+
* @jsonmapper
|
|
14
|
+
* @component
|
|
15
|
+
*/
|
|
16
|
+
export declare class TemporalMapper implements JsonMapperMethods {
|
|
17
|
+
deserialize(data: string | number, ctx: JsonMapperCtx): unknown;
|
|
18
|
+
deserialize(data: boolean | null | undefined, ctx: JsonMapperCtx): boolean | null | undefined;
|
|
19
|
+
serialize(object: {
|
|
20
|
+
toString(): string;
|
|
21
|
+
} | null | undefined): any;
|
|
22
|
+
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
export * from "./components/DateMapper.js";
|
|
5
5
|
export * from "./components/PrimitiveMapper.js";
|
|
6
6
|
export * from "./components/SymbolMapper.js";
|
|
7
|
+
export * from "./components/TemporalMapper.js";
|
|
7
8
|
export * from "./decorators/afterDeserialize.js";
|
|
8
9
|
export * from "./decorators/beforeDeserialize.js";
|
|
9
10
|
export * from "./decorators/jsonMapper.js";
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/json-mapper",
|
|
3
3
|
"description": "Json mapper module for Ts.ED Framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.
|
|
5
|
+
"version": "8.33.0",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -41,11 +41,12 @@
|
|
|
41
41
|
"tslib": ">=2.7.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@tsed/barrels": "8.
|
|
45
|
-
"@tsed/core": "8.
|
|
46
|
-
"@tsed/schema": "8.
|
|
47
|
-
"@tsed/typescript": "8.
|
|
44
|
+
"@tsed/barrels": "8.33.0",
|
|
45
|
+
"@tsed/core": "8.33.0",
|
|
46
|
+
"@tsed/schema": "8.33.0",
|
|
47
|
+
"@tsed/typescript": "8.33.0",
|
|
48
48
|
"eslint": "9.12.0",
|
|
49
|
+
"temporal-polyfill": "1.0.1",
|
|
49
50
|
"typescript": "6.0.2",
|
|
50
51
|
"vitest": "4.1.4"
|
|
51
52
|
},
|
package/vitest.setup.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import {Temporal} from "temporal-polyfill";
|
|
2
|
+
|
|
3
|
+
// Provide the global `Temporal` on runtimes that don't ship it natively yet (e.g. Node < 24),
|
|
4
|
+
// so `TemporalMapper` registers and its spec runs in CI instead of being skipped. Native
|
|
5
|
+
// `Temporal` (Node >= 24/26) takes precedence and the polyfill is a no-op there.
|
|
6
|
+
(globalThis as {Temporal?: unknown}).Temporal ??= Temporal;
|