From 144390194cb7ea96c34e6eab16f76cbea1a51132 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 9 Nov 2026 19:35:56 +0000 Subject: [PATCH] JsonValue by key: value["name"] and value[0] JsonValue declares `[key: number | string]: JsonValue`, so `value["name"]` and `value[0]` read through get(), and `value["name"] = member` and `value[0] = element` write through set(). get and set take a number for an array and a string for an object; `get("x")` and `set("x", v)` work as before, and `at(i)` stays. Writing past an array's end leaves JSON nulls before the new element, as in JavaScript; a negative index is a RangeError. A key that names a JsonValue member (length, kind, keys, ...) is that member, as in TypeScript. Literal string keys need TypeScriptCompiler#799 and #800. Under -mm=own two writes by key to a JsonValue crash the compiler (a known issue), so tests/json_index.ts writes by key under the other models only, and the README says to use set() there. Co-Authored-By: Claude Opus 5.5 --- README.md | 12 ++++++++- src/lib.d.ts | 5 ++-- src/lib.ts | 59 ++++++++++++++++++++++++++++++++---------- tests/json_index.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 tests/json_index.ts diff --git a/README.md b/README.md index 3d72dc5..3b81476 100644 --- a/README.md +++ b/README.md @@ -151,9 +151,19 @@ Globals (`src/lib.ts`): ends at its first 0. A value is built with `JsonValue.ofBoolean`, `ofNumber`, `ofString`, `newArray` and `newObject`, then `push` and `set`; `clone()` makes a deep copy. + An object's members and an array's elements can also be reached by key: `value["name"]` is + `value.get("name")`, `value[0]` is `value.get(0)`, and `value["name"] = member` and + `value[0] = element` call `set`. `get` and `set` take a number for an array and a string for an + object. Writing past an array's end leaves JSON nulls before the new element, as in JavaScript; a + negative index is a `RangeError`. A key that names a member of `JsonValue` itself (`length`, `kind`, + `keys`, ...) is that member, as in TypeScript, so `get` and `set` stay the way to reach a JSON member + with such a name. Literal string keys need a compiler with TypeScriptCompiler#799 and #800 (for a + `number | string` key); a key in a variable, or a number, works with any. Under `-mm=own`, writes by + key crash the compiler for now: use `set` there. + ```ts const config = JSON.parse(`{"name":"tslang","sizes":[1,2]}`); - console.log(config.get("name").asString(), config.get("sizes").at(1).asNumber()); + console.log(config["name"].asString(), config["sizes"][1].asNumber()); ``` - `console` (static): `log`, `warn`, `error`, `assert`, and `readLine`, which returns the next line of standard input without its line break, or `null` at the end of input: diff --git a/src/lib.d.ts b/src/lib.d.ts index 8fe5702..84f5480 100644 --- a/src/lib.d.ts +++ b/src/lib.d.ts @@ -724,12 +724,13 @@ declare class JsonValue { @lends("this") @keeps("value") push(value: JsonValue): JsonValue; + [key: number | string]: JsonValue; has(key: string): boolean; @lends("this") - get(key: string): JsonValue; + get(key: number | string): JsonValue; @lends("this") @keeps("value") - set(key: string, value: JsonValue): JsonValue; + set(key: number | string, value: JsonValue): JsonValue; clone(): JsonValue; keys(): string[]; stringify(space?: string): string; diff --git a/src/lib.ts b/src/lib.ts index 829424c..e379bfb 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -2349,16 +2349,30 @@ export class JsonValue { return this; } + // value["name"] and value[0] read through get(), value["name"] = member and value[0] = element + // write through set() + [key: number | string]: JsonValue; + public has(key: string): boolean { this.expect(JSON_OBJECT, "object"); return this.indexOf(key) >= 0; } - // an object's member, or a JSON null when it has none + // An object's member (a string key) or an array's element (a number), or a JSON null when there + // is none. @lends("this") - public get(key: string): JsonValue { - this.expect(JSON_OBJECT, "object"); - const position = this.indexOf(key); + public get(key: number | string): JsonValue { + let position = -1; + if (typeof key == "number") { + this.expect(JSON_ARRAY, "array"); + if (key >= 0 && key < this.items.length) { + position = key; + } + } else { + this.expect(JSON_OBJECT, "object"); + position = this.indexOf(key); + } + if (position < 0) { return this.nothing(); } @@ -2366,17 +2380,36 @@ export class JsonValue { return this.items[position]; } - // A key already there keeps its place and takes the new value, as in JavaScript. + // An object's member (a string key) or an array's element (a number). A key already there keeps + // its place and takes the new value, and an element past an array's end leaves JSON nulls before + // it, as in JavaScript. @lends("this") @keeps("value") - public set(key: string, value: JsonValue): JsonValue { - this.expect(JSON_OBJECT, "object"); - let position = this.indexOf(key); - if (position < 0) { - position = this.items.length; - // a copy: only this path takes the key, and under -mm=own a parameter is taken on every path or none - this.names.push(key + ""); - this.items.length = position + 1; + public set(key: number | string, value: JsonValue): JsonValue { + let position = -1; + if (typeof key == "number") { + this.expect(JSON_ARRAY, "array"); + if (key < 0) { + throw new RangeError("JSON array index is negative"); + } + + position = key; + while (this.items.length < position) { + this.items.push(new JsonValue()); + } + + if (position == this.items.length) { + this.items.length = position + 1; + } + } else { + this.expect(JSON_OBJECT, "object"); + position = this.indexOf(key); + if (position < 0) { + position = this.items.length; + // a copy: only this path takes the key, and under -mm=own a parameter is taken on every path or none + this.names.push(key + ""); + this.items.length = position + 1; + } } // in one place, for the same reason diff --git a/tests/json_index.ts b/tests/json_index.ts new file mode 100644 index 0000000..0f4611d --- /dev/null +++ b/tests/json_index.ts @@ -0,0 +1,63 @@ +// JsonValue by key: value["name"] and value[0] read through get(), value["name"] = member and +// value[0] = element write through set(). A key that names a JsonValue member (length, kind, ...) is +// that member. +function read(value: JsonValue) { + assert(value["name"].asString() == "tslang", "a literal key"); + assert(value["sizes"][1].asNumber() == 2.5, "an element of a member"); + assert(value["sizes"][7].isNull(), "an element past the end"); + assert(value["missing"].isNull(), "a missing key"); + + const key = "ok"; + assert(value[key].asBoolean(), "a const key"); + + let other = "name"; + assert(value[other].asString() == "tslang", "a variable key"); + + let index = 0; + assert(value["sizes"][index].asNumber() == 1, "a variable index"); + + // members of JsonValue, not of the JSON object + assert(value["length"] == 3, "the length getter"); + assert(value["kind"] == "object", "the kind getter"); + + // the methods take keys and indexes too + assert(value.get("sizes").get(1).asNumber() == 2.5, "get with an index"); +} + +function write() { + const value = JsonValue.newObject(); + value["n"] = JsonValue.ofNumber(1); + value["s"] = JsonValue.ofString("a"); + value["n"] = JsonValue.ofNumber(2); + assert(value["n"].asNumber() == 2, "a member written again"); + assert(value.stringify() == `{"n":2,"s":"a"}`, "written out, in order"); + + const list = JsonValue.newArray(); + list[0] = JsonValue.ofNumber(1); + list[2] = JsonValue.ofNumber(3); + list[0] = JsonValue.ofNumber(0); + assert(list.length == 3, "an element past the end makes room"); + assert(list.stringify() == `[0,null,3]`, "with nulls before it"); +} + +function main() { + read(JSON.parse(`{"name":"tslang","sizes":[1,2.5],"ok":true}`)); + + // Under -mm=own, two writes by key to a JsonValue crash the compiler (TypeScriptCompiler, + // a known issue); value.set(key, member) works there. + if (__MEMORY_MODEL__ != "own") { + write(); + } + + // an index that is not there in an array to write + let failed = false; + try { + JsonValue.newArray().set(-1, JsonValue.ofNumber(1)); + } catch (e: RangeError) { + failed = true; + } + + assert(failed, "a negative index"); + + console.log("ALL DONE"); +}