metano-runtime 3.0.1 → 3.1.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.
- package/dist/system/char-members.d.ts +51 -0
- package/dist/system/char-members.js +108 -0
- package/dist/system/collections/array-helpers.d.ts +28 -0
- package/dist/system/collections/array-helpers.js +105 -0
- package/dist/system/collections/hash-core.d.ts +5 -0
- package/dist/system/collections/hash-core.js +24 -0
- package/dist/system/collections/helpers.d.ts +16 -3
- package/dist/system/collections/helpers.js +28 -2
- package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
- package/dist/system/collections/immutable-list-builder-core.js +11 -0
- package/dist/system/collections/immutable-list-core.d.ts +8 -0
- package/dist/system/collections/immutable-list-core.js +19 -1
- package/dist/system/collections/immutable-storage.d.ts +2 -0
- package/dist/system/collections/immutable-storage.js +6 -1
- package/dist/system/collections/index.d.ts +2 -0
- package/dist/system/collections/index.js +2 -0
- package/dist/system/collections/interfaces.d.ts +13 -0
- package/dist/system/collections/list.d.ts +3 -0
- package/dist/system/collections/list.js +8 -11
- package/dist/system/collections/read-only-list.d.ts +11 -0
- package/dist/system/collections/read-only-list.js +26 -1
- package/dist/system/collections/sorted-list.d.ts +5 -0
- package/dist/system/collections/sorted-list.js +12 -1
- package/dist/system/collections/string-comparer.d.ts +14 -0
- package/dist/system/collections/string-comparer.js +45 -0
- package/dist/system/decimal-parsing.d.ts +12 -0
- package/dist/system/decimal-parsing.js +86 -0
- package/dist/system/exceptions/index.d.ts +1 -0
- package/dist/system/exceptions/index.js +1 -0
- package/dist/system/exceptions/throw-error.d.ts +7 -0
- package/dist/system/exceptions/throw-error.js +9 -0
- package/dist/system/index.d.ts +5 -0
- package/dist/system/index.js +5 -0
- package/dist/system/int64.d.ts +6 -0
- package/dist/system/int64.js +18 -9
- package/dist/system/integer-division.d.ts +12 -4
- package/dist/system/integer-division.js +19 -2
- package/dist/system/json/contract-name.d.ts +2 -1
- package/dist/system/json/contract-name.js +3 -2
- package/dist/system/json/converters.d.ts +15 -52
- package/dist/system/json/converters.js +33 -317
- package/dist/system/json/dictionary-keys.js +1 -3
- package/dist/system/json/index.d.ts +1 -1
- package/dist/system/json/index.js +1 -1
- package/dist/system/json/json-serializer.d.ts +12 -1
- package/dist/system/json/json-serializer.js +29 -1
- package/dist/system/json/json-text-writer.d.ts +17 -0
- package/dist/system/json/json-text-writer.js +175 -0
- package/dist/system/json/null-reads.d.ts +2 -1
- package/dist/system/json/null-reads.js +3 -1
- package/dist/system/json/number-from-string.d.ts +2 -0
- package/dist/system/json/number-from-string.js +24 -6
- package/dist/system/json/numeric-converters.d.ts +4 -4
- package/dist/system/json/numeric-converters.js +46 -8
- package/dist/system/json/numeric-enum-members.d.ts +3 -3
- package/dist/system/json/numeric-enum-members.js +2 -4
- package/dist/system/json/spec-plans.d.ts +102 -0
- package/dist/system/json/spec-plans.js +312 -0
- package/dist/system/json/types.d.ts +15 -9
- package/dist/system/lazy.d.ts +15 -0
- package/dist/system/lazy.js +43 -0
- package/dist/system/linq/grouping.d.ts +12 -0
- package/dist/system/linq/grouping.js +42 -0
- package/dist/system/linq/index.d.ts +4 -3
- package/dist/system/linq/index.js +3 -2
- package/dist/system/linq/linq.js +20 -1
- package/dist/system/linq/lookup.d.ts +17 -0
- package/dist/system/linq/lookup.js +20 -0
- package/dist/system/linq/operators.d.ts +35 -5
- package/dist/system/linq/operators.js +82 -31
- package/dist/system/linq/terminals.d.ts +41 -2
- package/dist/system/linq/terminals.js +122 -2
- package/dist/system/linq/types.d.ts +48 -4
- package/dist/system/math.d.ts +7 -0
- package/dist/system/math.js +14 -0
- package/dist/system/number-members.d.ts +10 -0
- package/dist/system/number-members.js +25 -0
- package/dist/system/number-parsing.d.ts +16 -4
- package/dist/system/number-parsing.js +21 -4
- package/dist/system/string-builder.d.ts +30 -0
- package/dist/system/string-builder.js +86 -0
- package/dist/system/task-completion-source.d.ts +37 -0
- package/dist/system/task-completion-source.js +81 -0
- package/dist-unchecked/system/char-members.d.ts +51 -0
- package/dist-unchecked/system/char-members.js +108 -0
- package/dist-unchecked/system/collections/array-helpers.d.ts +28 -0
- package/dist-unchecked/system/collections/array-helpers.js +105 -0
- package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
- package/dist-unchecked/system/collections/hash-core.js +24 -0
- package/dist-unchecked/system/collections/helpers.d.ts +16 -3
- package/dist-unchecked/system/collections/helpers.js +28 -2
- package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
- package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
- package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
- package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
- package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
- package/dist-unchecked/system/collections/immutable-storage.js +6 -1
- package/dist-unchecked/system/collections/index.d.ts +2 -0
- package/dist-unchecked/system/collections/index.js +2 -0
- package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
- package/dist-unchecked/system/collections/list.d.ts +3 -0
- package/dist-unchecked/system/collections/list.js +8 -11
- package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
- package/dist-unchecked/system/collections/read-only-list.js +26 -1
- package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
- package/dist-unchecked/system/collections/sorted-list.js +12 -1
- package/dist-unchecked/system/collections/string-comparer.d.ts +14 -0
- package/dist-unchecked/system/collections/string-comparer.js +45 -0
- package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
- package/dist-unchecked/system/decimal-parsing.js +86 -0
- package/dist-unchecked/system/exceptions/index.d.ts +1 -0
- package/dist-unchecked/system/exceptions/index.js +1 -0
- package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
- package/dist-unchecked/system/exceptions/throw-error.js +9 -0
- package/dist-unchecked/system/index.d.ts +5 -0
- package/dist-unchecked/system/index.js +5 -0
- package/dist-unchecked/system/int64.d.ts +6 -0
- package/dist-unchecked/system/int64.js +18 -9
- package/dist-unchecked/system/integer-division.d.ts +12 -4
- package/dist-unchecked/system/integer-division.js +19 -2
- package/dist-unchecked/system/json/contract-name.d.ts +2 -1
- package/dist-unchecked/system/json/contract-name.js +3 -2
- package/dist-unchecked/system/json/converters.d.ts +15 -52
- package/dist-unchecked/system/json/converters.js +33 -317
- package/dist-unchecked/system/json/dictionary-keys.js +1 -3
- package/dist-unchecked/system/json/index.d.ts +1 -1
- package/dist-unchecked/system/json/index.js +1 -1
- package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
- package/dist-unchecked/system/json/json-serializer.js +29 -1
- package/dist-unchecked/system/json/json-text-writer.d.ts +17 -0
- package/dist-unchecked/system/json/json-text-writer.js +175 -0
- package/dist-unchecked/system/json/null-reads.d.ts +2 -1
- package/dist-unchecked/system/json/null-reads.js +3 -1
- package/dist-unchecked/system/json/number-from-string.d.ts +2 -0
- package/dist-unchecked/system/json/number-from-string.js +24 -6
- package/dist-unchecked/system/json/numeric-converters.d.ts +4 -4
- package/dist-unchecked/system/json/numeric-converters.js +46 -8
- package/dist-unchecked/system/json/numeric-enum-members.d.ts +3 -3
- package/dist-unchecked/system/json/numeric-enum-members.js +2 -4
- package/dist-unchecked/system/json/spec-plans.d.ts +102 -0
- package/dist-unchecked/system/json/spec-plans.js +312 -0
- package/dist-unchecked/system/json/types.d.ts +15 -9
- package/dist-unchecked/system/lazy.d.ts +15 -0
- package/dist-unchecked/system/lazy.js +43 -0
- package/dist-unchecked/system/linq/grouping.d.ts +12 -0
- package/dist-unchecked/system/linq/grouping.js +42 -0
- package/dist-unchecked/system/linq/index.d.ts +4 -3
- package/dist-unchecked/system/linq/index.js +3 -2
- package/dist-unchecked/system/linq/linq.js +20 -1
- package/dist-unchecked/system/linq/lookup.d.ts +17 -0
- package/dist-unchecked/system/linq/lookup.js +20 -0
- package/dist-unchecked/system/linq/operators.d.ts +35 -5
- package/dist-unchecked/system/linq/operators.js +82 -31
- package/dist-unchecked/system/linq/terminals.d.ts +41 -2
- package/dist-unchecked/system/linq/terminals.js +122 -2
- package/dist-unchecked/system/linq/types.d.ts +48 -4
- package/dist-unchecked/system/math.d.ts +7 -0
- package/dist-unchecked/system/math.js +14 -0
- package/dist-unchecked/system/number-members.d.ts +10 -0
- package/dist-unchecked/system/number-members.js +25 -0
- package/dist-unchecked/system/number-parsing.d.ts +16 -4
- package/dist-unchecked/system/number-parsing.js +21 -4
- package/dist-unchecked/system/string-builder.d.ts +30 -0
- package/dist-unchecked/system/string-builder.js +86 -0
- package/dist-unchecked/system/task-completion-source.d.ts +37 -0
- package/dist-unchecked/system/task-completion-source.js +81 -0
- package/package.json +1 -1
- package/src/system/char-members.ts +131 -0
- package/src/system/collections/array-helpers.ts +139 -0
- package/src/system/collections/hash-core.ts +31 -0
- package/src/system/collections/helpers.ts +39 -4
- package/src/system/collections/immutable-list-builder-core.ts +16 -2
- package/src/system/collections/immutable-list-core.ts +23 -1
- package/src/system/collections/immutable-storage.ts +8 -1
- package/src/system/collections/index.ts +2 -0
- package/src/system/collections/interfaces.ts +16 -0
- package/src/system/collections/list.ts +10 -15
- package/src/system/collections/read-only-list.ts +35 -1
- package/src/system/collections/sorted-list.ts +15 -1
- package/src/system/collections/string-comparer.ts +57 -0
- package/src/system/decimal-parsing.ts +109 -0
- package/src/system/exceptions/index.ts +1 -0
- package/src/system/exceptions/throw-error.ts +9 -0
- package/src/system/index.ts +5 -0
- package/src/system/int64.ts +27 -10
- package/src/system/integer-division.ts +43 -6
- package/src/system/json/contract-name.ts +3 -2
- package/src/system/json/converters.ts +38 -526
- package/src/system/json/dictionary-keys.ts +6 -4
- package/src/system/json/index.ts +1 -1
- package/src/system/json/json-serializer.ts +42 -1
- package/src/system/json/json-text-writer.ts +224 -0
- package/src/system/json/null-reads.ts +3 -1
- package/src/system/json/number-from-string.ts +31 -6
- package/src/system/json/numeric-converters.ts +60 -8
- package/src/system/json/numeric-enum-members.ts +4 -6
- package/src/system/json/spec-plans.ts +524 -0
- package/src/system/json/types.ts +15 -9
- package/src/system/lazy.ts +62 -0
- package/src/system/linq/grouping.ts +77 -0
- package/src/system/linq/index.ts +14 -0
- package/src/system/linq/linq.ts +23 -1
- package/src/system/linq/lookup.ts +34 -0
- package/src/system/linq/operators.ts +109 -17
- package/src/system/linq/terminals.ts +161 -2
- package/src/system/linq/types.ts +66 -3
- package/src/system/math.ts +19 -0
- package/src/system/number-members.ts +29 -0
- package/src/system/number-parsing.ts +24 -4
- package/src/system/string-builder.ts +117 -0
- package/src/system/task-completion-source.ts +106 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TypeSpec, ValueSpec, JsonConverter } from "./types.ts";
|
|
1
|
+
import type { TypeSpec, ValueSpec, JsonTypeInfo, JsonConverter } from "./types.ts";
|
|
2
2
|
import type { SerializerContext } from "./serializer-context.ts";
|
|
3
3
|
/**
|
|
4
4
|
* A bound serializer that uses a SerializerContext to resolve custom converters
|
|
@@ -13,6 +13,8 @@ export declare class BoundSerializer {
|
|
|
13
13
|
serialize<T>(value: T | null, spec: TypeSpec<T>): Record<string, unknown> | null;
|
|
14
14
|
/** Serialize a non-object root (collection, map, primitive, …) to a JSON-safe value. */
|
|
15
15
|
serialize<T>(value: T, spec: ValueSpec<T>): unknown;
|
|
16
|
+
/** Serialize a value to JSON text, as System.Text.Json writes it. */
|
|
17
|
+
serializeText<T>(value: T | null, spec: JsonTypeInfo<T>): string;
|
|
16
18
|
/**
|
|
17
19
|
* Deserialize a parsed JSON value into a typed instance; JSON `null` reads
|
|
18
20
|
* as `null`, except for a struct contract (`valueType`), which throws
|
|
@@ -65,6 +67,15 @@ export declare class JsonSerializer {
|
|
|
65
67
|
static serialize<T>(value: T | null, spec: TypeSpec<T>, converters?: readonly JsonConverter[]): Record<string, unknown> | null;
|
|
66
68
|
/** Serialize a non-object root (collection, map, primitive, …) to a JSON-safe value. */
|
|
67
69
|
static serialize<T>(value: T, spec: ValueSpec<T>, converters?: readonly JsonConverter[]): unknown;
|
|
70
|
+
/**
|
|
71
|
+
* Serialize a value to JSON text using the given spec, as System.Text.Json's
|
|
72
|
+
* `Serialize(value, JsonTypeInfo<T>)` writes it: no whitespace, strings
|
|
73
|
+
* escaped by its default encoder (`<` is `\u003C`, `é` is `\u00E9`),
|
|
74
|
+
* numbers in .NET's round-trip text (`1E+20`), `decimal` and 64-bit
|
|
75
|
+
* integers with their exact digits, `null` as `null`. NaN and the
|
|
76
|
+
* infinities throw `ArgumentException`, as STJ throws for them.
|
|
77
|
+
*/
|
|
78
|
+
static serializeText<T>(value: T | null, spec: JsonTypeInfo<T>, converters?: readonly JsonConverter[]): string;
|
|
68
79
|
/**
|
|
69
80
|
* Deserialize a parsed JSON value (`JSON.parse`'s tree) into a typed
|
|
70
81
|
* instance using the given spec. `JSON.parse` has already rounded every
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { serializeWithSpec, deserializeWithSpec, serializeWithValueSpec, deserializeWithValueSpec, } from "./
|
|
1
|
+
import { serializeWithSpec, deserializeWithSpec, serializeWithValueSpec, deserializeWithValueSpec, } from "./spec-plans.js";
|
|
2
2
|
import { specNumberTextLeaves } from "./exact-number-reads.js";
|
|
3
3
|
import { mayHoldNumberTextLeaf, parseJsonText, readJsonText } from "./json-text-reader.js";
|
|
4
4
|
import { readNull } from "./null-reads.js";
|
|
5
5
|
import { isValueSpec } from "./spec-kind.js";
|
|
6
|
+
import { writeJsonText } from "./json-text-writer.js";
|
|
7
|
+
import { throwIfNull } from "../argument-guards.js";
|
|
6
8
|
/**
|
|
7
9
|
* Serializes through either spec kind: an object contract walks its
|
|
8
10
|
* properties, and writes a `null` value as JSON `null` (FR-037); a value root
|
|
@@ -46,6 +48,8 @@ function deserializeWithInfo(data, spec, converters, treeNumbers) {
|
|
|
46
48
|
* text throws STJ's `JsonException` on every path.
|
|
47
49
|
*/
|
|
48
50
|
function deserializeTextWithInfo(text, spec, converters) {
|
|
51
|
+
throwIfNull(text, "json");
|
|
52
|
+
throwIfNull(spec, "jsonTypeInfo");
|
|
49
53
|
const leaves = specNumberTextLeaves(spec);
|
|
50
54
|
const readsExactTree = leaves !== undefined && mayHoldNumberTextLeaf(text, leaves);
|
|
51
55
|
if (readsExactTree) {
|
|
@@ -53,6 +57,15 @@ function deserializeTextWithInfo(text, spec, converters) {
|
|
|
53
57
|
}
|
|
54
58
|
return deserializeWithInfo(parseJsonText(text), spec, converters);
|
|
55
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Writes JSON text as System.Text.Json's `Serialize(value, JsonTypeInfo<T>)`
|
|
62
|
+
* does (#741): the tree `serialize` builds, written by `writeJsonText`
|
|
63
|
+
* (STJ's escaping and number text, not `JSON.stringify`'s).
|
|
64
|
+
*/
|
|
65
|
+
function serializeTextWithInfo(value, spec, converters) {
|
|
66
|
+
throwIfNull(spec, "jsonTypeInfo");
|
|
67
|
+
return writeJsonText(serializeWithInfo(value, spec, converters));
|
|
68
|
+
}
|
|
56
69
|
/**
|
|
57
70
|
* A bound serializer that uses a SerializerContext to resolve custom converters
|
|
58
71
|
* and look up specs by constructor.
|
|
@@ -65,6 +78,10 @@ export class BoundSerializer {
|
|
|
65
78
|
serialize(value, spec) {
|
|
66
79
|
return serializeWithInfo(value, spec, this.context.converters);
|
|
67
80
|
}
|
|
81
|
+
/** Serialize a value to JSON text, as System.Text.Json writes it. */
|
|
82
|
+
serializeText(value, spec) {
|
|
83
|
+
return serializeTextWithInfo(value, spec, this.context.converters);
|
|
84
|
+
}
|
|
68
85
|
deserialize(data, spec) {
|
|
69
86
|
return deserializeWithInfo(data, spec, this.context.converters);
|
|
70
87
|
}
|
|
@@ -101,6 +118,17 @@ export class JsonSerializer {
|
|
|
101
118
|
static serialize(value, spec, converters) {
|
|
102
119
|
return serializeWithInfo(value, spec, converters);
|
|
103
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Serialize a value to JSON text using the given spec, as System.Text.Json's
|
|
123
|
+
* `Serialize(value, JsonTypeInfo<T>)` writes it: no whitespace, strings
|
|
124
|
+
* escaped by its default encoder (`<` is `\u003C`, `é` is `\u00E9`),
|
|
125
|
+
* numbers in .NET's round-trip text (`1E+20`), `decimal` and 64-bit
|
|
126
|
+
* integers with their exact digits, `null` as `null`. NaN and the
|
|
127
|
+
* infinities throw `ArgumentException`, as STJ throws for them.
|
|
128
|
+
*/
|
|
129
|
+
static serializeText(value, spec, converters) {
|
|
130
|
+
return serializeTextWithInfo(value, spec, converters);
|
|
131
|
+
}
|
|
104
132
|
static deserialize(data, spec, converters) {
|
|
105
133
|
return deserializeWithInfo(data, spec, converters);
|
|
106
134
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes the tree `JsonSerializer.serialize` returns as the JSON text
|
|
3
|
+
* System.Text.Json's `JsonSerializer.Serialize(value, JsonTypeInfo<T>)`
|
|
4
|
+
* writes with its default options (#741, measured on .NET 10):
|
|
5
|
+
*
|
|
6
|
+
* - no whitespace;
|
|
7
|
+
* - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
|
|
8
|
+
* `\r` and `\t` keep their short escapes; every other control character,
|
|
9
|
+
* `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
|
|
10
|
+
* printable ASCII is written `\uXXXX` (upper-case hex, one escape per
|
|
11
|
+
* UTF-16 code unit), and a lone surrogate is written `\uFFFD`;
|
|
12
|
+
* - numbers in .NET's shortest round-trip text (`1E+20`, `1E-07`, `-0`),
|
|
13
|
+
* a `JSON.rawJSON` leaf (an exact `decimal` / 64-bit integer) as its text;
|
|
14
|
+
* NaN and the infinities throw `ArgumentException`, as STJ throws for them.
|
|
15
|
+
*/
|
|
16
|
+
/** The JSON text of `tree`, as System.Text.Json writes it. */
|
|
17
|
+
export declare function writeJsonText(tree: unknown): string;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes the tree `JsonSerializer.serialize` returns as the JSON text
|
|
3
|
+
* System.Text.Json's `JsonSerializer.Serialize(value, JsonTypeInfo<T>)`
|
|
4
|
+
* writes with its default options (#741, measured on .NET 10):
|
|
5
|
+
*
|
|
6
|
+
* - no whitespace;
|
|
7
|
+
* - strings escaped by `JavaScriptEncoder.Default`: `\\`, `\b`, `\f`, `\n`,
|
|
8
|
+
* `\r` and `\t` keep their short escapes; every other control character,
|
|
9
|
+
* `"`, `&`, `'`, `+`, `<`, `>`, `` ` `` and every character outside
|
|
10
|
+
* printable ASCII is written `\uXXXX` (upper-case hex, one escape per
|
|
11
|
+
* UTF-16 code unit), and a lone surrogate is written `\uFFFD`;
|
|
12
|
+
* - numbers in .NET's shortest round-trip text (`1E+20`, `1E-07`, `-0`),
|
|
13
|
+
* a `JSON.rawJSON` leaf (an exact `decimal` / 64-bit integer) as its text;
|
|
14
|
+
* NaN and the infinities throw `ArgumentException`, as STJ throws for them.
|
|
15
|
+
*/
|
|
16
|
+
import { formatDouble } from "../double-format.js";
|
|
17
|
+
import { ArgumentException } from "../exceptions/argument-exception.js";
|
|
18
|
+
import { rawJsonTextOf } from "./exact-json-number.js";
|
|
19
|
+
/** The message STJ throws for a NaN or infinite number written as JSON. */
|
|
20
|
+
const NON_FINITE_NUMBER_MESSAGE = ".NET number values such as positive and negative infinity cannot be written as valid JSON. " +
|
|
21
|
+
"To make it work when using 'JsonSerializer', consider specifying " +
|
|
22
|
+
"'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
|
|
23
|
+
"(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
|
|
24
|
+
/** The JSON text of `tree`, as System.Text.Json writes it. */
|
|
25
|
+
export function writeJsonText(tree) {
|
|
26
|
+
const parts = [];
|
|
27
|
+
writeValue(tree, parts);
|
|
28
|
+
return parts.join("");
|
|
29
|
+
}
|
|
30
|
+
function writeValue(value, parts) {
|
|
31
|
+
if (value == null) {
|
|
32
|
+
parts.push("null");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
switch (typeof value) {
|
|
36
|
+
case "string":
|
|
37
|
+
parts.push(quoted(value));
|
|
38
|
+
return;
|
|
39
|
+
case "number":
|
|
40
|
+
parts.push(numberText(value));
|
|
41
|
+
return;
|
|
42
|
+
case "bigint":
|
|
43
|
+
case "boolean":
|
|
44
|
+
parts.push(String(value));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
writeObject(value, parts);
|
|
48
|
+
}
|
|
49
|
+
function writeObject(value, parts) {
|
|
50
|
+
const rawText = rawJsonTextOf(value);
|
|
51
|
+
if (rawText !== undefined) {
|
|
52
|
+
parts.push(rawText);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const toJson = value.toJSON;
|
|
56
|
+
if (typeof toJson === "function") {
|
|
57
|
+
writeValue(toJson.call(value), parts);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(value)) {
|
|
61
|
+
writeArray(value, parts);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
writeProperties(value, parts);
|
|
65
|
+
}
|
|
66
|
+
function writeArray(items, parts) {
|
|
67
|
+
parts.push("[");
|
|
68
|
+
items.forEach((item, index) => {
|
|
69
|
+
if (index > 0) {
|
|
70
|
+
parts.push(",");
|
|
71
|
+
}
|
|
72
|
+
writeValue(item, parts);
|
|
73
|
+
});
|
|
74
|
+
parts.push("]");
|
|
75
|
+
}
|
|
76
|
+
/** An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify` skips it. */
|
|
77
|
+
function writeProperties(object, parts) {
|
|
78
|
+
let first = true;
|
|
79
|
+
parts.push("{");
|
|
80
|
+
for (const key of Object.keys(object)) {
|
|
81
|
+
const member = object[key];
|
|
82
|
+
if (member === undefined) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (!first) {
|
|
86
|
+
parts.push(",");
|
|
87
|
+
}
|
|
88
|
+
first = false;
|
|
89
|
+
parts.push(quoted(key), ":");
|
|
90
|
+
writeValue(member, parts);
|
|
91
|
+
}
|
|
92
|
+
parts.push("}");
|
|
93
|
+
}
|
|
94
|
+
function numberText(value) {
|
|
95
|
+
if (!Number.isFinite(value)) {
|
|
96
|
+
throw new ArgumentException(NON_FINITE_NUMBER_MESSAGE);
|
|
97
|
+
}
|
|
98
|
+
return formatDouble(value);
|
|
99
|
+
}
|
|
100
|
+
function quoted(text) {
|
|
101
|
+
for (let index = 0; index < text.length; index++) {
|
|
102
|
+
if (isEscaped(text.charCodeAt(index))) {
|
|
103
|
+
return `"${escaped(text)}"`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return `"${text}"`;
|
|
107
|
+
}
|
|
108
|
+
/** Whether `JavaScriptEncoder.Default` escapes the UTF-16 code unit `code`. */
|
|
109
|
+
function isEscaped(code) {
|
|
110
|
+
switch (code) {
|
|
111
|
+
case 0x22: // "
|
|
112
|
+
case 0x26: // &
|
|
113
|
+
case 0x27: // '
|
|
114
|
+
case 0x2b: // +
|
|
115
|
+
case 0x3c: // <
|
|
116
|
+
case 0x3e: // >
|
|
117
|
+
case 0x5c: // backslash
|
|
118
|
+
case 0x60: // `
|
|
119
|
+
return true;
|
|
120
|
+
default:
|
|
121
|
+
return code < 0x20 || code >= 0x7f;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** The short escape STJ keeps for `code`, or `undefined`. */
|
|
125
|
+
function shortEscapeOf(code) {
|
|
126
|
+
switch (code) {
|
|
127
|
+
case 0x08:
|
|
128
|
+
return "\\b";
|
|
129
|
+
case 0x09:
|
|
130
|
+
return "\\t";
|
|
131
|
+
case 0x0a:
|
|
132
|
+
return "\\n";
|
|
133
|
+
case 0x0c:
|
|
134
|
+
return "\\f";
|
|
135
|
+
case 0x0d:
|
|
136
|
+
return "\\r";
|
|
137
|
+
case 0x5c:
|
|
138
|
+
return "\\\\";
|
|
139
|
+
default:
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function escaped(text) {
|
|
144
|
+
let result = "";
|
|
145
|
+
for (let index = 0; index < text.length; index++) {
|
|
146
|
+
const code = text.charCodeAt(index);
|
|
147
|
+
if (isHighSurrogate(code) && isLowSurrogate(text.charCodeAt(index + 1))) {
|
|
148
|
+
result += unicodeEscape(code) + unicodeEscape(text.charCodeAt(index + 1));
|
|
149
|
+
index++;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
result += escapedCodeUnit(code);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
function escapedCodeUnit(code) {
|
|
158
|
+
const shortEscape = shortEscapeOf(code);
|
|
159
|
+
if (shortEscape !== undefined) {
|
|
160
|
+
return shortEscape;
|
|
161
|
+
}
|
|
162
|
+
if (isHighSurrogate(code) || isLowSurrogate(code)) {
|
|
163
|
+
return unicodeEscape(0xfffd);
|
|
164
|
+
}
|
|
165
|
+
return isEscaped(code) ? unicodeEscape(code) : String.fromCharCode(code);
|
|
166
|
+
}
|
|
167
|
+
function unicodeEscape(code) {
|
|
168
|
+
return `\\u${code.toString(16).toUpperCase().padStart(4, "0")}`;
|
|
169
|
+
}
|
|
170
|
+
function isHighSurrogate(code) {
|
|
171
|
+
return code >= 0xd800 && code <= 0xdbff;
|
|
172
|
+
}
|
|
173
|
+
function isLowSurrogate(code) {
|
|
174
|
+
return code >= 0xdc00 && code <= 0xdfff;
|
|
175
|
+
}
|
|
@@ -15,7 +15,8 @@ export declare function readNull(spec: JsonTypeInfo): null;
|
|
|
15
15
|
* `JsonException`, or `undefined` when the descriptor reads `null` (#471,
|
|
16
16
|
* measured on .NET 10): a `string` primitive, containers, `byte[]`, a
|
|
17
17
|
* `nullable` and a `ref` read `null`; a numeric or `bool`/`char` primitive,
|
|
18
|
-
* an enum, a branded value, a temporal value
|
|
18
|
+
* an enum, a branded value, a temporal value, a `decimal` and an
|
|
19
|
+
* `ImmutableArray<T>` (the one value-type collection, #713) reject it,
|
|
19
20
|
* naming the type the descriptor carries (the element type, wherever the
|
|
20
21
|
* value sits; the declaring contract names itself at the property).
|
|
21
22
|
*/
|
|
@@ -20,7 +20,8 @@ export function readNull(spec) {
|
|
|
20
20
|
* `JsonException`, or `undefined` when the descriptor reads `null` (#471,
|
|
21
21
|
* measured on .NET 10): a `string` primitive, containers, `byte[]`, a
|
|
22
22
|
* `nullable` and a `ref` read `null`; a numeric or `bool`/`char` primitive,
|
|
23
|
-
* an enum, a branded value, a temporal value
|
|
23
|
+
* an enum, a branded value, a temporal value, a `decimal` and an
|
|
24
|
+
* `ImmutableArray<T>` (the one value-type collection, #713) reject it,
|
|
24
25
|
* naming the type the descriptor carries (the element type, wherever the
|
|
25
26
|
* value sits; the declaring contract names itself at the property).
|
|
26
27
|
*/
|
|
@@ -36,6 +37,7 @@ export function valueTypeNameOf(descriptor) {
|
|
|
36
37
|
case "enum":
|
|
37
38
|
case "numericEnum":
|
|
38
39
|
case "branded":
|
|
40
|
+
case "immutableArray":
|
|
39
41
|
return descriptor.typeName;
|
|
40
42
|
default:
|
|
41
43
|
return undefined;
|
|
@@ -15,6 +15,8 @@ export interface SignificandParts {
|
|
|
15
15
|
}
|
|
16
16
|
/** Whether `value` fits `numberType`'s integer range. */
|
|
17
17
|
export declare function isWithinIntegerRange(value: bigint, numberType: IntegerTypeName): boolean;
|
|
18
|
+
/** Whether `numberType` is an unsigned integer type, which takes no sign. */
|
|
19
|
+
export declare function isUnsignedIntegerType(numberType: IntegerTypeName): boolean;
|
|
18
20
|
/**
|
|
19
21
|
* The number `text` spells as a `numberType`, or `undefined` when System.Text.Json
|
|
20
22
|
* rejects it. The 64-bit types answer a `bigint` in range (spec 012 FR-054);
|
|
@@ -75,10 +75,10 @@ function isWithinRange(value, min, max) {
|
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* Reads the floating-point grammar; `staysFinite` rejects a value that
|
|
78
|
-
* overflows the target type
|
|
79
|
-
*
|
|
78
|
+
* overflows the target type, and `round` brings the double to the target's
|
|
79
|
+
* precision (a `Single` rounds to single precision, #695).
|
|
80
80
|
*/
|
|
81
|
-
function floatingPointReader(staysFinite) {
|
|
81
|
+
function floatingPointReader(staysFinite, round) {
|
|
82
82
|
return (text) => {
|
|
83
83
|
const literal = FLOATING_POINT_LITERALS.get(text);
|
|
84
84
|
if (literal !== undefined) {
|
|
@@ -89,12 +89,26 @@ function floatingPointReader(staysFinite) {
|
|
|
89
89
|
return undefined;
|
|
90
90
|
}
|
|
91
91
|
const value = Number(text);
|
|
92
|
-
return staysFinite(parts, value) ? value : undefined;
|
|
92
|
+
return staysFinite(parts, value) ? round(value) : undefined;
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
95
|
function fitsDouble(_, value) {
|
|
96
96
|
return Number.isFinite(value);
|
|
97
97
|
}
|
|
98
|
+
/** The largest `Single`, `(2 − 2^-23) × 2^127`. */
|
|
99
|
+
const SINGLE_MAX_VALUE = 3.4028234663852886e38;
|
|
100
|
+
/**
|
|
101
|
+
* A finite double that {@link fitsSingle} accepted, rounded to single
|
|
102
|
+
* precision (#695). Only the threshold double itself would round to an
|
|
103
|
+
* infinity (a tie to even); its text was below the threshold, so it is
|
|
104
|
+
* `±Single.MaxValue`.
|
|
105
|
+
*/
|
|
106
|
+
function roundToFiniteSingle(value) {
|
|
107
|
+
const single = Math.fround(value);
|
|
108
|
+
return Number.isFinite(single) || !Number.isFinite(value)
|
|
109
|
+
? single
|
|
110
|
+
: Math.sign(single) * SINGLE_MAX_VALUE;
|
|
111
|
+
}
|
|
98
112
|
/**
|
|
99
113
|
* Whether the text stays below the `Single` overflow threshold. The double
|
|
100
114
|
* is already rounded, so it decides every case but one: a double equal to
|
|
@@ -130,6 +144,10 @@ const UNSIGNED_INTEGER_TYPES = /* @__PURE__ */ new Set([
|
|
|
130
144
|
"System.UInt32",
|
|
131
145
|
"System.UInt64",
|
|
132
146
|
]);
|
|
147
|
+
/** Whether `numberType` is an unsigned integer type, which takes no sign. */
|
|
148
|
+
export function isUnsignedIntegerType(numberType) {
|
|
149
|
+
return UNSIGNED_INTEGER_TYPES.has(numberType);
|
|
150
|
+
}
|
|
133
151
|
/** The integers that read exactly, as the `bigint` they compute (spec 012 FR-054). */
|
|
134
152
|
const EXACT_INTEGER_TYPES = /* @__PURE__ */ new Set([
|
|
135
153
|
"System.Int64",
|
|
@@ -169,8 +187,8 @@ const INTEGER_TOKEN_READERS = /* @__PURE__ */ lazyValue(() => integerReadersOf((
|
|
|
169
187
|
: SIGNED_INTEGER_TOKEN_PATTERN, () => (value) => value));
|
|
170
188
|
const NUMBER_TEXT_READERS = /* @__PURE__ */ lazyValue(() => ({
|
|
171
189
|
...STRICT_INTEGER_READERS(),
|
|
172
|
-
"System.Single": floatingPointReader(fitsSingle),
|
|
173
|
-
"System.Double": floatingPointReader(fitsDouble),
|
|
190
|
+
"System.Single": floatingPointReader(fitsSingle, roundToFiniteSingle),
|
|
191
|
+
"System.Double": floatingPointReader(fitsDouble, (value) => value),
|
|
174
192
|
}));
|
|
175
193
|
/**
|
|
176
194
|
* The number `text` spells as a `numberType`, or `undefined` when System.Text.Json
|
|
@@ -28,8 +28,8 @@ export declare function primitiveConverterOf(descriptor: PrimitiveDescriptor, re
|
|
|
28
28
|
* range-checks: a safe-integer `number` from untyped code writes its
|
|
29
29
|
* digits too, and any value outside the 64-bit range — a negative one for
|
|
30
30
|
* a `ulong` — or of any other kind throws `JsonException` (FR-051). A
|
|
31
|
-
*
|
|
32
|
-
* into
|
|
31
|
+
* `-0` write, a `number` or a `bigint` (`-0n === 0n`), puts `0` into the
|
|
32
|
+
* slot, a ulong's included (#545); a `-0` read into a ulong still throws.
|
|
33
33
|
* - Read as a `bigint` in range from: a `bigint`; a `rawJSON` leaf or
|
|
34
34
|
* `JsonNumberText` whose text is a JSON integer token (`-0` reads `0`,
|
|
35
35
|
* `1.0` / `1e3` are not tokens, R7 — the exact text the
|
|
@@ -41,8 +41,8 @@ export declare function primitiveConverterOf(descriptor: PrimitiveDescriptor, re
|
|
|
41
41
|
* Anything else throws System.Text.Json's conversion error (FR-052),
|
|
42
42
|
* a `JsonNonIntegerNumber` (`1.0`, `1e3` read from text) included.
|
|
43
43
|
* - The descriptor's `create` — a `[Branded]` type's companion factory —
|
|
44
|
-
* builds the read value, as the branded converter does
|
|
45
|
-
*
|
|
44
|
+
* builds the read value, as the branded converter does (added by the
|
|
45
|
+
* review of slice 012e, #450).
|
|
46
46
|
*/
|
|
47
47
|
export declare function int64ConverterOf(descriptor: Int64Descriptor, readOptions: JsonReadOptions | undefined, errorTypeName?: ErrorTypeName): ValueConverter;
|
|
48
48
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { formatSingle } from "../double-format.js";
|
|
1
2
|
import { JsonException } from "./errors.js";
|
|
2
3
|
import { isDecimalText, readDecimalText } from "./decimal-text.js";
|
|
3
4
|
import { exactBigIntLeafOf, exactNumberTextOf, JsonNonIntegerNumber, numberOfTreeValue, writeExactNumber, } from "./exact-json-number.js";
|
|
@@ -27,6 +28,8 @@ const passthroughConverter = {
|
|
|
27
28
|
};
|
|
28
29
|
/** The CLR name of a C# `char`, which reads as a one-code-unit `string`. */
|
|
29
30
|
const CHAR_TYPE_NAME = "System.Char";
|
|
31
|
+
/** The CLR name of a C# `float`, whose values read rounded to single precision. */
|
|
32
|
+
const SINGLE_TYPE_NAME = "System.Single";
|
|
30
33
|
/**
|
|
31
34
|
* A `primitive`: values pass through both ways, except that a JSON string
|
|
32
35
|
* for a numeric `numberType` is read by System.Text.Json's grammar when the
|
|
@@ -39,6 +42,9 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
|
|
|
39
42
|
if (descriptor.typeName === CHAR_TYPE_NAME) {
|
|
40
43
|
return charConverterOf(errorTypeName);
|
|
41
44
|
}
|
|
45
|
+
if (numberType === SINGLE_TYPE_NAME) {
|
|
46
|
+
return singleConverterOf(readOptions, errorTypeName);
|
|
47
|
+
}
|
|
42
48
|
if (numberType === undefined || !readsNumbersFromStrings(readOptions)) {
|
|
43
49
|
return passthroughConverter;
|
|
44
50
|
}
|
|
@@ -56,6 +62,41 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
|
|
|
56
62
|
},
|
|
57
63
|
};
|
|
58
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* A `float`: a JSON number reads rounded to single precision, as
|
|
67
|
+
* System.Text.Json parses it into a `Single` (#695), and a string reads
|
|
68
|
+
* through the number grammar (which rounds too) when the context reads
|
|
69
|
+
* numbers from strings. A finite value writes the shortest text that reads
|
|
70
|
+
* back as the same `float`, as System.Text.Json writes it (`0.1`, not the
|
|
71
|
+
* double `0.10000000149011612`), through the exact-number leaf. A
|
|
72
|
+
* non-finite value passes through: System.Text.Json throws for it without
|
|
73
|
+
* `AllowNamedFloatingPointLiterals`, which Metano does not mirror.
|
|
74
|
+
*/
|
|
75
|
+
function singleConverterOf(readOptions, errorTypeName) {
|
|
76
|
+
const readsStrings = readsNumbersFromStrings(readOptions);
|
|
77
|
+
return {
|
|
78
|
+
serialize: (value) => typeof value === "number" && Number.isFinite(value)
|
|
79
|
+
? writeExactNumber(formatSingle(value), () => Math.fround(value))
|
|
80
|
+
: passthroughConverter.serialize(value),
|
|
81
|
+
deserialize: (value) => {
|
|
82
|
+
if (typeof value === "number") {
|
|
83
|
+
return Math.fround(value);
|
|
84
|
+
}
|
|
85
|
+
const exact = exactNumberTextOf(value);
|
|
86
|
+
if (exact !== undefined) {
|
|
87
|
+
return Math.fround(Number(exact));
|
|
88
|
+
}
|
|
89
|
+
if (typeof value !== "string" || !readsStrings) {
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
const number = readNumberTextAsNumber(value, SINGLE_TYPE_NAME);
|
|
93
|
+
if (number === undefined) {
|
|
94
|
+
throw cannotConvert(SINGLE_TYPE_NAME, errorTypeName);
|
|
95
|
+
}
|
|
96
|
+
return number;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
59
100
|
/**
|
|
60
101
|
* A `long` / `ulong` — the `int64` descriptor (spec 012 FR-050), whatever
|
|
61
102
|
* the read options:
|
|
@@ -65,8 +106,8 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
|
|
|
65
106
|
* range-checks: a safe-integer `number` from untyped code writes its
|
|
66
107
|
* digits too, and any value outside the 64-bit range — a negative one for
|
|
67
108
|
* a `ulong` — or of any other kind throws `JsonException` (FR-051). A
|
|
68
|
-
*
|
|
69
|
-
* into
|
|
109
|
+
* `-0` write, a `number` or a `bigint` (`-0n === 0n`), puts `0` into the
|
|
110
|
+
* slot, a ulong's included (#545); a `-0` read into a ulong still throws.
|
|
70
111
|
* - Read as a `bigint` in range from: a `bigint`; a `rawJSON` leaf or
|
|
71
112
|
* `JsonNumberText` whose text is a JSON integer token (`-0` reads `0`,
|
|
72
113
|
* `1.0` / `1e3` are not tokens, R7 — the exact text the
|
|
@@ -78,8 +119,8 @@ export function primitiveConverterOf(descriptor, readOptions, errorTypeName = de
|
|
|
78
119
|
* Anything else throws System.Text.Json's conversion error (FR-052),
|
|
79
120
|
* a `JsonNonIntegerNumber` (`1.0`, `1e3` read from text) included.
|
|
80
121
|
* - The descriptor's `create` — a `[Branded]` type's companion factory —
|
|
81
|
-
* builds the read value, as the branded converter does
|
|
82
|
-
*
|
|
122
|
+
* builds the read value, as the branded converter does (added by the
|
|
123
|
+
* review of slice 012e, #450).
|
|
83
124
|
*/
|
|
84
125
|
export function int64ConverterOf(descriptor, readOptions, errorTypeName = declaredTypeName) {
|
|
85
126
|
const { numberType, create } = descriptor;
|
|
@@ -92,10 +133,7 @@ export function int64ConverterOf(descriptor, readOptions, errorTypeName = declar
|
|
|
92
133
|
return isWithinIntegerRange(value, numberType) ? value : undefined;
|
|
93
134
|
}
|
|
94
135
|
if (typeof value === "number" && Number.isSafeInteger(value)) {
|
|
95
|
-
// `-0`
|
|
96
|
-
// ulong takes no sign (measured: STJ throws for `-0`).
|
|
97
|
-
if (Object.is(value, -0) && numberType === "System.UInt64")
|
|
98
|
-
return undefined;
|
|
136
|
+
// `-0` writes `0`, in a ulong slot too: `BigInt(-0)` is `0n` (#545).
|
|
99
137
|
const read = BigInt(value);
|
|
100
138
|
return isWithinIntegerRange(read, numberType) ? read : undefined;
|
|
101
139
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { Int64Descriptor, NumericEnumDescriptor } from "./types.ts";
|
|
1
|
+
import type { Int64Descriptor, IntegerTypeName, NumericEnumDescriptor } from "./types.ts";
|
|
2
|
+
/** The underlying type of a numeric enum whose descriptor names none, the C# default. */
|
|
3
|
+
export declare const DEFAULT_ENUM_INTEGER_TYPE: IntegerTypeName;
|
|
2
4
|
/** A numeric enum member's value: a `number`, or a `bigint` for a 64-bit enum (#701). */
|
|
3
5
|
export type NumericEnumValue = number | bigint;
|
|
4
6
|
/** A numeric enum descriptor of a 64-bit enum, which names its underlying type (#701). */
|
|
@@ -7,8 +9,6 @@ export type Int64EnumDescriptor = NumericEnumDescriptor & {
|
|
|
7
9
|
};
|
|
8
10
|
/** Whether a numeric enum's values are `bigint`s: its underlying type is 64-bit (#701). */
|
|
9
11
|
export declare function holdsBigInts(descriptor: NumericEnumDescriptor): descriptor is Int64EnumDescriptor;
|
|
10
|
-
/** Whether `value` is of a numeric enum's representation, a `number` or a `bigint`. */
|
|
11
|
-
export declare function isNumericEnumValue(value: unknown): value is NumericEnumValue;
|
|
12
12
|
/**
|
|
13
13
|
* The declared members of a numeric enum, as `[name, value]` pairs: the
|
|
14
14
|
* entries of its `as const` object whose value is a number of the enum's
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
+
/** The underlying type of a numeric enum whose descriptor names none, the C# default. */
|
|
2
|
+
export const DEFAULT_ENUM_INTEGER_TYPE = "System.Int32";
|
|
1
3
|
/** Whether a numeric enum's values are `bigint`s: its underlying type is 64-bit (#701). */
|
|
2
4
|
export function holdsBigInts(descriptor) {
|
|
3
5
|
return descriptor.numberType === "System.Int64" || descriptor.numberType === "System.UInt64";
|
|
4
6
|
}
|
|
5
|
-
/** Whether `value` is of a numeric enum's representation, a `number` or a `bigint`. */
|
|
6
|
-
export function isNumericEnumValue(value) {
|
|
7
|
-
return typeof value === "number" || typeof value === "bigint";
|
|
8
|
-
}
|
|
9
7
|
/**
|
|
10
8
|
* The declared members of a numeric enum, as `[name, value]` pairs: the
|
|
11
9
|
* entries of its `as const` object whose value is a number of the enum's
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The spec plans (#456, split out by #462): each contract's wire properties
|
|
3
|
+
* and converters compiled once per set of custom converters and kind of tree
|
|
4
|
+
* numbers, and the `serializeWithSpec` / `deserializeWithSpec` /
|
|
5
|
+
* `*WithValueSpec` entry points that run them. Converters receive the plans
|
|
6
|
+
* as their context (`converters.ts` imports only their type), so the two
|
|
7
|
+
* modules import each other in one direction at run time.
|
|
8
|
+
*/
|
|
9
|
+
import type { TypeSpec, PropertySpec, JsonConverter, TypeDiscriminator, ValueSpec } from "./types.ts";
|
|
10
|
+
import type { ValueConverter } from "./value-converter.ts";
|
|
11
|
+
/**
|
|
12
|
+
* Collects all properties from a spec chain, walking the base hierarchy.
|
|
13
|
+
* Base properties come first, own properties last: the declaration order a
|
|
14
|
+
* factory reads, **not** the wire order (see `collectWireProperties`). An
|
|
15
|
+
* override appears once per spec that declares it.
|
|
16
|
+
*/
|
|
17
|
+
export declare function collectProperties(spec: TypeSpec): readonly PropertySpec[];
|
|
18
|
+
/**
|
|
19
|
+
* The properties a spec writes and reads, in System.Text.Json's wire order
|
|
20
|
+
* (FR-009): the spec's own properties, then its base's, recursively. A JSON
|
|
21
|
+
* name appears once, from its most-derived declaration, and a base
|
|
22
|
+
* declaration whose `ts` name a more-derived spec lists in
|
|
23
|
+
* `ignoredProperties` (a `[JsonIgnore]` override) is left out. When any
|
|
24
|
+
* property has an `order` (`[JsonPropertyOrder]`), the list is then sorted by
|
|
25
|
+
* it, stably, across the whole chain (measured: `{"Der2":20,"Base3":3,"Der1":10,
|
|
26
|
+
* "Base1":1,"Der3":30,"Base2":2}` for orders -5, -5, 0, 0, 1, 5).
|
|
27
|
+
*/
|
|
28
|
+
export declare function collectWireProperties(spec: TypeSpec): readonly PropertySpec[];
|
|
29
|
+
/**
|
|
30
|
+
* Everything the reads and writes of one spec share, compiled on its first
|
|
31
|
+
* use (#456): the wire properties (`collectWireProperties`, with ignored
|
|
32
|
+
* overrides), their converters resolved under the spec's read options
|
|
33
|
+
* (numbers from strings included), and the case-insensitive key index. A
|
|
34
|
+
* polymorphic base dispatches per value through its own cached dispatch
|
|
35
|
+
* index (`polymorphism.ts`), then runs the chosen contract's plan.
|
|
36
|
+
*
|
|
37
|
+
* Writes use the converters resolved for reads: read options never change
|
|
38
|
+
* how a value is written (FR-048 item 5).
|
|
39
|
+
*/
|
|
40
|
+
export interface SpecPlan {
|
|
41
|
+
/** Writes through the spec, dispatching a polymorphic base (FR-005, FR-006). */
|
|
42
|
+
readonly serialize: (value: unknown) => Record<string, unknown>;
|
|
43
|
+
/** Reads through the spec, dispatching a polymorphic base (FR-007). */
|
|
44
|
+
readonly deserialize: (data: unknown) => unknown;
|
|
45
|
+
/** Writes `value` with exactly the spec's properties, after the tag when one is given. */
|
|
46
|
+
readonly writeContract: (value: unknown, typeMetadata?: TypeMetadata) => Record<string, unknown>;
|
|
47
|
+
/**
|
|
48
|
+
* Reads `raw` with exactly the spec's properties and builds it with the
|
|
49
|
+
* spec's factory. `metadataKey` is the discriminator a polymorphic read
|
|
50
|
+
* consumed.
|
|
51
|
+
*/
|
|
52
|
+
readonly readContract: (raw: Record<string, unknown>, metadataKey?: string) => unknown;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* How the tree being read holds its numbers: `parsed`, each a `number`, as
|
|
56
|
+
* `JSON.parse` builds it; `exact`, some as leaves, as `readJsonText` builds
|
|
57
|
+
* it (#457): a `JsonNumberText` for those with more significant digits than
|
|
58
|
+
* a double holds and, for a spec that reads a 64-bit integer, a
|
|
59
|
+
* `JsonNonIntegerNumber` for the other ones with a fraction or an exponent
|
|
60
|
+
* (`NumberTextLeaves`).
|
|
61
|
+
*/
|
|
62
|
+
export type TreeNumbers = "parsed" | "exact";
|
|
63
|
+
/**
|
|
64
|
+
* The plans compiled under one set of custom converters for one kind of
|
|
65
|
+
* tree numbers, each built once.
|
|
66
|
+
*/
|
|
67
|
+
export interface SpecPlans {
|
|
68
|
+
readonly customConverters: readonly JsonConverter[];
|
|
69
|
+
readonly treeNumbers: TreeNumbers;
|
|
70
|
+
planOf(spec: TypeSpec): SpecPlan;
|
|
71
|
+
valueConverterOf(spec: ValueSpec): ValueConverter;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Serialize a TS object to a JSON-safe plain object using the given spec. A
|
|
75
|
+
* polymorphic spec first chooses the derived contract and writes its tag
|
|
76
|
+
* before every property (FR-005); the chosen contract is never dispatched
|
|
77
|
+
* again (FR-006).
|
|
78
|
+
*/
|
|
79
|
+
export declare function serializeWithSpec(value: unknown, spec: TypeSpec, customConverters?: readonly JsonConverter[]): Record<string, unknown>;
|
|
80
|
+
/** The type discriminator a polymorphic write puts first. */
|
|
81
|
+
interface TypeMetadata {
|
|
82
|
+
readonly name: string;
|
|
83
|
+
readonly tag: TypeDiscriminator;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Deserialize unknown data (from JSON.parse) into a typed instance using the
|
|
87
|
+
* spec. A polymorphic spec reads the derived contract its tag selects
|
|
88
|
+
* (FR-007). `treeNumbers` says how `data` holds its numbers; a root number
|
|
89
|
+
* leaf reads as the number it is, which no contract accepts.
|
|
90
|
+
*/
|
|
91
|
+
export declare function deserializeWithSpec<T>(data: unknown, spec: TypeSpec<T>, customConverters?: readonly JsonConverter[], treeNumbers?: TreeNumbers): T;
|
|
92
|
+
/**
|
|
93
|
+
* Serializes a non-object root through its descriptor, exactly as a property
|
|
94
|
+
* of that type would be (custom converters still match by kind).
|
|
95
|
+
*/
|
|
96
|
+
export declare function serializeWithValueSpec(value: unknown, spec: ValueSpec, customConverters?: readonly JsonConverter[]): unknown;
|
|
97
|
+
/**
|
|
98
|
+
* Deserializes a non-object root through its descriptor, under the spec's
|
|
99
|
+
* read options. `treeNumbers` says how `data` holds its numbers.
|
|
100
|
+
*/
|
|
101
|
+
export declare function deserializeWithValueSpec(data: unknown, spec: ValueSpec, customConverters?: readonly JsonConverter[], treeNumbers?: TreeNumbers): unknown;
|
|
102
|
+
export {};
|