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.
Files changed (211) hide show
  1. package/dist/system/char-members.d.ts +51 -0
  2. package/dist/system/char-members.js +108 -0
  3. package/dist/system/collections/array-helpers.d.ts +28 -0
  4. package/dist/system/collections/array-helpers.js +105 -0
  5. package/dist/system/collections/hash-core.d.ts +5 -0
  6. package/dist/system/collections/hash-core.js +24 -0
  7. package/dist/system/collections/helpers.d.ts +16 -3
  8. package/dist/system/collections/helpers.js +28 -2
  9. package/dist/system/collections/immutable-list-builder-core.d.ts +7 -2
  10. package/dist/system/collections/immutable-list-builder-core.js +11 -0
  11. package/dist/system/collections/immutable-list-core.d.ts +8 -0
  12. package/dist/system/collections/immutable-list-core.js +19 -1
  13. package/dist/system/collections/immutable-storage.d.ts +2 -0
  14. package/dist/system/collections/immutable-storage.js +6 -1
  15. package/dist/system/collections/index.d.ts +2 -0
  16. package/dist/system/collections/index.js +2 -0
  17. package/dist/system/collections/interfaces.d.ts +13 -0
  18. package/dist/system/collections/list.d.ts +3 -0
  19. package/dist/system/collections/list.js +8 -11
  20. package/dist/system/collections/read-only-list.d.ts +11 -0
  21. package/dist/system/collections/read-only-list.js +26 -1
  22. package/dist/system/collections/sorted-list.d.ts +5 -0
  23. package/dist/system/collections/sorted-list.js +12 -1
  24. package/dist/system/collections/string-comparer.d.ts +14 -0
  25. package/dist/system/collections/string-comparer.js +45 -0
  26. package/dist/system/decimal-parsing.d.ts +12 -0
  27. package/dist/system/decimal-parsing.js +86 -0
  28. package/dist/system/exceptions/index.d.ts +1 -0
  29. package/dist/system/exceptions/index.js +1 -0
  30. package/dist/system/exceptions/throw-error.d.ts +7 -0
  31. package/dist/system/exceptions/throw-error.js +9 -0
  32. package/dist/system/index.d.ts +5 -0
  33. package/dist/system/index.js +5 -0
  34. package/dist/system/int64.d.ts +6 -0
  35. package/dist/system/int64.js +18 -9
  36. package/dist/system/integer-division.d.ts +12 -4
  37. package/dist/system/integer-division.js +19 -2
  38. package/dist/system/json/contract-name.d.ts +2 -1
  39. package/dist/system/json/contract-name.js +3 -2
  40. package/dist/system/json/converters.d.ts +15 -52
  41. package/dist/system/json/converters.js +33 -317
  42. package/dist/system/json/dictionary-keys.js +1 -3
  43. package/dist/system/json/index.d.ts +1 -1
  44. package/dist/system/json/index.js +1 -1
  45. package/dist/system/json/json-serializer.d.ts +12 -1
  46. package/dist/system/json/json-serializer.js +29 -1
  47. package/dist/system/json/json-text-writer.d.ts +17 -0
  48. package/dist/system/json/json-text-writer.js +175 -0
  49. package/dist/system/json/null-reads.d.ts +2 -1
  50. package/dist/system/json/null-reads.js +3 -1
  51. package/dist/system/json/number-from-string.d.ts +2 -0
  52. package/dist/system/json/number-from-string.js +24 -6
  53. package/dist/system/json/numeric-converters.d.ts +4 -4
  54. package/dist/system/json/numeric-converters.js +46 -8
  55. package/dist/system/json/numeric-enum-members.d.ts +3 -3
  56. package/dist/system/json/numeric-enum-members.js +2 -4
  57. package/dist/system/json/spec-plans.d.ts +102 -0
  58. package/dist/system/json/spec-plans.js +312 -0
  59. package/dist/system/json/types.d.ts +15 -9
  60. package/dist/system/lazy.d.ts +15 -0
  61. package/dist/system/lazy.js +43 -0
  62. package/dist/system/linq/grouping.d.ts +12 -0
  63. package/dist/system/linq/grouping.js +42 -0
  64. package/dist/system/linq/index.d.ts +4 -3
  65. package/dist/system/linq/index.js +3 -2
  66. package/dist/system/linq/linq.js +20 -1
  67. package/dist/system/linq/lookup.d.ts +17 -0
  68. package/dist/system/linq/lookup.js +20 -0
  69. package/dist/system/linq/operators.d.ts +35 -5
  70. package/dist/system/linq/operators.js +82 -31
  71. package/dist/system/linq/terminals.d.ts +41 -2
  72. package/dist/system/linq/terminals.js +122 -2
  73. package/dist/system/linq/types.d.ts +48 -4
  74. package/dist/system/math.d.ts +7 -0
  75. package/dist/system/math.js +14 -0
  76. package/dist/system/number-members.d.ts +10 -0
  77. package/dist/system/number-members.js +25 -0
  78. package/dist/system/number-parsing.d.ts +16 -4
  79. package/dist/system/number-parsing.js +21 -4
  80. package/dist/system/string-builder.d.ts +30 -0
  81. package/dist/system/string-builder.js +86 -0
  82. package/dist/system/task-completion-source.d.ts +37 -0
  83. package/dist/system/task-completion-source.js +81 -0
  84. package/dist-unchecked/system/char-members.d.ts +51 -0
  85. package/dist-unchecked/system/char-members.js +108 -0
  86. package/dist-unchecked/system/collections/array-helpers.d.ts +28 -0
  87. package/dist-unchecked/system/collections/array-helpers.js +105 -0
  88. package/dist-unchecked/system/collections/hash-core.d.ts +5 -0
  89. package/dist-unchecked/system/collections/hash-core.js +24 -0
  90. package/dist-unchecked/system/collections/helpers.d.ts +16 -3
  91. package/dist-unchecked/system/collections/helpers.js +28 -2
  92. package/dist-unchecked/system/collections/immutable-list-builder-core.d.ts +7 -2
  93. package/dist-unchecked/system/collections/immutable-list-builder-core.js +11 -0
  94. package/dist-unchecked/system/collections/immutable-list-core.d.ts +8 -0
  95. package/dist-unchecked/system/collections/immutable-list-core.js +19 -1
  96. package/dist-unchecked/system/collections/immutable-storage.d.ts +2 -0
  97. package/dist-unchecked/system/collections/immutable-storage.js +6 -1
  98. package/dist-unchecked/system/collections/index.d.ts +2 -0
  99. package/dist-unchecked/system/collections/index.js +2 -0
  100. package/dist-unchecked/system/collections/interfaces.d.ts +13 -0
  101. package/dist-unchecked/system/collections/list.d.ts +3 -0
  102. package/dist-unchecked/system/collections/list.js +8 -11
  103. package/dist-unchecked/system/collections/read-only-list.d.ts +11 -0
  104. package/dist-unchecked/system/collections/read-only-list.js +26 -1
  105. package/dist-unchecked/system/collections/sorted-list.d.ts +5 -0
  106. package/dist-unchecked/system/collections/sorted-list.js +12 -1
  107. package/dist-unchecked/system/collections/string-comparer.d.ts +14 -0
  108. package/dist-unchecked/system/collections/string-comparer.js +45 -0
  109. package/dist-unchecked/system/decimal-parsing.d.ts +12 -0
  110. package/dist-unchecked/system/decimal-parsing.js +86 -0
  111. package/dist-unchecked/system/exceptions/index.d.ts +1 -0
  112. package/dist-unchecked/system/exceptions/index.js +1 -0
  113. package/dist-unchecked/system/exceptions/throw-error.d.ts +7 -0
  114. package/dist-unchecked/system/exceptions/throw-error.js +9 -0
  115. package/dist-unchecked/system/index.d.ts +5 -0
  116. package/dist-unchecked/system/index.js +5 -0
  117. package/dist-unchecked/system/int64.d.ts +6 -0
  118. package/dist-unchecked/system/int64.js +18 -9
  119. package/dist-unchecked/system/integer-division.d.ts +12 -4
  120. package/dist-unchecked/system/integer-division.js +19 -2
  121. package/dist-unchecked/system/json/contract-name.d.ts +2 -1
  122. package/dist-unchecked/system/json/contract-name.js +3 -2
  123. package/dist-unchecked/system/json/converters.d.ts +15 -52
  124. package/dist-unchecked/system/json/converters.js +33 -317
  125. package/dist-unchecked/system/json/dictionary-keys.js +1 -3
  126. package/dist-unchecked/system/json/index.d.ts +1 -1
  127. package/dist-unchecked/system/json/index.js +1 -1
  128. package/dist-unchecked/system/json/json-serializer.d.ts +12 -1
  129. package/dist-unchecked/system/json/json-serializer.js +29 -1
  130. package/dist-unchecked/system/json/json-text-writer.d.ts +17 -0
  131. package/dist-unchecked/system/json/json-text-writer.js +175 -0
  132. package/dist-unchecked/system/json/null-reads.d.ts +2 -1
  133. package/dist-unchecked/system/json/null-reads.js +3 -1
  134. package/dist-unchecked/system/json/number-from-string.d.ts +2 -0
  135. package/dist-unchecked/system/json/number-from-string.js +24 -6
  136. package/dist-unchecked/system/json/numeric-converters.d.ts +4 -4
  137. package/dist-unchecked/system/json/numeric-converters.js +46 -8
  138. package/dist-unchecked/system/json/numeric-enum-members.d.ts +3 -3
  139. package/dist-unchecked/system/json/numeric-enum-members.js +2 -4
  140. package/dist-unchecked/system/json/spec-plans.d.ts +102 -0
  141. package/dist-unchecked/system/json/spec-plans.js +312 -0
  142. package/dist-unchecked/system/json/types.d.ts +15 -9
  143. package/dist-unchecked/system/lazy.d.ts +15 -0
  144. package/dist-unchecked/system/lazy.js +43 -0
  145. package/dist-unchecked/system/linq/grouping.d.ts +12 -0
  146. package/dist-unchecked/system/linq/grouping.js +42 -0
  147. package/dist-unchecked/system/linq/index.d.ts +4 -3
  148. package/dist-unchecked/system/linq/index.js +3 -2
  149. package/dist-unchecked/system/linq/linq.js +20 -1
  150. package/dist-unchecked/system/linq/lookup.d.ts +17 -0
  151. package/dist-unchecked/system/linq/lookup.js +20 -0
  152. package/dist-unchecked/system/linq/operators.d.ts +35 -5
  153. package/dist-unchecked/system/linq/operators.js +82 -31
  154. package/dist-unchecked/system/linq/terminals.d.ts +41 -2
  155. package/dist-unchecked/system/linq/terminals.js +122 -2
  156. package/dist-unchecked/system/linq/types.d.ts +48 -4
  157. package/dist-unchecked/system/math.d.ts +7 -0
  158. package/dist-unchecked/system/math.js +14 -0
  159. package/dist-unchecked/system/number-members.d.ts +10 -0
  160. package/dist-unchecked/system/number-members.js +25 -0
  161. package/dist-unchecked/system/number-parsing.d.ts +16 -4
  162. package/dist-unchecked/system/number-parsing.js +21 -4
  163. package/dist-unchecked/system/string-builder.d.ts +30 -0
  164. package/dist-unchecked/system/string-builder.js +86 -0
  165. package/dist-unchecked/system/task-completion-source.d.ts +37 -0
  166. package/dist-unchecked/system/task-completion-source.js +81 -0
  167. package/package.json +1 -1
  168. package/src/system/char-members.ts +131 -0
  169. package/src/system/collections/array-helpers.ts +139 -0
  170. package/src/system/collections/hash-core.ts +31 -0
  171. package/src/system/collections/helpers.ts +39 -4
  172. package/src/system/collections/immutable-list-builder-core.ts +16 -2
  173. package/src/system/collections/immutable-list-core.ts +23 -1
  174. package/src/system/collections/immutable-storage.ts +8 -1
  175. package/src/system/collections/index.ts +2 -0
  176. package/src/system/collections/interfaces.ts +16 -0
  177. package/src/system/collections/list.ts +10 -15
  178. package/src/system/collections/read-only-list.ts +35 -1
  179. package/src/system/collections/sorted-list.ts +15 -1
  180. package/src/system/collections/string-comparer.ts +57 -0
  181. package/src/system/decimal-parsing.ts +109 -0
  182. package/src/system/exceptions/index.ts +1 -0
  183. package/src/system/exceptions/throw-error.ts +9 -0
  184. package/src/system/index.ts +5 -0
  185. package/src/system/int64.ts +27 -10
  186. package/src/system/integer-division.ts +43 -6
  187. package/src/system/json/contract-name.ts +3 -2
  188. package/src/system/json/converters.ts +38 -526
  189. package/src/system/json/dictionary-keys.ts +6 -4
  190. package/src/system/json/index.ts +1 -1
  191. package/src/system/json/json-serializer.ts +42 -1
  192. package/src/system/json/json-text-writer.ts +224 -0
  193. package/src/system/json/null-reads.ts +3 -1
  194. package/src/system/json/number-from-string.ts +31 -6
  195. package/src/system/json/numeric-converters.ts +60 -8
  196. package/src/system/json/numeric-enum-members.ts +4 -6
  197. package/src/system/json/spec-plans.ts +524 -0
  198. package/src/system/json/types.ts +15 -9
  199. package/src/system/lazy.ts +62 -0
  200. package/src/system/linq/grouping.ts +77 -0
  201. package/src/system/linq/index.ts +14 -0
  202. package/src/system/linq/linq.ts +23 -1
  203. package/src/system/linq/lookup.ts +34 -0
  204. package/src/system/linq/operators.ts +109 -17
  205. package/src/system/linq/terminals.ts +161 -2
  206. package/src/system/linq/types.ts +66 -3
  207. package/src/system/math.ts +19 -0
  208. package/src/system/number-members.ts +29 -0
  209. package/src/system/number-parsing.ts +24 -4
  210. package/src/system/string-builder.ts +117 -0
  211. package/src/system/task-completion-source.ts +106 -0
