Skip to content

Commit 8de62fd

Browse files
author
Willem Wyndham
committed
fix: Obj.toString interpolation issue and simplify Obj implementation
1 parent 3f0ffd6 commit 8de62fd

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

assembly/JSON.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -298,20 +298,22 @@ export class Arr extends Value {
298298

299299
export class Obj extends Value {
300300
_obj: Map<string, Value>;
301-
keys: Array<string>;
302301

303302
constructor() {
304303
super();
305304
this._obj = new Map();
306-
this.keys = new Array();
307305
}
308306

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

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

324-
325326
set<T>(key: string, value: T): void {
326327
if (isReference<T>(value)) {
327328
if (value instanceof Value) {
328-
this._set(key, <Value>value);
329+
this._obj.set(key, <Value>value);
329330
return;
330331
}
331332
}
332-
this._set(key, from<T>(value));
333-
}
334-
private _set(key: string, value: Value): void {
335-
if (!this._obj.has(key)) {
336-
this.keys.push(key);
337-
}
338-
this._obj.set(key, value);
333+
this._obj.set(key, from<T>(value));
339334
}
340335

341336
has(key: string): bool {

assembly/__tests__/to-string.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { JSONDecoder } from "../decoder";
2-
import { JSONEncoder } from "../encoder";
3-
import { Buffer } from "../util";
41
import * as JSON from "../JSON";
52

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

2623
it("Str", () => {
2724
let value = primObj.getString("Str");
28-
expect(value!.toString()).toBe("Hello");
25+
expect(value!.toString()).toBe(`"Hello"`);
2926
});
3027

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

5754
it("Obj", () => {
5855
let value = primObj.getObj("Obj");
59-
expect(value!.toString()).toBe('{"isChild": true}');
56+
expect(value!.toString()).toBe('{"isChild":true}');
6057
});
6158

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

0 commit comments

Comments
 (0)