Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 46 additions & 13 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2349,34 +2349,67 @@ 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 = <int>key;
}
} else {
this.expect(JSON_OBJECT, "object");
position = this.indexOf(key);
}

if (position < 0) {
return this.nothing();
}

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 = <int>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
Expand Down
63 changes: 63 additions & 0 deletions tests/json_index.ts
Original file line number Diff line number Diff line change
@@ -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");
}
Loading