Skip to content

Commit

Permalink
fix: #70
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 14, 2024
1 parent 529d636 commit 0c412c3
Show file tree
Hide file tree
Showing 21 changed files with 1,432 additions and 746 deletions.
2 changes: 1 addition & 1 deletion assembly/deserialize/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { deserializeStringArray } from "./array/string";
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
const type = changetype<nonnull<valueof<T>>>(0);
// @ts-ignore
if (isDefined(type.__JSON_Set_Key)) {
if (isDefined(type.__DESERIALIZE)) {
return deserializeObjectArray<T>(data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/array/array.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { leftBracketCode, rightBracketCode } from "../../src/chars";
import { JSON } from "../../src/json";
import { JSON } from "../..";
import { unsafeCharCodeAt } from "../../src/util";

// @ts-ignore: Decorator
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/array/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { leftBraceCode, rightBraceCode } from "../../src/chars";
import { JSON } from "../../src/json";
import { JSON } from "../..";
import { unsafeCharCodeAt } from "../../src/util";

// @ts-ignore: Decorator
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/array/object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { leftBraceCode, rightBraceCode } from "../../src/chars";
import { JSON } from "../../src/json";
import { JSON } from "../..";
import { unsafeCharCodeAt } from "../../src/util";

// @ts-ignore: Decorator
Expand Down
5 changes: 4 additions & 1 deletion assembly/deserialize/box.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { JSON } from "../src/json";
import { JSON } from "..";

// @ts-ignore: Decorator
@inline export function deserializeBox<T extends Box<any>>(data: string): T {
if (isNullable<T>() && data == "null") {
return null;
}
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>;
const val = instance._val;
instance._val = parseDirectInference(val, data);
Expand Down
2 changes: 1 addition & 1 deletion assembly/deserialize/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
uCode
} from "../src/chars";
import { deserializeBoolean } from "./bool";
import { JSON } from "../src/json";
import { JSON } from "..";
import { deserializeString } from "./string";
import { isSpace } from "util/string";
import { deserializeInteger } from "./integer";
Expand Down
249 changes: 127 additions & 122 deletions assembly/deserialize/object.ts
Original file line number Diff line number Diff line change
@@ -1,139 +1,144 @@
import { Virtual } from "as-virtual/assembly";
import { containsCodePoint, unsafeCharCodeAt } from "../src/util";
import { nullWord,trueWord,falseWord,aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, leftBracketCode, nCode, quoteCode, rCode, rightBraceCode, rightBracketCode, sCode, tCode, uCode } from "../src/chars";
import { deserializeString } from "./string";
import { aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, leftBracketCode, nCode, quoteCode, rCode, rightBraceCode, rightBracketCode, sCode, tCode, uCode } from "../src/chars";
import { isSpace } from "util/string";

// @ts-ignore: Decorator
@inline export function deserializeObject<T>(data: string, initializeDefaultValues: boolean): T {
const schema: nonnull<T> = changetype<nonnull<T>>(
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
);
// @ts-ignore
if (initializeDefaultValues) schema.__JSON_Initialize();
const key = Virtual.createEmpty<string>();
let isKey = false;
let depth = 0;
let outerLoopIndex = 1;
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
const char = unsafeCharCodeAt(data, outerLoopIndex);
if (char === leftBracketCode) {
for (
let arrayValueIndex = outerLoopIndex;
arrayValueIndex < data.length - 1;
arrayValueIndex++
) {
const char = unsafeCharCodeAt(data, arrayValueIndex);
if (char === leftBracketCode) {
depth++;
} else if (char === rightBracketCode) {
depth--;
if (depth === 0) {
++arrayValueIndex;
// @ts-ignore
schema.__JSON_Set_Key(key, data, outerLoopIndex, arrayValueIndex, initializeDefaultValues);
outerLoopIndex = arrayValueIndex;
isKey = false;
break;
}
const schema: nonnull<T> = changetype<nonnull<T>>(
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
);

// @ts-ignore
if (initializeDefaultValues) schema.__INITIALIZE();

let key_start: i32 = 0;
let key_end: i32 = 0;
let isKey = false;
let depth = 0;
let outerLoopIndex = 1;
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
const char = unsafeCharCodeAt(data, outerLoopIndex);
if (char === leftBracketCode) {
for (
let arrayValueIndex = outerLoopIndex;
arrayValueIndex < data.length - 1;
arrayValueIndex++
) {
const char = unsafeCharCodeAt(data, arrayValueIndex);
if (char === leftBracketCode) {
depth++;
} else if (char === rightBracketCode) {
depth--;
if (depth === 0) {
++arrayValueIndex;
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, arrayValueIndex);
outerLoopIndex = arrayValueIndex;
isKey = false;
break;
}
}
} else if (char === leftBraceCode) {
for (
let objectValueIndex = outerLoopIndex;
objectValueIndex < data.length - 1;
objectValueIndex++
) {
const char = unsafeCharCodeAt(data, objectValueIndex);
if (char === leftBraceCode) {
depth++;
} else if (char === rightBraceCode) {
depth--;
if (depth === 0) {
++objectValueIndex;
// @ts-ignore
schema.__JSON_Set_Key(key, data, outerLoopIndex, objectValueIndex, initializeDefaultValues);
outerLoopIndex = objectValueIndex;
isKey = false;
break;
}
}
} else if (char === leftBraceCode) {
for (
let objectValueIndex = outerLoopIndex;
objectValueIndex < data.length - 1;
objectValueIndex++
) {
const char = unsafeCharCodeAt(data, objectValueIndex);
if (char === leftBraceCode) {
depth++;
} else if (char === rightBraceCode) {
depth--;
if (depth === 0) {
++objectValueIndex;
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, objectValueIndex);
outerLoopIndex = objectValueIndex;
isKey = false;
break;
}
}
} else if (char === quoteCode) {
let escaping = false;
for (
let stringValueIndex = ++outerLoopIndex;
stringValueIndex < data.length - 1;
stringValueIndex++
) {
const char = unsafeCharCodeAt(data, stringValueIndex);
if (char === backSlashCode && !escaping) {
escaping = true;
} else {
if (char === quoteCode && !escaping) {
if (isKey === false) {
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
} else {
key.reinst(data, outerLoopIndex, stringValueIndex);
}
isKey = true;
}
} else if (char === quoteCode) {
let escaping = false;
for (
let stringValueIndex = ++outerLoopIndex;
stringValueIndex < data.length - 1;
stringValueIndex++
) {
const char = unsafeCharCodeAt(data, stringValueIndex);
if (char === backSlashCode && !escaping) {
escaping = true;
} else {
if (char === quoteCode && !escaping) {
if (isKey === false) {
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
key_start = outerLoopIndex - 1;
key_end = stringValueIndex;
console.log(`[KEY-00]: ${data.slice(outerLoopIndex - 1, stringValueIndex)}`)
} else {
const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
// @ts-ignore
schema.__JSON_Set_Key(key, value, 0, value.length, initializeDefaultValues);
isKey = false;
key_start = outerLoopIndex;
key_end = stringValueIndex;
console.log(`[KEY-01]: ${data.slice(outerLoopIndex, stringValueIndex)}`)
}
outerLoopIndex = ++stringValueIndex;
break;
isKey = true;
} else {
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, stringValueIndex);
isKey = false;
}
escaping = false;
}
}
} else if (
char == nCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode
) {
// @ts-ignore
schema.__JSON_Set_Key(key, nullWord, 0, 4, initializeDefaultValues);
isKey = false;
} else if (
char === tCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === rCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
) {
// @ts-ignore
schema.__JSON_Set_Key(key, trueWord, 0, 4, initializeDefaultValues);
isKey = false;
} else if (
char === fCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === aCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === sCode &&
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
) {
// @ts-ignore
schema.__JSON_Set_Key(key, falseWord, 0, 5, initializeDefaultValues);
isKey = false;
} else if ((char >= 48 && char <= 57) || char === 45) {
let numberValueIndex = ++outerLoopIndex;
for (; numberValueIndex < data.length; numberValueIndex++) {
const char = unsafeCharCodeAt(data, numberValueIndex);
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
// @ts-ignore
schema.__JSON_Set_Key(key, data, outerLoopIndex - 1, numberValueIndex, initializeDefaultValues);
outerLoopIndex = numberValueIndex;
isKey = false;
outerLoopIndex = ++stringValueIndex;
break;
}
escaping = false;
}
}
} else if (
char == nCode &&
unsafeCharCodeAt(data, outerLoopIndex + 1) === uCode &&
unsafeCharCodeAt(data, outerLoopIndex + 2) === lCode &&
unsafeCharCodeAt(data, outerLoopIndex + 3) === lCode
) {
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
outerLoopIndex += 3;
isKey = false;
} else if (
char === tCode &&
unsafeCharCodeAt(data, outerLoopIndex + 1) === rCode &&
unsafeCharCodeAt(data, outerLoopIndex + 2) === uCode &&
unsafeCharCodeAt(data, outerLoopIndex + 3) === eCode
) {
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
outerLoopIndex += 3;
isKey = false;
} else if (
char === fCode &&
unsafeCharCodeAt(data, outerLoopIndex + 1) === aCode &&
unsafeCharCodeAt(data, outerLoopIndex + 2) === lCode &&
unsafeCharCodeAt(data, outerLoopIndex + 3) === sCode &&
unsafeCharCodeAt(data, outerLoopIndex + 4) === eCode
) {
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5);
outerLoopIndex += 4;
isKey = false;
} else if ((char >= 48 && char <= 57) || char === 45) {
let numberValueIndex = ++outerLoopIndex;
for (; numberValueIndex < data.length; numberValueIndex++) {
const char = unsafeCharCodeAt(data, numberValueIndex);
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
// @ts-ignore
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, numberValueIndex);
outerLoopIndex = numberValueIndex;
isKey = false;
break;
}
}
}
return schema;
}
}
return schema;
}
8 changes: 7 additions & 1 deletion assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ declare function serializable(target: any): void;
*/
declare function alias(name: string): Function;

/**
* Property decorator that allows omits a field, making it be ignored.
*/
declare function omit(): Function;


/**
* Property decorator that allows a field to be omitted when equal to an Expression.
*/
declare function omitwhen(condition: string): Function;
declare function omitif(condition: string): Function;

/**
* Property decorator that allows a field to be omitted when a property is null.
Expand Down
Loading

0 comments on commit 0c412c3

Please sign in to comment.