@@ -6,11 +6,13 @@ import {
6
6
  serializeWithValueSpec,
7
7
  deserializeWithValueSpec,
8
8
  type TreeNumbers,
9
- } from "./converters.ts";
9
+ } from "./spec-plans.ts";
10
10
  import { specNumberTextLeaves } from "./exact-number-reads.ts";
11
11
  import { mayHoldNumberTextLeaf, parseJsonText, readJsonText } from "./json-text-reader.ts";
12
12
  import { readNull } from "./null-reads.ts";
13
13
  import { isValueSpec } from "./spec-kind.ts";
14
+ import { writeJsonText } from "./json-text-writer.ts";
15
+ import { throwIfNull } from "../argument-guards.ts";
14
16
 
15
17
  /**
16
18
  * Serializes through either spec kind: an object contract walks its
@@ -74,6 +76,9 @@ function deserializeTextWithInfo<T>(
74
76
  spec: JsonTypeInfo<T>,
75
77
  converters?: readonly JsonConverter[],
76
78
  ): T | null {
79
+ throwIfNull(text, "json");
80
+ throwIfNull(spec, "jsonTypeInfo");
81
+
77
82
  const leaves = specNumberTextLeaves(spec);
78
83
  const readsExactTree = leaves !== undefined && mayHoldNumberTextLeaf(text, leaves);
79
84
 
@@ -84,6 +89,21 @@ function deserializeTextWithInfo<T>(
84
89
  return deserializeWithInfo(parseJsonText(text), spec, converters);
85
90
  }
86
91
 
92
+ /**
93
+ * Writes JSON text as System.Text.Json's `Serialize(value, JsonTypeInfo<T>)`
94
+ * does (#741): the tree `serialize` builds, written by `writeJsonText`
95
+ * (STJ's escaping and number text, not `JSON.stringify`'s).
96
+ */
97
+ function serializeTextWithInfo<T>(
98
+ value: T | null,
99
+ spec: JsonTypeInfo<T>,
100
+ converters?: readonly JsonConverter[],
101
+ ): string {
102
+ throwIfNull(spec, "jsonTypeInfo");
103
+
104
+ return writeJsonText(serializeWithInfo(value, spec, converters));
105
+ }
106
+
87
107
  /**
88
108
  * A bound serializer that uses a SerializerContext to resolve custom converters
89
109
  * and look up specs by constructor.
@@ -101,6 +121,11 @@ export class BoundSerializer {
101
121
  return serializeWithInfo(value, spec, this.context.converters);
102
122
  }
103
123
 
124
+ /** Serialize a value to JSON text, as System.Text.Json writes it. */
125
+ serializeText<T>(value: T | null, spec: JsonTypeInfo<T>): string {
126
+ return serializeTextWithInfo(value, spec, this.context.converters);
127
+ }
128
+
104
129
  /**
105
130
  * Deserialize a parsed JSON value into a typed instance; JSON `null` reads
106
131
  * as `null`, except for a struct contract (`valueType`), which throws
@@ -188,6 +213,22 @@ export class JsonSerializer {
188
213
  return serializeWithInfo(value, spec, converters);
189
214
  }
190
215
 
216
+ /**
217
+ * Serialize a value to JSON text using the given spec, as System.Text.Json's
218
+ * `Serialize(value, JsonTypeInfo<T>)` writes it: no whitespace, strings
219
+ * escaped by its default encoder (`<` is `\u003C`, `é` is `\u00E9`),
220
+ * numbers in .NET's round-trip text (`1E+20`), `decimal` and 64-bit
221
+ * integers with their exact digits, `null` as `null`. NaN and the
222
+ * infinities throw `ArgumentException`, as STJ throws for them.
223
+ */
224
+ static serializeText<T>(
225
+ value: T | null,
226
+ spec: JsonTypeInfo<T>,
227
+ converters?: readonly JsonConverter[],
228
+ ): string {
229
+ return serializeTextWithInfo(value, spec, converters);
230
+ }
231
+
191
232
  /**
192
233
  * Deserialize a parsed JSON value (`JSON.parse`'s tree) into a typed
193
234
  * instance using the given spec. `JSON.parse` has already rounded every
@@ -0,0 +1,224 @@
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
+
17
+ import { formatDouble } from "../double-format.ts";
18
+ import { ArgumentException } from "../exceptions/argument-exception.ts";
19
+ import { rawJsonTextOf } from "./exact-json-number.ts";
20
+
21
+ /** The message STJ throws for a NaN or infinite number written as JSON. */
22
+ const NON_FINITE_NUMBER_MESSAGE =
23
+ ".NET number values such as positive and negative infinity cannot be written as valid JSON. " +
24
+ "To make it work when using 'JsonSerializer', consider specifying " +
25
+ "'JsonNumberHandling.AllowNamedFloatingPointLiterals' " +
26
+ "(see https://learn.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling).";
27
+
28
+ /** The JSON text of `tree`, as System.Text.Json writes it. */
29
+ export function writeJsonText(tree: unknown): string {
30
+ const parts: string[] = [];
31
+
32
+ writeValue(tree, parts);
33
+
34
+ return parts.join("");
35
+ }
36
+
37
+ function writeValue(value: unknown, parts: string[]): void {
38
+ if (value == null) {
39
+ parts.push("null");
40
+
41
+ return;
42
+ }
43
+
44
+ switch (typeof value) {
45
+ case "string":
46
+ parts.push(quoted(value));
47
+
48
+ return;
49
+ case "number":
50
+ parts.push(numberText(value));
51
+
52
+ return;
53
+ case "bigint":
54
+ case "boolean":
55
+ parts.push(String(value));
56
+
57
+ return;
58
+ }
59
+
60
+ writeObject(value as object, parts);
61
+ }
62
+
63
+ function writeObject(value: object, parts: string[]): void {
64
+ const rawText = rawJsonTextOf(value);
65
+
66
+ if (rawText !== undefined) {
67
+ parts.push(rawText);
68
+
69
+ return;
70
+ }
71
+
72
+ const toJson = (value as { toJSON?: unknown }).toJSON;
73
+
74
+ if (typeof toJson === "function") {
75
+ writeValue(toJson.call(value), parts);
76
+
77
+ return;
78
+ }
79
+
80
+ if (Array.isArray(value)) {
81
+ writeArray(value, parts);
82
+
83
+ return;
84
+ }
85
+
86
+ writeProperties(value as Record<string, unknown>, parts);
87
+ }
88
+
89
+ function writeArray(items: readonly unknown[], parts: string[]): void {
90
+ parts.push("[");
91
+
92
+ items.forEach((item, index) => {
93
+ if (index > 0) {
94
+ parts.push(",");
95
+ }
96
+
97
+ writeValue(item, parts);
98
+ });
99
+
100
+ parts.push("]");
101
+ }
102
+
103
+ /** An object's own enumerable properties; an `undefined` one is skipped, as `JSON.stringify` skips it. */
104
+ function writeProperties(object: Record<string, unknown>, parts: string[]): void {
105
+ let first = true;
106
+
107
+ parts.push("{");
108
+
109
+ for (const key of Object.keys(object)) {
110
+ const member = object[key];
111
+
112
+ if (member === undefined) {
113
+ continue;
114
+ }
115
+
116
+ if (!first) {
117
+ parts.push(",");
118
+ }
119
+
120
+ first = false;
121
+ parts.push(quoted(key), ":");
122
+ writeValue(member, parts);
123
+ }
124
+
125
+ parts.push("}");
126
+ }
127
+
128
+ function numberText(value: number): string {
129
+ if (!Number.isFinite(value)) {
130
+ throw new ArgumentException(NON_FINITE_NUMBER_MESSAGE);
131
+ }
132
+
133
+ return formatDouble(value);
134
+ }
135
+
136
+ function quoted(text: string): string {
137
+ for (let index = 0; index < text.length; index++) {
138
+ if (isEscaped(text.charCodeAt(index))) {
139
+ return `"${escaped(text)}"`;
140
+ }
141
+ }
142
+
143
+ return `"${text}"`;
144
+ }
145
+
146
+ /** Whether `JavaScriptEncoder.Default` escapes the UTF-16 code unit `code`. */
147
+ function isEscaped(code: number): boolean {
148
+ switch (code) {
149
+ case 0x22: // "
150
+ case 0x26: // &
151
+ case 0x27: // '
152
+ case 0x2b: // +
153
+ case 0x3c: // <
154
+ case 0x3e: // >
155
+ case 0x5c: // backslash
156
+ case 0x60: // `
157
+ return true;
158
+ default:
159
+ return code < 0x20 || code >= 0x7f;
160
+ }
161
+ }
162
+
163
+ /** The short escape STJ keeps for `code`, or `undefined`. */
164
+ function shortEscapeOf(code: number): string | undefined {
165
+ switch (code) {
166
+ case 0x08:
167
+ return "\\b";
168
+ case 0x09:
169
+ return "\\t";
170
+ case 0x0a:
171
+ return "\\n";
172
+ case 0x0c:
173
+ return "\\f";
174
+ case 0x0d:
175
+ return "\\r";
176
+ case 0x5c:
177
+ return "\\\\";
178
+ default:
179
+ return undefined;
180
+ }
181
+ }
182
+
183
+ function escaped(text: string): string {
184
+ let result = "";
185
+
186
+ for (let index = 0; index < text.length; index++) {
187
+ const code = text.charCodeAt(index);
188
+
189
+ if (isHighSurrogate(code) && isLowSurrogate(text.charCodeAt(index + 1))) {
190
+ result += unicodeEscape(code) + unicodeEscape(text.charCodeAt(index + 1));
191
+ index++;
192
+ } else {
193
+ result += escapedCodeUnit(code);
194
+ }
195
+ }
196
+
197
+ return result;
198
+ }
199
+
200
+ function escapedCodeUnit(code: number): string {
201
+ const shortEscape = shortEscapeOf(code);
202
+
203
+ if (shortEscape !== undefined) {
204
+ return shortEscape;
205
+ }
206
+
207
+ if (isHighSurrogate(code) || isLowSurrogate(code)) {
208
+ return unicodeEscape(0xfffd);
209
+ }
210
+
211
+ return isEscaped(code) ? unicodeEscape(code) : String.fromCharCode(code);
212
+ }
213
+
214
+ function unicodeEscape(code: number): string {
215
+ return `\\u${code.toString(16).toUpperCase().padStart(4, "0")}`;
216
+ }
217
+
218
+ function isHighSurrogate(code: number): boolean {
219
+ return code >= 0xd800 && code <= 0xdbff;
220
+ }
221
+
222
+ function isLowSurrogate(code: number): boolean {
223
+ return code >= 0xdc00 && code <= 0xdfff;
224
+ }
@@ -24,7 +24,8 @@ export function readNull(spec: JsonTypeInfo): null {
24
24
  * `JsonException`, or `undefined` when the descriptor reads `null` (#471,
25
25
  * measured on .NET 10): a `string` primitive, containers, `byte[]`, a
26
26
  * `nullable` and a `ref` read `null`; a numeric or `bool`/`char` primitive,
27
- * an enum, a branded value, a temporal value and a `decimal` reject it,
27
+ * an enum, a branded value, a temporal value, a `decimal` and an
28
+ * `ImmutableArray<T>` (the one value-type collection, #713) reject it,
28
29
  * naming the type the descriptor carries (the element type, wherever the
29
30
  * value sits; the declaring contract names itself at the property).
30
31
  */
@@ -40,6 +41,7 @@ export function valueTypeNameOf(descriptor: TypeDescriptor): string | undefined
40
41
  case "enum":
41
42
  case "numericEnum":
42
43
  case "branded":
44
+ case "immutableArray":
43
45
  return descriptor.typeName;
44
46
  default:
45
47
  return undefined;
@@ -122,10 +122,13 @@ function isWithinRange(value: bigint, min: bigint, max: bigint): boolean {
122
122
 
123
123
  /**
124
124
  * Reads the floating-point grammar; `staysFinite` rejects a value that
125
- * overflows the target type. The value keeps double precision, as a JSON
126
- * number read into a `Single` does.
125
+ * overflows the target type, and `round` brings the double to the target's
126
+ * precision (a `Single` rounds to single precision, #695).
127
127
  */
128
- function floatingPointReader(staysFinite: FiniteCheck): TextReader<number> {
128
+ function floatingPointReader(
129
+ staysFinite: FiniteCheck,
130
+ round: (value: number) => number,
131
+ ): TextReader<number> {
129
132
  return (text) => {
130
133
  const literal = FLOATING_POINT_LITERALS.get(text);
131
134
 
@@ -141,7 +144,7 @@ function floatingPointReader(staysFinite: FiniteCheck): TextReader<number> {
141
144
 
142
145
  const value = Number(text);
143
146
 
144
- return staysFinite(parts, value) ? value : undefined;
147
+ return staysFinite(parts, value) ? round(value) : undefined;
145
148
  };
146
149
  }
147
150
 
@@ -149,6 +152,23 @@ function fitsDouble(_: RegExpExecArray, value: number): boolean {
149
152
  return Number.isFinite(value);
150
153
  }
151
154
 
155
+ /** The largest `Single`, `(2 − 2^-23) × 2^127`. */
156
+ const SINGLE_MAX_VALUE = 3.4028234663852886e38;
157
+
158
+ /**
159
+ * A finite double that {@link fitsSingle} accepted, rounded to single
160
+ * precision (#695). Only the threshold double itself would round to an
161
+ * infinity (a tie to even); its text was below the threshold, so it is
162
+ * `±Single.MaxValue`.
163
+ */
164
+ function roundToFiniteSingle(value: number): number {
165
+ const single = Math.fround(value);
166
+
167
+ return Number.isFinite(single) || !Number.isFinite(value)
168
+ ? single
169
+ : Math.sign(single) * SINGLE_MAX_VALUE;
170
+ }
171
+
152
172
  /**
153
173
  * Whether the text stays below the `Single` overflow threshold. The double
154
174
  * is already rounded, so it decides every case but one: a double equal to
@@ -193,6 +213,11 @@ const UNSIGNED_INTEGER_TYPES: ReadonlySet<IntegerTypeName> = /* @__PURE__ */ new
193
213
  "System.UInt64",
194
214
  ]);
195
215
 
216
+ /** Whether `numberType` is an unsigned integer type, which takes no sign. */
217
+ export function isUnsignedIntegerType(numberType: IntegerTypeName): boolean {
218
+ return UNSIGNED_INTEGER_TYPES.has(numberType);
219
+ }
220
+
196
221
  /** The integers that read exactly, as the `bigint` they compute (spec 012 FR-054). */
197
222
  const EXACT_INTEGER_TYPES: ReadonlySet<IntegerTypeName> = /* @__PURE__ */ new Set([
198
223
  "System.Int64",
@@ -267,8 +292,8 @@ const INTEGER_TOKEN_READERS = /* @__PURE__ */ lazyValue(
267
292
  const NUMBER_TEXT_READERS = /* @__PURE__ */ lazyValue(
268
293
  (): Readonly<Record<NumberTypeName, TextReader<number | bigint>>> => ({
269
294
  ...STRICT_INTEGER_READERS(),
270
- "System.Single": floatingPointReader(fitsSingle),
271
- "System.Double": floatingPointReader(fitsDouble),
295
+ "System.Single": floatingPointReader(fitsSingle, roundToFiniteSingle),
296
+ "System.Double": floatingPointReader(fitsDouble, (value) => value),
272
297
  }),
273
298
  );
274
299
 
@@ -5,6 +5,7 @@ import type {
5
5
  PrimitiveDescriptor,
6
6
  } from "./types.ts";
7
7
  import type { ValueConverter } from "./value-converter.ts";
8
+ import { formatSingle } from "../double-format.ts";
8
9
  import { JsonException } from "./errors.ts";
9
10
  import { isDecimalText, readDecimalText } from "./decimal-text.ts";
10
11
  import {
@@ -61,6 +62,9 @@ const passthroughConverter: ValueConverter = {
61
62
  /** The CLR name of a C# `char`, which reads as a one-code-unit `string`. */
62
63
  const CHAR_TYPE_NAME = "System.Char";
63
64
 
65
+ /** The CLR name of a C# `float`, whose values read rounded to single precision. */
66
+ const SINGLE_TYPE_NAME = "System.Single";
67
+
64
68
  /**
65
69
  * A `primitive`: values pass through both ways, except that a JSON string
66
70
  * for a numeric `numberType` is read by System.Text.Json's grammar when the
@@ -79,6 +83,10 @@ export function primitiveConverterOf(
79
83
  return charConverterOf(errorTypeName);
80
84
  }
81
85
 
86
+ if (numberType === SINGLE_TYPE_NAME) {
87
+ return singleConverterOf(readOptions, errorTypeName);
88
+ }
89
+
82
90
  if (numberType === undefined || !readsNumbersFromStrings(readOptions)) {
83
91
  return passthroughConverter;
84
92
  }
@@ -101,6 +109,53 @@ export function primitiveConverterOf(
101
109
  };
102
110
  }
103
111
 
112
+ /**
113
+ * A `float`: a JSON number reads rounded to single precision, as
114
+ * System.Text.Json parses it into a `Single` (#695), and a string reads
115
+ * through the number grammar (which rounds too) when the context reads
116
+ * numbers from strings. A finite value writes the shortest text that reads
117
+ * back as the same `float`, as System.Text.Json writes it (`0.1`, not the
118
+ * double `0.10000000149011612`), through the exact-number leaf. A
119
+ * non-finite value passes through: System.Text.Json throws for it without
120
+ * `AllowNamedFloatingPointLiterals`, which Metano does not mirror.
121
+ */
122
+ function singleConverterOf(
123
+ readOptions: JsonReadOptions | undefined,
124
+ errorTypeName: ErrorTypeName,
125
+ ): ValueConverter {
126
+ const readsStrings = readsNumbersFromStrings(readOptions);
127
+
128
+ return {
129
+ serialize: (value: unknown) =>
130
+ typeof value === "number" && Number.isFinite(value)
131
+ ? writeExactNumber(formatSingle(value), () => Math.fround(value))
132
+ : passthroughConverter.serialize(value),
133
+ deserialize: (value: unknown) => {
134
+ if (typeof value === "number") {
135
+ return Math.fround(value);
136
+ }
137
+
138
+ const exact = exactNumberTextOf(value);
139
+
140
+ if (exact !== undefined) {
141
+ return Math.fround(Number(exact));
142
+ }
143
+
144
+ if (typeof value !== "string" || !readsStrings) {
145
+ return value;
146
+ }
147
+
148
+ const number = readNumberTextAsNumber(value, SINGLE_TYPE_NAME);
149
+
150
+ if (number === undefined) {
151
+ throw cannotConvert(SINGLE_TYPE_NAME, errorTypeName);
152
+ }
153
+
154
+ return number;
155
+ },
156
+ };
157
+ }
158
+
104
159
  /**
105
160
  * A `long` / `ulong` — the `int64` descriptor (spec 012 FR-050), whatever
106
161
  * the read options:
@@ -110,8 +165,8 @@ export function primitiveConverterOf(
110
165
  * range-checks: a safe-integer `number` from untyped code writes its
111
166
  * digits too, and any value outside the 64-bit range — a negative one for
112
167
  * a `ulong` — or of any other kind throws `JsonException` (FR-051). A
113
- * bigint has no negative zero (`-0n === 0n`), so a `-0` write puts `0`
114
- * into the slot.
168
+ * `-0` write, a `number` or a `bigint` (`-0n === 0n`), puts `0` into the
169
+ * slot, a ulong's included (#545); a `-0` read into a ulong still throws.
115
170
  * - Read as a `bigint` in range from: a `bigint`; a `rawJSON` leaf or
116
171
  * `JsonNumberText` whose text is a JSON integer token (`-0` reads `0`,
117
172
  * `1.0` / `1e3` are not tokens, R7 — the exact text the
@@ -123,8 +178,8 @@ export function primitiveConverterOf(
123
178
  * Anything else throws System.Text.Json's conversion error (FR-052),
124
179
  * a `JsonNonIntegerNumber` (`1.0`, `1e3` read from text) included.
125
180
  * - The descriptor's `create` — a `[Branded]` type's companion factory —
126
- * builds the read value, as the branded converter does. FR-050 does not
127
- * describe that factory case; the review of slice 012e (#450) added it.
181
+ * builds the read value, as the branded converter does (added by the
182
+ * review of slice 012e, #450).
128
183
  */
129
184
  export function int64ConverterOf(
130
185
  descriptor: Int64Descriptor,
@@ -144,10 +199,7 @@ export function int64ConverterOf(
144
199
  }
145
200
 
146
201
  if (typeof value === "number" && Number.isSafeInteger(value)) {
147
- // `-0` is a safe integer whose bigint is in a ulong's range, but a
148
- // ulong takes no sign (measured: STJ throws for `-0`).
149
- if (Object.is(value, -0) && numberType === "System.UInt64") return undefined;
150
-
202
+ // `-0` writes `0`, in a ulong slot too: `BigInt(-0)` is `0n` (#545).
151
203
  const read = BigInt(value);
152
204
 
153
205
  return isWithinIntegerRange(read, numberType) ? read : undefined;
@@ -1,4 +1,7 @@
1
- import type { Int64Descriptor, NumericEnumDescriptor } from "./types.ts";
1
+ import type { Int64Descriptor, IntegerTypeName, NumericEnumDescriptor } from "./types.ts";
2
+
3
+ /** The underlying type of a numeric enum whose descriptor names none, the C# default. */
4
+ export const DEFAULT_ENUM_INTEGER_TYPE: IntegerTypeName = "System.Int32";
2
5
 
3
6
  /** A numeric enum member's value: a `number`, or a `bigint` for a 64-bit enum (#701). */
4
7
  export type NumericEnumValue = number | bigint;
@@ -13,11 +16,6 @@ export function holdsBigInts(descriptor: NumericEnumDescriptor): descriptor is I
13
16
  return descriptor.numberType === "System.Int64" || descriptor.numberType === "System.UInt64";
14
17
  }
15
18
 
16
- /** Whether `value` is of a numeric enum's representation, a `number` or a `bigint`. */
17
- export function isNumericEnumValue(value: unknown): value is NumericEnumValue {
18
- return typeof value === "number" || typeof value === "bigint";
19
- }
20
-
21
19
  /**
22
20
  * The declared members of a numeric enum, as `[name, value]` pairs: the
23
21
  * entries of its `as const` object whose value is a number of the enum's