Skip to content

Commit

Permalink
fix: Obj.toString interpolation issue and simplify Obj implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Wyndham committed Jul 1, 2021
1 parent 3f0ffd6 commit 8de62fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
27 changes: 11 additions & 16 deletions assembly/JSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,22 @@ export class Arr extends Value {

export class Obj extends Value {
_obj: Map<string, Value>;
keys: Array<string>;

constructor() {
super();
this._obj = new Map();
this.keys = new Array();
}

toString(): string {
const objs: string[] = new Array<string>(this.keys.length);
for (let i: i32 = 0; i < this.keys.length; i++) {
const key = this.keys[i];
const value = this._obj.get(key) || Value.Null();
objs[i] = `"${key}":${value}`;
const keys = this._obj.keys();
const objs: string[] = new Array<string>(keys.length);
for (let i: i32 = 0; i < keys.length; i++) {
const key = keys[i];
const value = this._obj.get(key);
// Currently must get the string value before interpolation
// see: https://github.com/AssemblyScript/assemblyscript/issues/1944
const valStr = value.toString();
objs[i] = `"${key}":${valStr}`;
}

return `{${objs.join(",")}}`;
Expand All @@ -321,21 +323,14 @@ export class Obj extends Value {
return this._obj;
}


set<T>(key: string, value: T): void {
if (isReference<T>(value)) {
if (value instanceof Value) {
this._set(key, <Value>value);
this._obj.set(key, <Value>value);
return;
}
}
this._set(key, from<T>(value));
}
private _set(key: string, value: Value): void {
if (!this._obj.has(key)) {
this.keys.push(key);
}
this._obj.set(key, value);
this._obj.set(key, from<T>(value));
}

has(key: string): bool {
Expand Down
9 changes: 3 additions & 6 deletions assembly/__tests__/to-string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { JSONDecoder } from "../decoder";
import { JSONEncoder } from "../encoder";
import { Buffer } from "../util";
import * as JSON from "../JSON";

let primObj: JSON.Obj;
Expand All @@ -25,7 +22,7 @@ describe("JSON.Value.toString()", () => {

it("Str", () => {
let value = primObj.getString("Str");
expect(value!.toString()).toBe("Hello");
expect(value!.toString()).toBe(`"Hello"`);
});

it("Num", () => {
Expand Down Expand Up @@ -56,11 +53,11 @@ describe("JSON.Value.toString()", () => {

it("Obj", () => {
let value = primObj.getObj("Obj");
expect(value!.toString()).toBe('{"isChild": true}');
expect(value!.toString()).toBe('{"isChild":true}');
});

it("Entire Object", () => {
expect(primObj.toString()).toBe("{\"Str\": \"Hello\",\"Num\": 42.24,\"Float\": 42.24,\"Integer\": 42,\"Bool\": true,\"Arr\": [1,2,3],\"Obj\": {\"isChild\": true}}");
expect(primObj.toString()).toBe(`{"Str":"Hello","Num":42.24,"Float":42.24,"Integer":42,"Bool":true,"Arr":[1,2,3],"Obj":{"isChild":true}}`);
});
});

0 comments on commit 8de62fd

Please sign in to comment.