Skip to content

JsonValue by key: value["name"] and value[0] - #86

Merged
ASDAlexander77 merged 1 commit into
mainfrom
json-index-access
Nov 9, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
json-index-access

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

  • JsonValue declares [key: number | string]: JsonValue.
    • value["name"] and value[0] read through get().
    • value["name"] = member and value[0] = element write through set().
  • get and set now take number | string: a number for an array, 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.
  • New tests/json_index.ts, and a README note in the JSON section.

Depends on a compiler release

Literal string keys (value["name"]) need ASDAlexander77/TypeScriptCompiler#799 (merged) and ASDAlexander77/TypeScriptCompiler#800. This repository's CI compiles with the newest compiler release, v0.0-pre-alpha120, which has neither. So json_index.ts fails on every CI leg until a release includes both. Re-run CI after that release; compiler_tag in workflow_dispatch can pick it.

Known issue under -mm=own

Two writes by key to a JsonValue crash tslang.exe under -mm=own when it builds the exe. This happens on compiler main too, and also with variable keys. Direct set() calls work, and so does a test class of the same shape, in one file or across a shared library. So json_index.ts writes by key under the other models only, and the README says to use set() under own for now.

Testing

I built the library for gc, rc, none and own with a compiler that has #799 and #800, and ran every test in release JIT. All pass. console_readLine and stdio_bytes were run with their .stdin piped in. Debug wasn't run locally.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f024e2e into main Nov 9, 2026
2 of 10 checks passed
ASDAlexander77 added a commit that referenced this pull request Nov 9, 2026
Conflicts with #86 (JsonValue by key) in JsonValue's set() and push(), in
src/lib.ts and src/lib.d.ts: set() takes #86's `key: number | string` and
body, with this branch's `: this` result and no @Lends("this"); push()
returns `this` as here.

Tested with tslang built from TypeScriptCompiler main plus #800 and #801:
every test passes in release JIT under gc, rc, none and own (json.ts with
its chains, json_index.ts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant