mirror of
https://github.com/fluencelabs/assemblyscript-json
synced 2025-07-31 05:52:05 +00:00
Move JSON encoder from test to it’s proper place
This commit is contained in:
@@ -1,126 +1,19 @@
|
||||
import "allocator/arena";
|
||||
|
||||
import { JSONDecoder, JSONHandler } from "../../assembly/decoder";
|
||||
import { JSONDecoder } from "../../assembly/decoder";
|
||||
import { JSONEncoder } from "../../assembly/encoder";
|
||||
|
||||
declare function logStr(str: string): void;
|
||||
declare function logF64(val: f64): void;
|
||||
|
||||
class JSONTestHandler extends JSONHandler {
|
||||
isFirstKey: boolean = true
|
||||
inObject: Array<boolean> = [false]
|
||||
result: string = ""
|
||||
|
||||
setString(name: string, value: string): void {
|
||||
this.writeKey(name);
|
||||
this.writeString(value);
|
||||
}
|
||||
|
||||
setBoolean(name: string, value: bool): void {
|
||||
this.writeKey(name);
|
||||
this.writeBoolean(value);
|
||||
}
|
||||
|
||||
setNull(name: string): void {
|
||||
this.writeKey(name);
|
||||
this.write("null");
|
||||
}
|
||||
|
||||
setInteger(name: string, value: i32): void {
|
||||
this.writeKey(name);
|
||||
this.writeInteger(value);
|
||||
}
|
||||
|
||||
pushArray(name: string): bool {
|
||||
this.writeKey(name);
|
||||
this.write("[");
|
||||
this.isFirstKey = true
|
||||
this.inObject.push(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
popArray(): void {
|
||||
this.write("]");
|
||||
}
|
||||
|
||||
pushObject(name: string): bool {
|
||||
this.writeKey(name);
|
||||
this.write("{");
|
||||
this.isFirstKey = true
|
||||
this.inObject.push(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
popObject(): void {
|
||||
this.write("}");
|
||||
}
|
||||
|
||||
private writeKey(str: string): void {
|
||||
if (!this.isFirstKey ) {
|
||||
this.write(",");
|
||||
} else {
|
||||
this.isFirstKey = false;
|
||||
}
|
||||
if (str != null) {
|
||||
this.writeString(str);
|
||||
this.write(":");
|
||||
}
|
||||
}
|
||||
|
||||
private writeString(str: string): void {
|
||||
this.write('"');
|
||||
let savedIndex = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
let char = str.charCodeAt(i);
|
||||
let needsEscaping = char < 0x20 || char == '"'.charCodeAt(0) || char == '\\'.charCodeAt(0);
|
||||
if (needsEscaping) {
|
||||
this.write(str.substring(savedIndex, i));
|
||||
savedIndex = i + 1;
|
||||
if (char == '"'.charCodeAt(0)) {
|
||||
this.write('\\"');
|
||||
} else if (char == "\\".charCodeAt(0)) {
|
||||
this.write("\\\\");
|
||||
} else if (char == "\b".charCodeAt(0)) {
|
||||
this.write("\\b");
|
||||
} else if (char == "\n".charCodeAt(0)) {
|
||||
this.write("\\n");
|
||||
} else if (char == "\r".charCodeAt(0)) {
|
||||
this.write("\\r");
|
||||
} else if (char == "\t".charCodeAt(0)) {
|
||||
this.write("\\t");
|
||||
} else {
|
||||
// TODO: Implement encoding for other contol characters
|
||||
assert(false, "Unsupported control chracter");
|
||||
}
|
||||
}
|
||||
}
|
||||
this.write(str.substring(savedIndex, str.length));
|
||||
this.write('"');
|
||||
}
|
||||
|
||||
private writeBoolean(value: bool): void {
|
||||
this.write(value ? "true" : "false");
|
||||
}
|
||||
|
||||
private writeInteger(value: i32): void {
|
||||
// TODO: More efficient encoding
|
||||
let arr: Array<i32> = [value];
|
||||
this.write(arr.toString());
|
||||
}
|
||||
|
||||
private write(str: string): void {
|
||||
this.result += str;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class StringConversionTests {
|
||||
private static handler : JSONTestHandler = new JSONTestHandler();
|
||||
private static handler : JSONEncoder = null;
|
||||
|
||||
static setUp(): void {
|
||||
this.handler = new JSONTestHandler();
|
||||
this.handler = new JSONEncoder();
|
||||
}
|
||||
|
||||
static createDecoder(): JSONDecoder<JSONTestHandler> {
|
||||
static createDecoder(): JSONDecoder<JSONEncoder> {
|
||||
return new JSONDecoder(this.handler);
|
||||
}
|
||||
|
||||
@@ -208,24 +101,10 @@ export class StringConversionTests {
|
||||
buffer[i] = load<u8>(utf8ptr + i);
|
||||
}
|
||||
this.createDecoder().deserialize(buffer);
|
||||
assert(this.handler.result == expectedString,
|
||||
"Expected:\n" + expectedString + "\n" + "Actual:\n" + this.handler.result);
|
||||
let resultBuffer = this.handler.serialize();
|
||||
let resultString = String.fromUTF8(resultBuffer.buffer.data, resultBuffer.length);
|
||||
assert(resultString == expectedString,
|
||||
"Expected:\n" + expectedString + "\n" + "Actual:\n" + resultString)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function bytes2array(typedArr: Uint8Array): Array<u8> {
|
||||
let arr = new Array<u8>();
|
||||
for (let i = 0; i < typedArr.length; i++) {
|
||||
arr.push(typedArr[i]);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
function hex2bin(hex: string): Uint8Array {
|
||||
let bin = new Uint8Array(hex.length >>> 1);
|
||||
for (let i = 0, len = hex.length >>> 1; i < len; i++) {
|
||||
bin[i] = u32(parseInt(hex.substr(i << 1, 2), 16));
|
||||
}
|
||||
return bin;
|
||||
}
|
Reference in New Issue
Block a user